X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flanguage%2Fxforms%2Fsample.c;h=037cff4a38854ea7afb17d2092e388b884b5c13e;hb=20a881a9067a01d798ff15e6bf0d1103687ad651;hp=f2a30a2a58fdbfc5e94958eec4bd65fbdc710464;hpb=9ade26c8349b4434008c46cf09bc7473ec743972;p=pspp diff --git a/src/language/xforms/sample.c b/src/language/xforms/sample.c index f2a30a2a58..037cff4a38 100644 --- a/src/language/xforms/sample.c +++ b/src/language/xforms/sample.c @@ -21,7 +21,7 @@ #include #include -#include "data/procedure.h" +#include "data/dataset.h" #include "data/variable.h" #include "language/command.h" #include "language/lexer/lexer.h" @@ -51,8 +51,7 @@ struct sample_trns unsigned frac; /* TYPE_FRACTION: a fraction of UINT_MAX. */ }; -static trns_proc_func sample_trns_proc; -static trns_free_func sample_trns_free; +static const struct trns_class sample_trns_class; int cmd_sample (struct lexer *lexer, struct dataset *ds) @@ -71,12 +70,8 @@ cmd_sample (struct lexer *lexer, struct dataset *ds) unsigned long max = gsl_rng_max (get_rng ()); type = TYPE_FRACTION; - if (lex_tokval (lexer) <= 0 || lex_tokval (lexer) >= 1) - { - msg (SE, _("The sampling factor must be between 0 and 1 " - "exclusive.")); - return CMD_FAILURE; - } + if (!lex_force_num_range_open (lexer, "SAMPLE", 0, 1)) + return CMD_FAILURE; frac = lex_tokval (lexer) * (max - min) + min; a = b = 0; @@ -88,17 +83,9 @@ cmd_sample (struct lexer *lexer, struct dataset *ds) lex_get (lexer); if (!lex_force_match_id (lexer, "FROM")) return CMD_FAILURE; - if (!lex_force_int (lexer)) + if (!lex_force_int_range (lexer, "FROM", a + 1, INT_MAX)) return CMD_FAILURE; b = lex_integer (lexer); - if (a >= b) - { - msg (SE, _("Cannot sample %d observations from a population of " - "%d."), - a, b); - return CMD_FAILURE; - } - frac = 0; } lex_get (lexer); @@ -109,13 +96,13 @@ cmd_sample (struct lexer *lexer, struct dataset *ds) trns->N = b; trns->m = trns->t = 0; trns->frac = frac; - add_transformation (ds, sample_trns_proc, sample_trns_free, trns); + add_transformation (ds, &sample_trns_class, trns); return CMD_SUCCESS; } /* Executes a SAMPLE transformation. */ -static int +static enum trns_result sample_trns_proc (void *t_, struct ccase **c UNUSED, casenumber case_num UNUSED) { @@ -154,3 +141,9 @@ sample_trns_free (void *t_) free (t); return true; } + +static const struct trns_class sample_trns_class = { + .name = "SAMPLE", + .execute = sample_trns_proc, + .destroy = sample_trns_free, +};