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