X-Git-Url: http://git.nguyen.vg/gitweb/?a=blobdiff_plain;ds=sidebyside;f=swcsa%2Futils%2Fbasics.c;fp=swcsa%2Futils%2Fbasics.c;h=f213ed6d4d78df843b3e9f32748f9ae3d84b4f65;hb=102e33b134075765e6d4e0c38bc1307568ce5602;hp=0000000000000000000000000000000000000000;hpb=ed61d2042a7ad7dd83bae32d7c31e69504dafa80;p=SXSI%2FTextCollection.git diff --git a/swcsa/utils/basics.c b/swcsa/utils/basics.c new file mode 100755 index 0000000..f213ed6 --- /dev/null +++ b/swcsa/utils/basics.c @@ -0,0 +1,107 @@ + +// Basics + +// #include "basics.h" included later to avoid macro recursion for malloc +#include +#include +#include + + // Memory management + + void *Malloc (int n) + + { void *p; + if (n == 0) return NULL; + p = (void*) malloc (n); + if (p == NULL) + { fprintf (stderr,"Could not allocate %i bytes\n",n); + exit(1); + } + return p; + } + + void Free (void *p) + + { if (p) free (p); + } + + void *Realloc (void *p, int n) + + { if (p == NULL) return Malloc (n); + if (n == 0) { Free(p); return NULL; } + p = (void*) realloc (p,n); + if (p == NULL) + { fprintf (stderr,"Could not allocate %i bytes\n",n); + exit(1); + } + return p; + } + +#include "basics.h" + + // bits needed to represent a number between 0 and n + +uint bits (uint n) + + { uint b = 0; + while (n) + { b++; n >>= 1; } + return b; + } + + // returns e[p..p+len-1], assuming len <= W + +uint bitread (uint *e, uint p, uint len) + + { uint answ; + e += p/W; p %= W; + answ = *e >> p; + if (len == W) + { if (p) answ |= (*(e+1)) << (W-p); + } + else { if (p+len > W) answ |= (*(e+1)) << (W-p); + answ &= (1<> (W-p)); + } + else { if (p+len <= W) + { *e = (*e & ~(((1<> (W-p)); + } + } + // writes e[p..p+len-1] = 0 + +void bitzero (register uint *e, register uint p, + register uint len) + + { e += p/W; p %= W; + if (p+len >= W) + { *e &= ~((1<= W) + { *e++ = 0; + len -= W; + } + if (len > 0) + *e &= ~(((1<