/* See vec.h */ /* Last edited on 2005-01-16 15:06:41 by stolfi */ /* Copyright © 2005 Jorge Stolfi, UNICAMP. See note at end of file. */ #include #include #include vec_t vec_new(int nel, size_t elsz) { vec_t v; v.nel = nel; v.el = malloc(nel*elsz); affirm((nel == 0) || (v.el != NULL), "out of mem"); return v; } void vec_expand(vec_t *v, nat index, size_t elsz) { if (index >= v->nel) { int nel = v->nel + index + 1; v->el = realloc(v->el, nel*elsz); affirm((nel == 0) || (v->el != NULL), "out of mem"); v->nel = nel; } } void vec_trim(vec_t *v, nat nel, size_t elsz) { if (nel != v->nel) { v->el = realloc(v->el, nel*elsz); affirm((nel == 0) || (v->el != NULL), "out of mem"); v->nel = nel; } } vec_t *vec_cast_ref(void *v) { return (vec_t *)v; } vec_sub_t vec_as_sub(vec_t *v) { vec_sub_t vs; vs.nel = v->nel; vs.el = v->el; vs.step = 1; return vs; } vec_sub_t vec_sub(vec_sub_t *vs, nat start, int step, nat nel, size_t elsz) { vec_sub_t rs; affirm(step != 0, "step cannot be zero"); if (start >= vs->nel) { rs.nel = 0; } else { int maxnel = (step > 0 ? (vs->nel - start)/step : (start-1)/(-step) + 1); rs.nel = (nel <= maxnel ? nel : maxnel); } if (rs.nel == 0) { rs.el = vs->el; rs.step = 1; } else { rs.el = (void *)((char *)vs->el + elsz*(start*vs->step)); rs.step = step*vs->step; } return rs; } /* SOME USEFUL TYPED VECTORS */ int_vec_t int_vec_new(nat nel) { /* This is not a macro only because gcc does not allow cast of struct: */ vec_t v = vec_new(nel, sizeof(int)); int_vec_t r; r.nel = v.nel; r.el = (int *)v.el; return r; } nat_vec_t nat_vec_new(nat nel) { /* This is not a macro only because gcc does not allow cast of struct: */ vec_t v = vec_new(nel, sizeof(nat)); nat_vec_t r; r.nel = v.nel; r.el = (nat *)v.el; return r; } double_vec_t double_vec_new(nat nel) { /* This is not a macro only because gcc does not allow cast of struct: */ vec_t v = vec_new(nel, sizeof(double)); double_vec_t r; r.nel = v.nel; r.el = (double *)v.el; return r; } bool_vec_t bool_vec_new(nat nel) { /* This is not a macro only because gcc does not allow cast of struct: */ vec_t v = vec_new(nel, sizeof(bool)); bool_vec_t r; r.nel = v.nel; r.el = (bool *)v.el; return r; } char_vec_t char_vec_new(nat nel) { /* This is not a macro only because gcc does not allow cast of struct: */ vec_t v = vec_new(nel, sizeof(char)); char_vec_t r; r.nel = v.nel; r.el = (char *)v.el; return r; } string_vec_t string_vec_new(nat nel) { /* This is not a macro only because gcc does not allow cast of struct: */ vec_t v = vec_new(nel, sizeof(char *)); string_vec_t r; r.nel = v.nel; r.el = (char **)v.el; return r; } /* COPYRIGHT AND AUTHORSHIP NOTICE Copyright © 2005 Jorge Stolfi, Universidade Estadual de Campinas (UNICAMP). Created by Jorge Stolfi in 1992--2005. This source file can be freely distributed, used, and modified, provided that this copyright and authorship notice is preserved in all copies, and that any modified versions of this file are clearly marked as such. This software has NO WARRANTY of correctness or applicability for any purpose. Neither the author nor his employers shall be held responsible for any losses or damages that may result from its use. END OF NOTICE */