hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
cf_vector.h
Go to the documentation of this file.
1 /*
2  * A general purpose vector
3  * Uses locks, so only moderately fast
4  * If you need to deal with sparse data, really sparse data,
5  * use a hash table. This assumes that packed data is a good idea.
6  * Does the fairly trivial realloc thing for extension,
7  * so
8  * And you can keep adding cool things to it
9  * Copywrite 2008 Brian Bulkowski
10  * All rights reserved
11  */
12 
13 #pragma once
14 
15 #include <pthread.h>
16 #include <stdint.h>
17 
19 
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 typedef struct cf_vector_s {
26  uint32_t value_len;
27  unsigned int flags;
28  unsigned int alloc_len; // number of elements currently allocated
29  unsigned int len; // number of elements in table, largest element set
30  uint8_t *vector;
33 #ifdef EXTERNAL_LOCKS
34  void *LOCK;
35 #else
36  pthread_mutex_t LOCK;
37 #endif // EXTERNAL_LOCKS
38 } cf_vector;
39 
40 
41 #define VECTOR_ELEM_SZ(_v) ( _h->value_len )
42 
43 #define VECTOR_FLAG_BIGLOCK 0x01 // support multithreaded access with a single big lock
44 #define VECTOR_FLAG_INITZERO 0x02 // internally init the vector objects to 0
45 #define VECTOR_FLAG_BIGRESIZE 0x04 // appends will be common - speculatively allocate extra memory
46 #define VECTOR_REDUCE_DELETE (1) // indicate that a delete should be done during the reduction
47 
48 
49 /*
50  * Create a vector with malloc for handing around
51  */
52 
53 cf_vector *
54 cf_vector_create(uint32_t value_len, uint32_t init_sz, unsigned int flags);
55 
56 /*
57 ** create a stack vector, but with an allocated internal-vector-bit
58 */
59 
60 int
61 cf_vector_init(cf_vector *v, uint32_t value_len, uint32_t init_sz, unsigned int flags);
62 
63 void
64 cf_vector_init_smalloc(cf_vector *v, uint32_t value_len, uint8_t *sbuf, int sbuf_sz, unsigned int flags);
65 
66 #define cf_vector_define(__x, __value_len, __flags) \
67  uint8_t cf_vector##__x[1024]; cf_vector __x; cf_vector_init_smalloc(&__x, __value_len, cf_vector##__x, sizeof(cf_vector##__x), __flags);
68 
69 void
70 cf_vector_clone_stack(cf_vector *source, cf_vector *target, uint8_t *target_buf);
71 
72 #define cf_vector_define_clone(__source, __target) \
73  uint8_t cf_vector##__target[__source.len]; cf_vector __target; cf_vector_clone_stack(&__source, &__target, cf_vector##__target);
74 
75 /* Place a value into the hash
76  * Value will be copied into the hash
77  */
78 extern int cf_vector_set(cf_vector *v, uint32_t index, void *value);
79 extern int cf_vector_get(cf_vector *v, uint32_t index, void *value);
80 // this is very dangerous if it's a multithreaded vector. Use _vlock if multithrad.
81 extern void * cf_vector_getp(cf_vector *v, uint32_t index);
82 extern void * cf_vector_getp_vlock(cf_vector *v, uint32_t index, pthread_mutex_t **vlock);
83 extern int cf_vector_append(cf_vector *v, void *value);
84 
85 #define cf_vector_reset( __v ) (__v)->len = 0; if ( (__v)->flags & VECTOR_FLAG_INITZERO) memset( (__v)->vector, 0, (__v)->alloc_len * (__v)->value_len);
86 
87 // Adds a an element to the end, only if it doesn't exist already
88 // uses a bit-by-bit compare, thus is O(N) against the current length
89 // of the vector
90 extern int cf_vector_append_unique(cf_vector *v, void *value);
91 
92 // Deletes an element by moving all the remaining elements down by one
93 extern int cf_vector_delete(cf_vector *v, uint32_t index);
94 // Delete a range in the vector. Inclusive. Thus:
95 // a vector with len 5, you could delete start=0, end=3, leaving one element at the beginning (slot 0)
96 // don't set start and end the same, that's a single element delete, use vector_delete instead
97 // (or change the code to support that!)
98 // returns -1 on bad ranges
99 extern int cf_vector_delete_range(cf_vector *v, uint32_t start_index, uint32_t end_index);
100 
101 // There may be more allocated than you need. Fix that.
102 //
103 extern void cf_vector_compact(cf_vector *v);
104 
105 
106 /*
107 ** Get the number of elements currently in the vector
108 */
109 static inline unsigned int cf_vector_size(cf_vector *v)
110 {
111  return(v->len);
112 }
113 
114 
115 /*
116  * Destroy the entire hash - all memory will be freed
117  */
118 extern void cf_vector_destroy(cf_vector *v);
119 
120 /*
121 ** nice wrapper functions
122 ** very common vector types are pointers, and integers
123 */
124 
125 static inline cf_vector *cf_vector_pointer_create(uint32_t init_sz, uint32_t flags)
126 {
127  return(cf_vector_create(sizeof(void *), init_sz, flags));
128 }
129 
130 static inline int cf_vector_pointer_init(cf_vector *v, uint32_t init_sz, uint32_t flags)
131 {
132  return(cf_vector_init(v, sizeof(void *), init_sz, flags));
133 }
134 
135 static inline int cf_vector_pointer_set(cf_vector *v, uint32_t index, void *p)
136 {
137  return(cf_vector_set(v, index, &p));
138 }
139 
140 static inline void * cf_vector_pointer_get(cf_vector *v, uint32_t index) {
141  void *p;
142  cf_vector_get(v, index, &p);
143  return(p);
144 }
145 
146 static inline int cf_vector_pointer_append(cf_vector *v, void *p)
147 {
148  return(cf_vector_append(v, &p));
149 }
150 
151 /*
152 ** integer vectors!
153 */
154 
155 static inline cf_vector *cf_vector_integer_create(uint32_t init_sz, uint32_t flags)
156 {
157  return(cf_vector_create(sizeof(int), init_sz, flags));
158 }
159 
160 static inline int cf_vector_integer_init(cf_vector *v, uint32_t init_sz, uint32_t flags)
161 {
162  return(cf_vector_init(v, sizeof(int), init_sz, flags));
163 }
164 
165 static inline int cf_vector_integer_set(cf_vector *v, uint32_t index, int i)
166 {
167  return(cf_vector_set(v, index, &i));
168 }
169 
170 static inline int cf_vector_integer_get(cf_vector *v, uint32_t index) {
171  int i;
172  cf_vector_get(v, index, &i);
173  return(i);
174 }
175 
176 static inline int cf_vector_integer_append(cf_vector *v, int i)
177 {
178  return(cf_vector_append(v, &i));
179 }
180 
181 #ifdef __cplusplus
182 } // end extern "C"
183 #endif
184 
185