EFL 1.7 svn doobies
[profile/ivi/eina.git] / src / tests / Ecore_Data.h
1 #ifndef _ECORE_DATA_H
2 # define _ECORE_DATA_H
3
4 #include <stdio.h>
5 /* we need this for size_t */
6 #include <stddef.h>
7
8 #ifdef EAPI
9 # undef EAPI
10 #endif
11
12 #ifdef _WIN32
13 # ifdef EFL_ECORE_BUILD
14 #  ifdef DLL_EXPORT
15 #   define EAPI __declspec(dllexport)
16 #  else
17 #   define EAPI
18 #  endif /* ! DLL_EXPORT */
19 # else
20 #  define EAPI __declspec(dllimport)
21 # endif /* ! EFL_ECORE_BUILD */
22 #else
23 # ifdef __GNUC__
24 #  if __GNUC__ >= 4
25 #   define EAPI __attribute__ ((visibility("default")))
26 #  else
27 #   define EAPI
28 #  endif
29 # else
30 #  define EAPI
31 # endif
32 #endif /* ! _WIN32 */
33
34 /**
35  * @file Ecore_Data.h
36  * @brief Contains threading, list, hash, debugging and tree functions.
37  */
38
39 # ifdef __cplusplus
40 extern "C" {
41 # endif
42
43
44 #ifndef TRUE
45 # define TRUE 1
46 #endif
47
48 #ifndef FALSE
49 # define FALSE 0
50 #endif
51
52 #ifdef FREE
53 # undef FREE
54 #endif
55 #define FREE(ptr) free(ptr); ptr = NULL;
56
57 #ifdef IF_FREE
58 # undef IF_FREE
59 #endif
60 #define IF_FREE(ptr) if (ptr) {free(ptr); } ptr = NULL;
61
62 /* convenience macros for checking pointer parameters for non-NULL */
63 #undef CHECK_PARAM_POINTER_RETURN
64 #define CHECK_PARAM_POINTER_RETURN(sparam, param, ret) \
65    if (!(param)) \
66      { \
67         printf("***** Developer Warning ***** :\n" \
68                "\tThis program is calling:\n\n" \
69                "\t%s();\n\n" \
70                "\tWith the parameter:\n\n" \
71                "\t%s\n\n" \
72                "\tbeing NULL. Please fix your program.", __FUNCTION__, sparam); \
73         if (getenv("ECORE_ERROR_ABORT")) { abort(); } \
74         return ret; \
75      }
76
77 #undef CHECK_PARAM_POINTER
78 #define CHECK_PARAM_POINTER(sparam, param) \
79    if (!(param)) \
80      { \
81         printf("***** Developer Warning ***** :\n" \
82                "\tThis program is calling:\n\n" \
83                "\t%s();\n\n" \
84                "\tWith the parameter:\n\n" \
85                "\t%s\n\n" \
86                "\tbeing NULL. Please fix your program.", __FUNCTION__, sparam); \
87         if (getenv("ECORE_ERROR_ABORT")) { abort(); } \
88         return; \
89      }
90
91
92 # ifdef __sgi
93 #  define __FUNCTION__ "unknown"
94 #  ifndef __cplusplus
95 #   define inline
96 #  endif
97 # endif
98
99 # define ECORE_SORT_MIN 0
100 # define ECORE_SORT_MAX 1
101
102 typedef void (*Ecore_For_Each)(void *value, void *user_data);
103 # define ECORE_FOR_EACH(function) ((Ecore_For_Each)function)
104
105 typedef void (*Ecore_Free_Cb)(void *data);
106 # define ECORE_FREE_CB(func) ((Ecore_Free_Cb)func)
107
108 typedef unsigned int (*Ecore_Hash_Cb)(const void *key);
109 # define ECORE_HASH_CB(function) ((Ecore_Hash_Cb)function)
110
111 typedef int (*Ecore_Compare_Cb)(const void *data1, const void *data2);
112 # define ECORE_COMPARE_CB(function) ((Ecore_Compare_Cb)function)
113
114 typedef struct _ecore_list Ecore_List;
115 # define ECORE_LIST(list) ((Ecore_List *)list)
116
117 typedef struct _ecore_list_node Ecore_List_Node;
118 # define ECORE_LIST_NODE(node) ((Ecore_List_Node *)node)
119
120 typedef struct _ecore_strbuf Ecore_Strbuf;
121 # define ECORE_STRBUF(buf) ((Ecore_Strbuf *)buf)
122
123 struct _ecore_list_node
124 {
125    void *data;
126    struct _ecore_list_node *next;
127 };
128
129 struct _ecore_list
130 {
131    Ecore_List_Node *first; /* The first node in the list */
132    Ecore_List_Node *last; /* The last node in the list */
133    Ecore_List_Node *current; /* The current node in the list */
134
135    Ecore_Free_Cb free_func; /* The callback to free data in nodes */
136
137    int nodes; /* The number of nodes in the list */
138    int index; /* The position from the front of the
139                  list of current node */
140 };
141
142 EAPI int              ecore_direct_compare(const void *key1, const void *key2);
143 EAPI int              ecore_str_compare(const void *key1, const void *key2);
144
145 EAPI unsigned int     ecore_direct_hash(const void *key);
146 EAPI unsigned int     ecore_str_hash(const void *key);
147
148 /* Creating and initializing new list structures */
149 EAPI Ecore_List *     ecore_list_new(void);
150 EAPI int              ecore_list_init(Ecore_List *list);
151
152 /* Adding items to the list */
153 EAPI int              ecore_list_append(Ecore_List *list, void *_data);
154 EAPI int              ecore_list_prepend(Ecore_List *list, void *_data);
155 EAPI int              ecore_list_insert(Ecore_List *list, void *_data);
156 EAPI int              ecore_list_append_list(Ecore_List *list,
157                                              Ecore_List *append);
158 EAPI int              ecore_list_prepend_list(Ecore_List *list,
159                                               Ecore_List *prepend);
160
161 /* Removing items from the list */
162 EAPI int              ecore_list_remove_destroy(Ecore_List *list);
163 EAPI void *           ecore_list_remove(Ecore_List *list);
164 EAPI void *           ecore_list_first_remove(Ecore_List *list);
165 EAPI void *           ecore_list_last_remove(Ecore_List *list);
166
167 /* Retrieve the current position in the list */
168 EAPI void *           ecore_list_current(Ecore_List *list);
169 EAPI void *           ecore_list_first(Ecore_List *list);
170 EAPI void *           ecore_list_last(Ecore_List *list);
171 EAPI int              ecore_list_index(Ecore_List *list);
172 EAPI int              ecore_list_count(Ecore_List *list);
173
174 /* Traversing the list */
175 EAPI int              ecore_list_for_each(Ecore_List *list,
176                                           Ecore_For_Each function,
177                                           void *user_data);
178 EAPI void *           ecore_list_first_goto(Ecore_List *list);
179 EAPI void *           ecore_list_last_goto(Ecore_List *list);
180 EAPI void *           ecore_list_index_goto(Ecore_List *list, int index);
181 EAPI void *           ecore_list_goto(Ecore_List *list, const void *_data);
182
183 /* Traversing the list and returning data */
184 EAPI void *           ecore_list_next(Ecore_List *list);
185 EAPI void *           ecore_list_find(Ecore_List *list,
186                                       Ecore_Compare_Cb function,
187                                       const void *user_data);
188
189 /* Sorting the list */
190 EAPI int              ecore_list_sort(Ecore_List *list,
191                                       Ecore_Compare_Cb compare,
192                                       char order);
193 EAPI int              ecore_list_mergesort(Ecore_List *list,
194                                            Ecore_Compare_Cb compare,
195                                            char order);
196 EAPI int              ecore_list_heapsort(Ecore_List *list,
197                                           Ecore_Compare_Cb compare,
198                                           char order);
199 EAPI void             ecore_list_merge(Ecore_List *list, Ecore_List *l2,
200                                        Ecore_Compare_Cb, char order);
201
202 /* Check to see if there is any data in the list */
203 EAPI int              ecore_list_empty_is(Ecore_List *list);
204
205 /* Remove every node in the list without freeing the list itself */
206 EAPI int              ecore_list_clear(Ecore_List *list);
207 /* Free the list and it's contents */
208 EAPI void             ecore_list_destroy(Ecore_List *list);
209
210 /* Creating and initializing list nodes */
211 EAPI Ecore_List_Node *ecore_list_node_new(void);
212 EAPI int              ecore_list_node_init(Ecore_List_Node *newNode);
213
214 /* Destroying nodes */
215 EAPI int              ecore_list_node_destroy(Ecore_List_Node *_e_node,
216                                               Ecore_Free_Cb free_func);
217
218 EAPI int              ecore_list_free_cb_set(Ecore_List *list,
219                                              Ecore_Free_Cb free_func);
220
221 typedef Ecore_List Ecore_DList;
222 # define ECORE_DLIST(dlist) ((Ecore_DList *)dlist)
223
224 typedef struct _ecore_dlist_node Ecore_DList_Node;
225 # define ECORE_DLIST_NODE(dlist) ((Ecore_DList_Node *)dlist)
226
227 struct _ecore_dlist_node
228 {
229    Ecore_List_Node single;
230    Ecore_DList_Node *previous;
231 };
232
233 /* Creating and initializing new list structures */
234 EAPI Ecore_DList *ecore_dlist_new(void);
235 EAPI int          ecore_dlist_init(Ecore_DList *list);
236 EAPI void         ecore_dlist_destroy(Ecore_DList *list);
237
238 /* Adding items to the list */
239 EAPI int          ecore_dlist_append(Ecore_DList *_e_dlist, void *_data);
240 EAPI int          ecore_dlist_prepend(Ecore_DList *_e_dlist, void *_data);
241 EAPI int          ecore_dlist_insert(Ecore_DList *_e_dlist, void *_data);
242 EAPI int          ecore_dlist_append_list(Ecore_DList *_e_dlist,
243                                           Ecore_DList *append);
244 EAPI int          ecore_dlist_prepend_list(Ecore_DList *_e_dlist,
245                                            Ecore_DList *prepend);
246
247 /* Info about list's state */
248 # define ecore_dlist_first(list) ecore_list_first(list)
249 # define ecore_dlist_last(list) ecore_list_last(list)
250 EAPI void *            ecore_dlist_current(Ecore_DList *list);
251 EAPI int               ecore_dlist_index(Ecore_DList *list);
252 # define ecore_dlist_count(list) ecore_list_count(list)
253
254 /* Removing items from the list */
255 EAPI void *            ecore_dlist_remove(Ecore_DList *_e_dlist);
256 EAPI void *            ecore_dlist_first_remove(Ecore_DList *_e_dlist);
257 EAPI int               ecore_dlist_remove_destroy(Ecore_DList *list);
258 EAPI void *            ecore_dlist_last_remove(Ecore_DList *_e_dlist);
259
260 /* Traversing the list */
261 # define ecore_dlist_for_each(list, function, user_data) \
262    ecore_list_for_each(list, function, user_data)
263 EAPI void *            ecore_dlist_first_goto(Ecore_DList *_e_dlist);
264 EAPI void *            ecore_dlist_last_goto(Ecore_DList *_e_dlist);
265 EAPI void *            ecore_dlist_index_goto(Ecore_DList *_e_dlist, int index);
266 EAPI void *            ecore_dlist_goto(Ecore_DList *_e_dlist, void *_data);
267
268 /* Traversing the list and returning data */
269 EAPI void *            ecore_dlist_next(Ecore_DList *list);
270 EAPI void *            ecore_dlist_previous(Ecore_DList *list);
271
272 /* Sorting the list */
273 EAPI int               ecore_dlist_sort(Ecore_DList *list,
274                                         Ecore_Compare_Cb compare,
275                                         char order);
276 EAPI int               ecore_dlist_mergesort(Ecore_DList *list,
277                                              Ecore_Compare_Cb compare,
278                                              char order);
279 # define ecore_dlist_heapsort(list, compare, order) \
280    ecore_list_heapsort(list, compare, order)
281 EAPI void              ecore_dlist_merge(Ecore_DList *list, Ecore_DList *l2,
282                                          Ecore_Compare_Cb, char order);
283
284 /* Check to see if there is any data in the list */
285 EAPI int               ecore_dlist_empty_is(Ecore_DList *_e_dlist);
286
287 /* Remove every node in the list without free'ing it */
288 EAPI int               ecore_dlist_clear(Ecore_DList *_e_dlist);
289
290 /* Creating and initializing list nodes */
291 EAPI int               ecore_dlist_node_init(Ecore_DList_Node *node);
292 EAPI Ecore_DList_Node *ecore_dlist_node_new(void);
293
294 /* Destroying nodes */
295 EAPI int               ecore_dlist_node_destroy(Ecore_DList_Node *node,
296                                                 Ecore_Free_Cb free_func);
297
298 EAPI int               ecore_dlist_free_cb_set(Ecore_DList *dlist,
299                                                Ecore_Free_Cb free_func);
300
301
302
303 /*
304  * Hash Table Implementation:
305  *
306  * Traditional hash table implementation. I had tried a list of tables
307  * approach to save on the realloc's but it ended up being much slower than
308  * the traditional approach.
309  */
310
311 typedef struct _ecore_hash_node Ecore_Hash_Node;
312 # define ECORE_HASH_NODE(hash) ((Ecore_Hash_Node *)hash)
313
314 struct _ecore_hash_node
315 {
316    Ecore_Hash_Node *next; /* Pointer to the next node in the bucket list */
317    void *key; /* The key for the data node */
318    void *value; /* The value associated with this node */
319 };
320
321 typedef struct _ecore_hash Ecore_Hash;
322 # define ECORE_HASH(hash) ((Ecore_Hash *)hash)
323
324 struct _ecore_hash
325 {
326    Ecore_Hash_Node **buckets;
327    int size; /* An index into the table of primes to
328                 determine size */
329    int nodes; /* The number of nodes currently in the hash */
330
331    int index; /* The current index into the bucket table */
332
333    Ecore_Compare_Cb compare; /* The function used to compare node values */
334    Ecore_Hash_Cb hash_func; /* The callback function to determine hash */
335
336    Ecore_Free_Cb free_key; /* The callback function to free key */
337    Ecore_Free_Cb free_value; /* The callback function to free value */
338 };
339
340 /* Create and initialize a hash */
341 EAPI Ecore_Hash *ecore_hash_new(Ecore_Hash_Cb hash_func,
342                                 Ecore_Compare_Cb compare);
343 EAPI int         ecore_hash_init(Ecore_Hash *hash,
344                                  Ecore_Hash_Cb hash_func,
345                                  Ecore_Compare_Cb compare);
346
347 /* Functions related to freeing the data in the hash table */
348 EAPI int         ecore_hash_free_key_cb_set(Ecore_Hash *hash,
349                                             Ecore_Free_Cb function);
350 EAPI int         ecore_hash_free_value_cb_set(Ecore_Hash *hash,
351                                               Ecore_Free_Cb function);
352 EAPI void        ecore_hash_destroy(Ecore_Hash *hash);
353
354 EAPI int         ecore_hash_count(Ecore_Hash *hash);
355 EAPI int         ecore_hash_for_each_node(Ecore_Hash *hash,
356                                           Ecore_For_Each for_each_func,
357                                           void *user_data);
358 EAPI Ecore_List *ecore_hash_keys(Ecore_Hash *hash);
359
360 /* Retrieve and store data into the hash */
361 EAPI void *      ecore_hash_get(Ecore_Hash *hash, const void *key);
362 EAPI int         ecore_hash_set(Ecore_Hash *hash, void *key, void *value);
363 EAPI int         ecore_hash_hash_set(Ecore_Hash *hash, Ecore_Hash *set);
364 EAPI void *      ecore_hash_remove(Ecore_Hash *hash, const void *key);
365 EAPI void *      ecore_hash_find(Ecore_Hash *hash,
366                                  Ecore_Compare_Cb compare,
367                                  const void *value);
368 EAPI void        ecore_hash_dump_graph(Ecore_Hash *hash);
369 EAPI void        ecore_hash_dump_stats(Ecore_Hash *hash);
370
371
372 typedef struct _ecore_heap Ecore_Sheap;
373 # define ECORE_HEAP(heap) ((Ecore_Sheap *)heap)
374
375 struct _ecore_heap
376 {
377    void **data;
378    int size;
379    int space;
380
381    char order, sorted;
382
383    /* Callback for comparing node values, default is direct comparison */
384    Ecore_Compare_Cb compare;
385
386    /* Callback for freeing node data, default is NULL */
387    Ecore_Free_Cb free_func;
388 };
389
390 EAPI Ecore_Sheap *ecore_sheap_new(Ecore_Compare_Cb compare, int size);
391 EAPI void         ecore_sheap_destroy(Ecore_Sheap *heap);
392 EAPI int          ecore_sheap_init(Ecore_Sheap *heap,
393                                    Ecore_Compare_Cb compare,
394                                    int size);
395 EAPI int          ecore_sheap_free_cb_set(Ecore_Sheap *heap,
396                                           Ecore_Free_Cb free_func);
397 EAPI int          ecore_sheap_insert(Ecore_Sheap *heap, void *data);
398 EAPI void *       ecore_sheap_extract(Ecore_Sheap *heap);
399 EAPI void *       ecore_sheap_extreme(Ecore_Sheap *heap);
400 EAPI int          ecore_sheap_change(Ecore_Sheap *heap,
401                                      void *item,
402                                      void *newval);
403 EAPI int          ecore_sheap_compare_set(Ecore_Sheap *heap,
404                                           Ecore_Compare_Cb compare);
405 EAPI void         ecore_sheap_order_set(Ecore_Sheap *heap, char order);
406 EAPI void         ecore_sheap_sort(Ecore_Sheap *heap);
407
408 EAPI void *       ecore_sheap_item(Ecore_Sheap *heap, int i);
409
410
411 typedef struct _ecore_string Ecore_String;
412 struct _ecore_string
413 {
414    char *string;
415    int references;
416 };
417
418 EAPI int         ecore_string_init();
419 EAPI void        ecore_string_shutdown();
420 EAPI const char *ecore_string_instance(const char *string);
421 EAPI void        ecore_string_release(const char *string);
422
423 typedef struct _Ecore_Tree_Node Ecore_Tree_Node;
424 # define ECORE_TREE_NODE(object) ((Ecore_Tree_Node *)object)
425 struct _Ecore_Tree_Node
426 {
427
428    /* The actual data for each node */
429    void *key;
430    void *value;
431
432    /* Pointers to surrounding nodes */
433    Ecore_Tree_Node *parent;
434    Ecore_Tree_Node *left_child;
435    Ecore_Tree_Node *right_child;
436
437    /* Book keeping information for quicker balancing of the tree */
438    int max_right;
439    int max_left;
440 };
441
442 typedef struct _Ecore_Tree Ecore_Tree;
443 # define ECORE_TREE(object) ((Ecore_Tree *)object)
444 struct _Ecore_Tree
445 {
446    /* Nodes of the tree */
447    Ecore_Tree_Node *tree;
448
449    /* Callback for comparing node values, default is direct comparison */
450    Ecore_Compare_Cb compare_func;
451
452    /* Callback for freeing node data, default is NULL */
453    Ecore_Free_Cb free_value;
454    /* Callback for freeing node key, default is NULL */
455    Ecore_Free_Cb free_key;
456 };
457
458 /* Some basic tree functions */
459 /* Allocate and initialize a new tree */
460 EAPI Ecore_Tree *     ecore_tree_new(Ecore_Compare_Cb compare_func);
461 /* Initialize a new tree */
462 EAPI int              ecore_tree_init(Ecore_Tree *tree,
463                                       Ecore_Compare_Cb compare_func);
464
465 /* Free the tree */
466 EAPI int              ecore_tree_destroy(Ecore_Tree *tree);
467 /* Check to see if the tree has any nodes in it */
468 EAPI int              ecore_tree_empty_is(Ecore_Tree *tree);
469
470 /* Retrieve the value associated with key */
471 EAPI void *           ecore_tree_get(Ecore_Tree *tree, const void *key);
472 EAPI Ecore_Tree_Node *ecore_tree_get_node(Ecore_Tree *tree, const void *key);
473 /* Retrieve the value of node with key greater than or equal to key */
474 EAPI void *           ecore_tree_closest_larger_get(Ecore_Tree *tree,
475                                                     const void *key);
476 /* Retrieve the value of node with key less than or equal to key */
477 EAPI void *           ecore_tree_closest_smaller_get(Ecore_Tree *tree,
478                                                      const void *key);
479
480 /* Set the value associated with key to value */
481 EAPI int              ecore_tree_set(Ecore_Tree *tree, void *key, void *value);
482 /* Remove the key from the tree */
483 EAPI int              ecore_tree_remove(Ecore_Tree *tree, const void *key);
484
485 /* Add a node to the tree */
486 EAPI int              ecore_tree_node_add(Ecore_Tree *tree,
487                                           Ecore_Tree_Node *node);
488 /* Remove a node from the tree */
489 EAPI int              ecore_tree_node_remove(Ecore_Tree *tree,
490                                              Ecore_Tree_Node *node);
491
492 /* For each node in the tree perform the for_each_func function */
493 /* For this one pass in the node */
494 EAPI int              ecore_tree_for_each_node(Ecore_Tree *tree,
495                                                Ecore_For_Each for_each_func,
496                                                void *user_data);
497 /* And here pass in the node's value */
498 EAPI int              ecore_tree_for_each_node_value(
499    Ecore_Tree *tree,
500    Ecore_For_Each
501    for_each_func,
502    void *user_data);
503
504 /* Some basic node functions */
505 /* Initialize a node */
506 EAPI int              ecore_tree_node_init(Ecore_Tree_Node *new_node);
507 /* Allocate and initialize a new node */
508 EAPI Ecore_Tree_Node *ecore_tree_node_new(void);
509 /* Free the desired node */
510 EAPI int              ecore_tree_node_destroy(Ecore_Tree_Node *node,
511                                               Ecore_Free_Cb free_value,
512                                               Ecore_Free_Cb free_key);
513
514 /* Set the node's key to key */
515 EAPI int              ecore_tree_node_key_set(Ecore_Tree_Node *node, void *key);
516 /* Retrieve the key in node */
517 EAPI void *           ecore_tree_node_key_get(Ecore_Tree_Node *node);
518
519 /* Set the node's value to value */
520 EAPI int              ecore_tree_node_value_set(Ecore_Tree_Node *node,
521                                                 void *value);
522 /* Retrieve the value in node */
523 EAPI void *           ecore_tree_node_value_get(Ecore_Tree_Node *node);
524
525 /* Add a function to free the data stored in nodes */
526 EAPI int              ecore_tree_free_value_cb_set(Ecore_Tree *tree,
527                                                    Ecore_Free_Cb free_value);
528 /* Add a function to free the keys stored in nodes */
529 EAPI int              ecore_tree_free_key_cb_set(Ecore_Tree *tree,
530                                                  Ecore_Free_Cb free_key);
531
532
533 EAPI Ecore_Strbuf *   ecore_strbuf_new(void);
534 EAPI void             ecore_strbuf_free(Ecore_Strbuf *buf);
535 EAPI void             ecore_strbuf_append(Ecore_Strbuf *buf, const char *str);
536 EAPI void             ecore_strbuf_append_char(Ecore_Strbuf *buf, char c);
537 EAPI void             ecore_strbuf_insert(Ecore_Strbuf *buf, const char *str,
538                                           size_t pos);
539 # define ecore_strbuf_prepend(buf, str) ecore_strbuf_insert(buf, str, 0)
540 EAPI const char *     ecore_strbuf_string_get(Ecore_Strbuf *buf);
541 EAPI size_t           ecore_strbuf_length_get(Ecore_Strbuf *buf);
542 EAPI int              ecore_strbuf_replace(Ecore_Strbuf *buf, const char *str,
543                                            const char *with, unsigned int n);
544 # define ecore_strbuf_replace_first(buf, str, with) \
545    ecore_strbuf_replace(buf, str, with, 1)
546 EAPI int              ecore_strbuf_replace_all(Ecore_Strbuf *buf,
547                                                const char *str,
548                                                const char *with);
549
550 extern int            ecore_str_compare(const void *key1, const void *key2);
551 extern int            ecore_direct_compare(const void *key1, const void *key2);
552 extern unsigned int   ecore_str_hash(const void *key);
553
554 #ifdef __cplusplus
555 }
556 #endif
557 #endif /* _ECORE_DATA_H */