X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fcase.c;h=1384791c82665c359eb3755e37516641c41f788d;hb=92fb12eb06716d14c05b781f5d9dcde956d77c30;hp=21fb0bdbfdb725a58cd73f5640e5ed8c5f9ebee7;hpb=b321086267ad1014dc5d09886396cde30f094437;p=pspp diff --git a/src/case.c b/src/case.c index 21fb0bdbfd..1384791c82 100644 --- a/src/case.c +++ b/src/case.c @@ -14,8 +14,8 @@ You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA - 02111-1307, USA. */ + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. */ #include #include "case.h" @@ -24,6 +24,7 @@ #include "val.h" #include "alloc.h" #include "str.h" +#include "var.h" #ifdef GLOBAL_DEBUGGING #undef NDEBUG @@ -91,7 +92,7 @@ void case_create (struct ccase *c, size_t value_cnt) { if (!case_try_create (c, value_cnt)) - out_of_memory (); + xalloc_die (); } #ifdef GLOBAL_DEBUGGING @@ -152,6 +153,27 @@ case_destroy (struct ccase *c) } #endif /* GLOBAL_DEBUGGING */ +/* Resizes case C from OLD_CNT to NEW_CNT values. */ +void +case_resize (struct ccase *c, size_t old_cnt, size_t new_cnt) +{ + struct ccase new; + + case_create (&new, new_cnt); + case_copy (&new, 0, c, 0, old_cnt < new_cnt ? old_cnt : new_cnt); + case_swap (&new, c); + case_destroy (&new); +} + +/* Swaps cases A and B. */ +void +case_swap (struct ccase *a, struct ccase *b) +{ + struct case_data *t = a->case_data; + a->case_data = b->case_data; + b->case_data = t; +} + /* Attempts to create C as a new case that holds VALUE_CNT values. Returns nonzero if successful, zero if memory allocation failed. */ @@ -328,6 +350,52 @@ case_data_rw (struct ccase *c, size_t idx) } #endif /* GLOBAL_DEBUGGING */ +/* Compares the values of the VAR_CNT variables in VP + in cases A and B and returns a strcmp()-type result. */ +int +case_compare (const struct ccase *a, const struct ccase *b, + struct variable *const *vp, size_t var_cnt) +{ + return case_compare_2dict (a, b, vp, vp, var_cnt); +} + +/* Compares the values of the VAR_CNT variables in VAP in case CA + to the values of the VAR_CNT variables in VBP in CB + and returns a strcmp()-type result. */ +int +case_compare_2dict (const struct ccase *ca, const struct ccase *cb, + struct variable *const *vap, struct variable *const *vbp, + size_t var_cnt) +{ + for (; var_cnt-- > 0; vap++, vbp++) + { + const struct variable *va = *vap; + const struct variable *vb = *vbp; + + assert (va->type == vb->type); + assert (va->width == vb->width); + + if (va->width == 0) + { + double af = case_num (ca, va->fv); + double bf = case_num (cb, vb->fv); + + if (af != bf) + return af > bf ? 1 : -1; + } + else + { + const char *as = case_str (ca, va->fv); + const char *bs = case_str (cb, vb->fv); + int cmp = memcmp (as, bs, va->width); + + if (cmp != 0) + return cmp; + } + } + return 0; +} + /* Returns a pointer to the array of `union value's used for C. The caller must *not* modify the returned data.