00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00042 #if !defined(_TIFA_ARRAY_H_)
00043 
00047 #define _TIFA_ARRAY_H_
00048 
00049 #include "tifa_config.h"
00050 
00051 #include <inttypes.h>
00052 #include <stdbool.h>
00053 #include <gmp.h>
00054 
00055 #include "bitstring_t.h"
00056 
00057 #ifdef __cplusplus
00058 extern "C" {
00059 #endif
00060 
00068 #define ELONGATION 16
00069 
00075 #define NOT_IN_ARRAY UINT32_MAX
00076 
00084 #define ARRAY_IS_FULL(ARRAY_PTR) ((ARRAY_PTR)->length == (ARRAY_PTR)->alloced)
00085 
00086 
00087 
00088 
00089 
00090 
00091 
00100 struct struct_byte_array_t {
00106     uint32_t alloced;
00111     uint32_t length;
00116     unsigned char* data;
00117 };
00118 
00123 typedef struct struct_byte_array_t byte_array_t;
00124 
00136 byte_array_t* alloc_byte_array(uint32_t length);
00137 
00150 void free_byte_array(byte_array_t* array);
00151 
00163 #define reset_byte_array(ARRAY) do {(ARRAY)->length = 0;} while (0)
00164 
00177 void resize_byte_array(byte_array_t* const array, uint32_t alloced);
00178 
00190 void append_byte_to_array(byte_array_t* array, const unsigned char to_append);
00191 
00204 void append_byte_array(byte_array_t* const array,
00205                        const byte_array_t* const to_append);
00206 
00219 void swap_byte_array(byte_array_t* const a, byte_array_t* const b);
00220 
00232 void print_byte_array(const byte_array_t* const array);
00233 
00242 void ins_sort_byte_array(byte_array_t* const array);
00243 
00255 void qsort_byte_array(byte_array_t* const array);
00256 
00277 uint32_t index_in_byte_array(unsigned char to_find,
00278                              const byte_array_t* const array);
00279 
00295 inline static bool
00296 is_in_byte_array(unsigned char to_find, const byte_array_t* const array) {
00297     return (NOT_IN_ARRAY != index_in_byte_array(to_find, array));
00298 }
00299 
00321 uint32_t index_in_sorted_byte_array(unsigned char to_find,
00322                                     const byte_array_t* const sorted_array,
00323                                     uint32_t min_index, uint32_t max_index);
00324 
00336 inline static bool
00337 is_in_sorted_byte_array(unsigned char to_find,
00338                         const byte_array_t* const array) {
00339     return (NOT_IN_ARRAY != index_in_sorted_byte_array(
00340                                 to_find,
00341                                 array,
00342                                 0,
00343                                 array->length - 1
00344                             )
00345             );
00346 }
00347 
00348 
00349 
00350 
00351 
00352 
00353 
00361 struct struct_uint32_array_t {
00367     uint32_t alloced;
00372     uint32_t length;
00376     uint32_t* data;
00377 };
00378 
00383 typedef struct struct_uint32_array_t uint32_array_t;
00384 
00396 uint32_array_t* alloc_uint32_array(uint32_t length);
00397 
00410 void free_uint32_array(uint32_array_t* array);
00411 
00423 #define reset_uint32_array(ARRAY) do {(ARRAY)->length = 0;} while (0)
00424 
00437 void resize_uint32_array(uint32_array_t* const array, uint32_t alloced);
00438 
00450 void append_uint32_to_array(uint32_array_t* array, const uint32_t to_append);
00451 
00464 void append_uint32_array(uint32_array_t* const array,
00465                          const uint32_array_t* const to_append);
00466 
00479 void swap_uint32_array(uint32_array_t* const a, uint32_array_t* const b);
00480 
00492 void print_uint32_array(const uint32_array_t* const array);
00493 
00502 void ins_sort_uint32_array(uint32_array_t* const array);
00503 
00516 void qsort_uint32_array(uint32_array_t* const array);
00517 
00538 uint32_t index_in_uint32_array(uint32_t to_find,
00539                                const uint32_array_t* const array);
00540 
00556 inline static bool
00557 is_in_uint32_array(uint32_t to_find, const uint32_array_t* const array) {
00558     return (NOT_IN_ARRAY != index_in_uint32_array(to_find, array));
00559 }
00560 
00582 uint32_t index_in_sorted_uint32_array(uint32_t to_find,
00583                                       const uint32_array_t* const sorted_array,
00584                                       uint32_t min_index, uint32_t max_index);
00585 
00597 inline static bool
00598 is_in_sorted_uint32_array(uint32_t to_find, const uint32_array_t* const array) {
00599     return (NOT_IN_ARRAY != index_in_sorted_uint32_array(
00600                                 to_find,
00601                                 array,
00602                                 0,
00603                                 array->length - 1
00604                             )
00605             );
00606 }
00607 
00608 
00609 
00610 
00611 
00612 
00613 
00621 struct struct_int32_array_t {
00627     uint32_t alloced;
00632     uint32_t length;
00636     int32_t* data;
00637 };
00638 
00643 typedef struct struct_int32_array_t int32_array_t;
00644 
00656 int32_array_t* alloc_int32_array(uint32_t length);
00657 
00670 void free_int32_array(int32_array_t* array);
00671 
00683 #define reset_int32_array(ARRAY) do {(ARRAY)->length = 0;} while (0)
00684 
00697 void resize_int32_array(int32_array_t* const array, uint32_t alloced);
00698 
00710 void append_int32_to_array(int32_array_t* array, const int32_t to_append);
00711 
00724 void append_int32_array(int32_array_t* const array,
00725                         const int32_array_t* const to_append);
00726 
00739 void swap_int32_array(int32_array_t* const a, int32_array_t* const b);
00740 
00752 void print_int32_array(const int32_array_t* const array);
00753 
00773 uint32_t index_in_int32_array(int32_t to_find,
00774                               const int32_array_t* const array);
00775 
00791 inline static bool
00792 is_in_int32_array(int32_t to_find, const int32_array_t* const array) {
00793     return (NOT_IN_ARRAY != index_in_int32_array(to_find, array));
00794 }
00795 
00817 uint32_t index_in_sorted_int32_array(int32_t to_find,
00818                                      const int32_array_t* const sorted_array,
00819                                      uint32_t min_index, uint32_t max_index);
00820 
00832 inline static bool
00833 is_in_sorted_int32_array(int32_t to_find, const int32_array_t* const array) {
00834     return (NOT_IN_ARRAY != index_in_sorted_int32_array(
00835                                 to_find,
00836                                 array,
00837                                 0,
00838                                 array->length - 1
00839                             )
00840             );
00841 }
00842 
00843 
00844 
00845 
00846 
00847 
00848 
00857 struct struct_mpz_array_t {
00863     uint32_t alloced;
00874     uint32_t length;
00879     mpz_t* data;
00880 };
00885 typedef struct struct_mpz_array_t mpz_array_t;
00886 
00904 mpz_array_t* alloc_mpz_array(uint32_t length);
00905 
00918 void free_mpz_array(mpz_array_t* array);
00919 
00934 #define reset_mpz_array(ARRAY) do {(ARRAY)->length = 0;} while (0)
00935 
00948 void resize_mpz_array(mpz_array_t* const array, uint32_t alloced);
00949 
00962 void swap_mpz_array(mpz_array_t* const a, mpz_array_t* const b);
00963 
00975 void append_mpz_to_array(mpz_array_t* array, const mpz_t to_append);
00976 
00989 void append_mpz_array(mpz_array_t* const array,
00990                       const mpz_array_t* const to_append);
00991 
01003 void print_mpz_array(const mpz_array_t* const array);
01004 
01021 uint32_t index_in_mpz_array(const mpz_t to_find,
01022                             const mpz_array_t* const array);
01042 uint32_t index_in_sorted_mpz_array(const mpz_t to_find,
01043                                    const mpz_array_t* const sorted_array,
01044                                    uint32_t min_index, uint32_t max_index);
01045 
01061 inline static bool
01062 is_in_mpz_array(const mpz_t to_find, const mpz_array_t* const array) {
01063     return (NOT_IN_ARRAY != index_in_mpz_array(to_find, array));
01064 }
01065 
01074 void ins_sort_mpz_array(mpz_array_t* const array);
01075 
01088 void qsort_mpz_array(mpz_array_t* const array);
01089 
01101 inline static bool
01102 is_in_sorted_mpz_array(const mpz_t to_find, const mpz_array_t* const array) {
01103     return (NOT_IN_ARRAY != index_in_sorted_mpz_array(
01104                                 to_find,
01105                                 array,
01106                                 0,
01107                                 array->length - 1
01108                             )
01109            );
01110 }
01111 
01112 
01113 
01114 
01115 
01116 
01117 
01127 struct struct_binary_array_t {
01137    uint32_t alloced;
01142    uint32_t length;
01147    TIFA_BITSTRING_T* data;
01148 };
01153 typedef struct struct_binary_array_t binary_array_t;
01154 
01170 binary_array_t* alloc_binary_array(uint32_t length);
01171 
01184 void free_binary_array(binary_array_t* array);
01185 
01197 #define reset_binary_array(ARRAY) do {(ARRAY)->length = 0;} while (0)
01198 
01211 void resize_binary_array(binary_array_t* const array, uint32_t alloced);
01212 
01226 void append_bit_to_array(binary_array_t* array, const unsigned int to_append);
01227 
01235 void print_binary_array(const binary_array_t* const array);
01236 
01247 inline static uint8_t
01248 get_array_bit(uint32_t index, const binary_array_t* const array) {
01249 #if BITSTRING_T_SIZE_IS_POW_OF_TWO
01250     uint32_t offset = index & (BITSTRING_T_BITSIZE - 1);
01251     index >>= POW_TWO_BITSTRING_T_SIZE;
01252 #else
01253     uint32_t offset = index % BITSTRING_T_BITSIZE;
01254     index /= BITSTRING_T_BITSIZE;
01255 #endif
01256     offset = BITSTRING_T_BITSIZE - 1 - offset;
01257     if (0 == ((((TIFA_BITSTRING_T)1)<<offset) & array->data[index])) {
01258         return 0;
01259     } else {
01260         return 1;
01261     }
01262 }
01263 
01273 inline static void
01274 set_array_bit_to_one(uint32_t index, binary_array_t* const array) {
01275 #if BITSTRING_T_SIZE_IS_POW_OF_TWO
01276     uint32_t offset = index & (BITSTRING_T_BITSIZE - 1);
01277     index >>= POW_TWO_BITSTRING_T_SIZE;
01278 #else
01279     uint32_t offset = index % BITSTRING_T_BITSIZE;
01280     index /= BITSTRING_T_BITSIZE;
01281 #endif
01282     offset = BITSTRING_T_BITSIZE - 1 - offset;
01283     array->data[index] |= (((TIFA_BITSTRING_T)1)<<offset);
01284 }
01285 
01295 inline static void
01296 set_array_bit_to_zero(uint32_t index, binary_array_t* const array) {
01297 #if BITSTRING_T_SIZE_IS_POW_OF_TWO
01298     uint32_t offset = index & (BITSTRING_T_BITSIZE - 1);
01299     index >>= POW_TWO_BITSTRING_T_SIZE;
01300 #else
01301     uint32_t offset = index % BITSTRING_T_BITSIZE;
01302     index /= BITSTRING_T_BITSIZE;
01303 #endif
01304     offset = BITSTRING_T_BITSIZE - 1 - offset;
01305     array->data[index] &= !(((TIFA_BITSTRING_T)1)<<offset);
01306 }
01307 
01317 inline static void
01318 flip_array_bit(uint32_t index, binary_array_t* const array) {
01319 #if BITSTRING_T_SIZE_IS_POW_OF_TWO
01320     uint32_t offset = index & (BITSTRING_T_BITSIZE - 1);
01321     index >>= POW_TWO_BITSTRING_T_SIZE;
01322 #else
01323     uint32_t offset = index % BITSTRING_T_BITSIZE;
01324     index /= BITSTRING_T_BITSIZE;
01325 #endif
01326     offset = BITSTRING_T_BITSIZE - 1 - offset;
01327     array->data[index] ^= (((TIFA_BITSTRING_T)1)<<offset);
01328 }
01329 
01330 #ifdef __cplusplus
01331 }
01332 #endif
01333 
01334 #endif