00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00028 #include <stdlib.h>
00029
00030 #if !defined(_TIFA_BUCKETS_H_)
00031 #define _TIFA_BUCKETS_H_
00032
00033 #ifdef __cplusplus
00034 extern "C" {
00035 #endif
00036
00037
00038 #define A_MASK 0xFFFFFF00
00039 #define B_MASK 0x000000FF
00040 #define GET_A(X) (((X) & A_MASK) >> 8)
00041 #define GET_B(X) ((X) & B_MASK)
00042
00043 struct struct_buckets_t {
00044 uint16_t nbins;
00045 uint32_array_t** bins;
00046 };
00047
00048 typedef struct struct_buckets_t buckets_t;
00049
00050 static inline buckets_t*
00051 alloc_buckets(uint16_t balloced, uint8_t nbins) {
00052
00053 buckets_t* buckets = malloc(sizeof(buckets_t));
00054
00055 buckets->nbins = nbins;
00056 buckets->bins = malloc(nbins * sizeof(uint32_array_t*));
00057
00058 for (uint8_t i = 0; i < nbins; i++) {
00059 buckets->bins[i] = alloc_uint32_array(balloced);
00060 }
00061 return buckets;
00062 }
00063
00064 static inline void
00065 free_buckets(buckets_t* const buckets) {
00066 if (buckets != NULL) {
00067 for (uint8_t i = 0; i < buckets->nbins; i++) {
00068 free_uint32_array(buckets->bins[i]);
00069 }
00070 free(buckets->bins);
00071 free(buckets);
00072 }
00073 }
00074
00075 static inline void
00076 reset_buckets(buckets_t* const buckets) {
00077 for (uint8_t i = 0; i < buckets->nbins; i++) {
00078 buckets->bins[i]->length = 0;
00079 }
00080 }
00081
00082 static inline void
00083 add_to_buckets(const uint32_t a, const uint8_t b, const uint8_t ibin,
00084 buckets_t* const buckets) {
00085
00086 uint32_array_t* bin = buckets->bins[ibin];
00087 uint32_t blen = bin->length;
00088
00089 if (blen == bin->alloced) {
00090 bin->data = realloc(bin->data, 2 * blen * sizeof(uint32_t));
00091 bin->alloced = 2 * blen;
00092 }
00093 bin->data[blen] = (a << 8) ^ (b & 0xFF);
00094 bin->length++;
00095 }
00096
00097 static inline void
00098 print_buckets(buckets_t* const buckets) {
00099 for (uint32_t i = 0; i < buckets->nbins; i++) {
00100 printf("bin %2i:\n", i);
00101 printf("-------\n");
00102 uint32_array_t* bin = buckets->bins[i];
00103 for (uint32_t j = 0; j < bin->length; j++) {
00104 printf(" (%5i, %2i)\n", GET_A(bin->data[j]), GET_B(bin->data[j]));
00105 }
00106 }
00107 }
00108
00109
00110 #ifdef __cplusplus
00111 }
00112 #endif
00113
00114 #endif
00115