/* See spmat.h */ /* Last edited on 2008-07-22 23:01:12 by stolfi */ #include #include #include #include void *spmat_alloc(uint32_t nr, uint32_t nc, uint32_t ne, size_t esz) { void *e = (ne == 0 ? NULL : malloc(ne*esz)); affirm((ne == 0) || (e != NULL), "out of mem"); return e; } void spmat_expand(uint32_t *nep, void **ep, uint32_t index, size_t esz) { if (index >= (*nep)) { int ne = (*nep) + index + 1; if ((*nep) == 0) { affirm((*ep) == NULL, "bad elem pointer"); } (*ep) = realloc((*ep), ne*esz); affirm((*ep) != NULL, "out of mem"); (*nep) = ne; } } void spmat_trim(uint32_t *nep, void **ep, uint32_t ne, size_t esz) { if (ne != (*nep)) { if (ne == 0) { free((*ep)); (*ep) = NULL; } else { (*ep) = realloc((*ep), ne*esz); affirm((*ep) != NULL, "out of mem"); } (*nep) = ne; } } /* SOME USEFUL TYPED SPMATTORS */ spmat_typeimpl(int_spmat_t, int_spmat, int); spmat_typeimpl(uint_spmat_t, uint_spmat, unsigned int); spmat_typeimpl(char_spmat_t, char_spmat, char); spmat_typeimpl(bool_spmat_t, bool_spmat, bool_t); spmat_typeimpl(float_spmat_t, float_spmat, float); spmat_typeimpl(double_spmat_t, double_spmat, double); spmat_typeimpl(ref_spmat_t, ref_spmat, void*); spmat_typeimpl(string_spmat_t, string_spmat, char*); spmat_typeimpl(int8_spmat_t, int8_spmat, int8_t); spmat_typeimpl(int16_spmat_t, int16_spmat, int16_t); spmat_typeimpl(int32_spmat_t, int32_spmat, int32_t); spmat_typeimpl(int64_spmat_t, int64_spmat, int64_t); spmat_typeimpl(uint8_spmat_t, uint8_spmat, uint8_t); spmat_typeimpl(uint16_spmat_t, uint16_spmat, uint16_t); spmat_typeimpl(uint32_spmat_t, uint32_spmat, uint32_t); spmat_typeimpl(uint64_spmat_t, uint64_spmat, uint64_t);