#define _EXTANT_xt_oset_t_C_ #include #include #include "xt_oset_t.h" #ifndef xt_oset_t_n_options #define xt_oset_t_n_options (1) #endif char *xt_oset_t_options[] = { "noscalar", NULL }; int xt_oset_init ( xt_oset_t *x, int32 n_ ) { int32 n = n_; if ( x == NULL ) return ( -1 ); if ( n <= 0 ) n = 1; x->n = 0; x->max = n; x->d = (oset_t*) malloc ( sizeof(oset_t) * n ); if ( x->d == NULL ) return ( -2 ); return ( 0 ); } oset_t *xt_oset_fetchb ( xt_oset_t *x, int32 n, oset_t *buf ) { if ( x == NULL ) return (NULL); if ( n >= x->n ) return (NULL); if ( x->d == NULL ) return (NULL); memcpy ( buf, &(x->d[n]), sizeof(oset_t) ); return ( buf ); } oset_t *xt_oset_fetchc ( xt_oset_t *x, int32 n ) { oset_t *p; p = (oset_t*) malloc ( sizeof(oset_t) ); if ( x == NULL ) return (NULL); if ( p == NULL ) return (NULL); if ( n >= x->n ) return (NULL); if ( x->d == NULL ) return (NULL); memcpy ( p, &(x->d[n]), sizeof(oset_t) ); return ( p ); } oset_t *xt_oset_fetchp ( xt_oset_t *x, int32 n ) { if ( x == NULL ) return (NULL); if ( n >= x->n ) return (NULL); if ( x->d == NULL ) return (NULL); return ( &(x->d[n]) ); } int xt_oset_addp ( xt_oset_t *x, oset_t *d ) { if ( x == NULL ) return (-1); if ( x->d == NULL ) return (-2); if ( x->max == 0 ) return (-4); if ( x->n >= x->max ) { oset_t *p = x->d; int32 i,k; k = x->n; x->max <<= 1; x->d = (oset_t*) malloc ( sizeof(oset_t) * x->max ); if ( x->d == NULL ) { x->d = p; x->max >>= 1; return (-2); } memcpy ( x->d, p, k * sizeof(oset_t) ); free ( p ); } memcpy ( &(x->d[x->n++]), d, sizeof(oset_t) ); return (0); } int xt_oset_add ( xt_oset_t *x, oset_t d ) { if ( x == NULL ) return (-1); if ( x->max == 0 ) return (-4); if ( x->n >= x->max ) { oset_t *p = x->d; int32 i,k; k = x->n; x->max <<= 1; x->d = (oset_t*) malloc ( sizeof(oset_t) * x->max ); if ( x->d == NULL ) { x->d = p; x->max >>= 1; return (-2); } memcpy ( x->d, p, k * sizeof(oset_t) ); free ( p ); } x->d[x->n++] = d; return (0); } int xt_oset_wrp ( xt_oset_t *x, int32 ix, oset_t *d ) { if ( x == NULL ) return (-1); if ( x->d == NULL ) return (-2); if ( x->max == 0 ) return (-4); if ( ix >= x->n ) return (-5); memcpy ( &(x->d[ix]), d, sizeof(oset_t) ); return (0); } int xt_oset_addn ( xt_oset_t *x, oset_t *str, int32 slen ) { int32 i; if ( x == NULL ) return (-1); if ( str == NULL ) return (-1); if ( slen == 0 ) return ( 0); if ( slen < 0 ) return (-1); if ( x->max == 0 ) return (-4); if ( x->n + slen > x->max ) { oset_t *p = x->d; int32 k; k = x->n; x->max += slen; x->d = (oset_t*) malloc ( x->max + 1 ); if ( x->d == NULL ) { x->d = p; x->max -= slen; return (-2); } memcpy ( x->d, p, k * sizeof ( oset_t ) ); free ( p ); memset ( &(x->d[k]), 0, sizeof ( oset_t ) ); } i = x->n; memcpy ( &(x->d[i]), str, slen * sizeof ( oset_t ) ); x->n += i; memset ( &(x->d[x->n]), 0, sizeof ( oset_t ) ); return (0); } int xt_oset_del ( xt_oset_t *x, int32 i, xt_oset_nuker_ft f ) { if ( x == NULL ) return (-1); if ( i < 0 ) return (-3); if ( i >= x->n ) return (-4); if ( x->n > 1 ) { int32 j = i+1; if ( f != NULL ) f ( &(x->d[i]) ); memmove ( &(x->d[i]), &(x->d[j]), ( x->n - j ) * sizeof(oset_t) ); x->n--; } return (0); } int xt_oset_deln ( xt_oset_t *x, int32 i, xt_oset_nuker_ft f, int32 n ) { int32 h, k; if ( x == NULL ) return (-1); if ( i < 0 ) return (-3); if ( n < 0 ) return (-3); if ( i >= x->n ) return (-4); if ( x->n > 1 ) { int32 j = i + 1; if ( f != NULL ) { k = i + n; if ( k > x->n ) k = x->n; for ( h = i; h < k; h++ ) f ( &(x->d[h]) ); } memmove ( &(x->d[i]), &(x->d[j]), ( x->n - j ) * sizeof(oset_t) ); x->n--; } return (0); } void xt_oset_nuke ( xt_oset_t *x, xt_oset_nuker_ft f ) { int32 i, k; if ( f != NULL ) for ( i = 0, k = x->n; i < k; i++ ) f ( &(x->d[i]) ); if ( x->d != NULL ) free ( x->d ); x->d = NULL; x->n = 0; x->max = 0; }