configure.ac: Add checks for Flex/Yacc/Bison and other furry animals, for the new...
[platform/upstream/gst-plugins-good.git] / gst / goom / goomsl_hash.c
1 #include "goomsl_hash.h"
2 #include <string.h>
3 #include <stdlib.h>
4
5 static GoomHashEntry *
6 entry_new (const char *key, HashValue value)
7 {
8
9   int len = strlen (key);
10   GoomHashEntry *entry = (GoomHashEntry *) malloc (sizeof (GoomHashEntry));
11
12   entry->key = (char *) malloc (len + 1);
13   memcpy (entry->key, key, len + 1);
14   entry->value = value;
15   entry->lower = NULL;
16   entry->upper = NULL;
17
18   return entry;
19 }
20
21 static void
22 entry_free (GoomHashEntry * entry)
23 {
24   if (entry != NULL) {
25     entry_free (entry->lower);
26     entry_free (entry->upper);
27     free (entry->key);
28     free (entry);
29   }
30 }
31
32 static void
33 entry_put (GoomHashEntry * entry, const char *key, HashValue value)
34 {
35   int cmp = strcmp (key, entry->key);
36
37   if (cmp == 0) {
38     entry->value = value;
39   } else if (cmp > 0) {
40     if (entry->upper == NULL)
41       entry->upper = entry_new (key, value);
42     else
43       entry_put (entry->upper, key, value);
44   } else {
45     if (entry->lower == NULL)
46       entry->lower = entry_new (key, value);
47     else
48       entry_put (entry->lower, key, value);
49   }
50 }
51
52 static HashValue *
53 entry_get (GoomHashEntry * entry, const char *key)
54 {
55
56   int cmp;
57
58   if (entry == NULL)
59     return NULL;
60   cmp = strcmp (key, entry->key);
61   if (cmp > 0)
62     return entry_get (entry->upper, key);
63   else if (cmp < 0)
64     return entry_get (entry->lower, key);
65   else
66     return &(entry->value);
67 }
68
69 GoomHash *
70 goom_hash_new ()
71 {
72   GoomHash *_this = (GoomHash *) malloc (sizeof (GoomHash));
73
74   _this->root = NULL;
75   _this->number_of_puts = 0;
76   return _this;
77 }
78
79 void
80 goom_hash_free (GoomHash * _this)
81 {
82   entry_free (_this->root);
83   free (_this);
84 }
85
86 void
87 goom_hash_put (GoomHash * _this, const char *key, HashValue value)
88 {
89   _this->number_of_puts += 1;
90   if (_this->root == NULL)
91     _this->root = entry_new (key, value);
92   else
93     entry_put (_this->root, key, value);
94 }
95
96 HashValue *
97 goom_hash_get (GoomHash * _this, const char *key)
98 {
99   if (_this == NULL)
100     return NULL;
101   return entry_get (_this->root, key);
102 }
103
104 void
105 goom_hash_put_int (GoomHash * _this, const char *key, int i)
106 {
107   HashValue value;
108
109   value.i = i;
110   goom_hash_put (_this, key, value);
111 }
112
113 void
114 goom_hash_put_float (GoomHash * _this, const char *key, float f)
115 {
116   HashValue value;
117
118   value.f = f;
119   goom_hash_put (_this, key, value);
120 }
121
122 void
123 goom_hash_put_ptr (GoomHash * _this, const char *key, void *ptr)
124 {
125   HashValue value;
126
127   value.ptr = ptr;
128   goom_hash_put (_this, key, value);
129 }
130
131 /* FOR EACH */
132
133 static void
134 _goom_hash_for_each (GoomHash * _this, GoomHashEntry * entry, GH_Func func)
135 {
136   if (entry == NULL)
137     return;
138   func (_this, entry->key, &(entry->value));
139   _goom_hash_for_each (_this, entry->lower, func);
140   _goom_hash_for_each (_this, entry->upper, func);
141 }
142
143 void
144 goom_hash_for_each (GoomHash * _this, GH_Func func)
145 {
146   _goom_hash_for_each (_this, _this->root, func);
147 }
148
149 int
150 goom_hash_number_of_puts (GoomHash * _this)
151 {
152   return _this->number_of_puts;
153 }