aboutsummaryrefslogtreecommitdiff
path: root/3rdparty/dict/dict.c
blob: ecd8f05d61a05199662c2415f8121255772d742b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/* ******************************************** */
/* File Name: dict.c                            */
/* Author: (C) 2008-2009 - Juan Jos� Ponteprino */
/* Description: Dictionary API Headers          */
/* License: This code is licenced as LGPL       */
/* ******************************************** */

/* --------------------------------------------------------------------------- */

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

/* --------------------------------------------------------------------------- */

#ifndef __GNUC__
#define inline __inline
#endif

/* --------------------------------------------------------------------------- */

#include "dict.h"

/* --------------------------------------------------------------------------- */

static DICT_T * _DICT_table = NULL;
static DICT_T * _DICT_table_hashed[ DICT_HASH_MAX ] = { NULL } ;

/* --------------------------------------------------------------------------- */

static int make_hash8( char * key )
{
    int hash = 0, i;

    for ( i = 0; key[i]; i++ )
#ifdef USE_ALTERNATIVE_MODE
        hash += key[i] ^( hash << DICT_HASH_SHIFT8 );
#else
        hash ^= key[i] + ( hash << DICT_HASH_SHIFT8 ); /* Best dispersion */
#endif

    return ( hash & DICT_HASH_MASK8 );
}

/* --------------------------------------------------------------------------- */

static int make_hash16( char * key )
{
    int hash = 0, i;

    for ( i = 0; key[i]; i++ )
#ifdef USE_ALTERNATIVE_MODE
        hash += key[i] ^( hash << DICT_HASH_SHIFT16 );
#else
        hash ^= key[i] + ( hash << DICT_HASH_SHIFT16 ); /* Best dispersion */
#endif

    return ( hash & DICT_HASH_MASK16 );
}

/* --------------------------------------------------------------------------- */

#ifdef USE_HASH8
#define make_hash   make_hash8
#else
#define make_hash   make_hash16
#endif

/* --------------------------------------------------------------------------- */

#define ihash8(x)   ((x)&0xff)

/* --------------------------------------------------------------------------- */

void DICT_Init()
{
}

/* --------------------------------------------------------------------------- */

void DICT_Exit()
{
}

/* --------------------------------------------------------------------------- */

DICT_T * DICT_Create( char * id )
{
    DICT_T * dict = calloc( 1, sizeof( DICT_T ) );

    if ( dict )
    {
        int hash = make_hash( id );

        /* Fill struct */

        dict->id = strdup( id );
        dict->hash = hash;

        /* Insert dict in table */

        dict->prev = NULL;
        dict->next = _DICT_table;
        if ( _DICT_table ) _DICT_table->prev = dict;
        _DICT_table = dict;

        /* Insert dict in hashed table */

        dict->prev_hashed = NULL;
        dict->next_hashed = _DICT_table_hashed[hash];
        if ( _DICT_table_hashed[ hash ] ) _DICT_table_hashed[ hash ]->prev_hashed = dict;
        _DICT_table_hashed[ hash ] = dict ;

        /* Cleanup entries structs */

        dict->entries = NULL;
        memset( dict->entries_hashed, '\0', sizeof( dict->entries_hashed ) );
    }

    return ( dict );
}

/* --------------------------------------------------------------------------- */

int DICT_Destroy( DICT_T * dict )
{
    if ( dict )
    {
        /* Remove dict from table */

        if ( dict->next ) dict->next->prev = dict->prev ;
        if ( dict->prev ) dict->prev->next = dict->next ;
        else _DICT_table = dict->next ; /* No prev, then this is first _DICT_table element */

        /* Remove dict from hashed table */

        if ( dict->next_hashed ) dict->next_hashed->prev_hashed = dict->prev_hashed ;
        if ( dict->prev_hashed ) dict->prev_hashed->next_hashed = dict->next_hashed ;
        else _DICT_table_hashed[ dict->hash ] = dict->next_hashed ; /* No prev, then this is first _DICT_table_hashed element */

        /* Destroy entries */

        DICT_DelAllEntries( dict );

        /* Free data  */

        free( dict->id );

        free( dict );
    }

    return ( 0 );
}

/* --------------------------------------------------------------------------- */

static inline DICT_ENTRY_T * __DICT_FINDENTRY( DICT_T * dict, char * key, int hash )
{
    DICT_ENTRY_T * entry = dict->entries_hashed[ hash ] ;

    while ( entry )
    {
        if ( !strcmp( entry->key, key ) ) return ( entry );
        entry = entry->next_hashed;
    }

    return ( NULL );
}

/* --------------------------------------------------------------------------- */

#define __DICT_SET_DATA \
    if ( flags & DICT_ENTRY_FLAG_DONT_ALLOC ) \
    { \
        entry->val = value; \
    } \
    else \
    { \
        entry->val = malloc( len ); \
        memmove( entry->val, value, len ); \
    } \
    \
    entry->flags = flags; \
    entry->len = len;

/* --------------------------------------------------------------------------- */

int DICT_AddEntry( DICT_T * dict, char * key, void * value, int len, int flags )
{
    DICT_ENTRY_T * entry;

    if ( dict && key )
    {
        int hash = make_hash( key );

        if ( ( entry = __DICT_FINDENTRY( dict, key, hash ) ) )
        {
            if ( !( entry->flags & DICT_ENTRY_FLAG_DONT_ALLOC ) && entry->val ) free( entry->val );

            __DICT_SET_DATA;

            return 0;
        }
        else
        {
            if ( ( entry = malloc( sizeof( DICT_ENTRY_T ) ) ) )
            {
                if ( flags & DICT_ENTRY_FLAG_DONT_ALLOC )
                {
                    entry->key = key;
                }
                else
                {
                    if ( !( entry->key = strdup( key ) ) )
                    {
                        free( entry );
                        return -1;
                    }
                }

                __DICT_SET_DATA;

                /* Insert entry in table */

                entry->prev = NULL;
                entry->next = dict->entries;
                if ( dict->entries ) dict->entries->prev = entry;
                dict->entries = entry;

                /* Insert entry in hashed table */

                entry->prev_hashed = NULL;
                entry->next_hashed = dict->entries_hashed[ hash ];
                if ( dict->entries_hashed[ hash ] ) dict->entries_hashed[ hash ]->prev_hashed = entry;
                dict->entries_hashed[ hash ] = entry ;

                return 0;
            }
        }
    }

    return -1;
}

/* --------------------------------------------------------------------------- */

void * DICT_GetEntry( DICT_T * dict, char * key, int * len )
{
    DICT_ENTRY_T * entry;

    if ( dict && key )
    {
        int hash = make_hash( key );
        if (( entry = __DICT_FINDENTRY( dict, key, hash ) ) )
        {
            if ( len ) *len = entry->len ;
            return ( entry->val );
        }
    }

    return ( NULL );
}

/* --------------------------------------------------------------------------- */

void DICT_DelEntry( DICT_T * dict, char * key )
{
    int hash = make_hash( key );
    DICT_ENTRY_T * entry = __DICT_FINDENTRY( dict, key, hash ) ;

    if ( !entry ) return ;

    if ( !( entry->flags & DICT_ENTRY_FLAG_DONT_ALLOC ) && entry->val ) free( entry->val );

    /* Remove entry from table */

    if ( entry->next ) entry->next->prev = entry->prev ;
    if ( entry->prev ) entry->prev->next = entry->next ;
    else dict->entries = entry->next ; /* No prev */

    /* Remove entry from hashed table */

    if ( entry->next_hashed ) entry->next_hashed->prev_hashed = entry->prev_hashed ;
    if ( entry->prev_hashed ) entry->prev_hashed->next_hashed = entry->next_hashed ;
    else dict->entries_hashed[ hash ] = entry->next_hashed ; /* No prev */

    free( entry->key );
    free( entry );
}

/* --------------------------------------------------------------------------- */

void DICT_DelAllEntries( DICT_T * dict )
{
    DICT_ENTRY_T * entry = NULL;
    while (( entry = DICT_GetNextEntry( dict, NULL ) ) ) DICT_DelEntry( dict, entry->key );
}

/* --------------------------------------------------------------------------- */

DICT_ENTRY_T * DICT_FindEntry( DICT_T * dict, char * key )
{
    int hash = make_hash( key );
    return ( __DICT_FINDENTRY( dict, key, hash ) );
}

/* --------------------------------------------------------------------------- */

int DICT_Merge( DICT_T * target, DICT_T * source )
{
    DICT_ENTRY_T * entry = NULL ;

    while (( entry = DICT_GetNextEntry( source, entry ) ) )
    {
        DICT_AddEntry( target, entry->key, entry->val, entry->len, DICT_ENTRY_FLAG_NORMAL );
    }

    return 0;
}

/* --------------------------------------------------------------------------- */

DICT_ENTRY_T * DICT_GetNextEntry( DICT_T * dict, DICT_ENTRY_T * current )
{
    if ( !current ) return ( dict->entries );
    return ( current->next );
}

/* --------------------------------------------------------------------------- */

DICT_ENTRY_T * DICT_GetPrevEntry( DICT_T * dict, DICT_ENTRY_T * current )
{
    if ( !current )
    {
        current = dict->entries;
        while ( current->next ) current = current->next;
        return ( current );
    }

    return ( current->prev );
}

/* --------------------------------------------------------------------------- */

void DICT_ShowAllEntries( DICT_T * dict, FILE * fp )
{
    DICT_ENTRY_T * entry = NULL;

    while (( entry = DICT_GetNextEntry( dict, entry ) ) )
    {
        fprintf( fp, "[%s]=[", entry->key );
        fwrite( entry->val, 1, entry->len, fp );
        fprintf( fp, "]\n" );
    }
}

/* --------------------------------------------------------------------------- */