File: map.c

package info (click to toggle)
directfb 1.7.7-11
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 32,212 kB
  • sloc: ansic: 306,760; cpp: 46,357; sh: 11,720; makefile: 5,620; perl: 662; asm: 507; xml: 116
file content (341 lines) | stat: -rw-r--r-- 9,020 bytes parent folder | download | duplicates (2)
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
/*
   (c) Copyright 2012-2013  DirectFB integrated media GmbH
   (c) Copyright 2001-2013  The world wide DirectFB Open Source Community (directfb.org)
   (c) Copyright 2000-2004  Convergence (integrated media) GmbH

   All rights reserved.

   Written by Denis Oliver Kropp <dok@directfb.org>,
              Andreas Shimokawa <andi@directfb.org>,
              Marek Pikarski <mass@directfb.org>,
              Sven Neumann <neo@directfb.org>,
              Ville Syrjälä <syrjala@sci.fi> and
              Claudio Ciccani <klan@users.sf.net>.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the
   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
*/



#include <config.h>

#include <stdlib.h>

#include <direct/debug.h>
#include <direct/map.h>
#include <direct/mem.h>
#include <direct/messages.h>


D_DEBUG_DOMAIN( Direct_Map, "Direct/Map", "Map implementation" );

/**********************************************************************************************************************/

#define REMOVED  ((void *) -1)

typedef struct {
     unsigned int           hash;
     void                  *object;
} MapEntry;

struct __D_DirectMap {
     int                    magic;

     unsigned int           size;

     unsigned int           count;
     unsigned int           removed;

     MapEntry              *entries;

     DirectMapCompareFunc   compare;
     DirectMapHashFunc      hash;
     void                  *ctx;
};

#define DIRECT_MAP_ASSERT( map )             \
     do {                                    \
          D_MAGIC_ASSERT( map, DirectMap );  \
     } while (0)

/**********************************************************************************************************************/

static int
locate_entry( DirectMap *map, unsigned int hash, const void *key )
{
     int             pos;
     const MapEntry *entry;

     D_DEBUG_AT( Direct_Map, "%s( hash %u )\n", __func__, hash );

     DIRECT_MAP_ASSERT( map );
     D_ASSERT( key != NULL );

     pos = hash % map->size;

     entry = &map->entries[pos];

     while (entry->object) {
          if (entry->object != REMOVED && entry->hash == hash && map->compare( map, key, entry->object, map->ctx ))
               return pos;

          if (++pos == map->size)
               pos = 0;

          entry = &map->entries[pos];
     }

     return -1;
}

static DirectResult
resize_map( DirectMap    *map,
            unsigned int  size )
{
     unsigned int  i, pos;
     MapEntry     *entries;

     D_DEBUG_AT( Direct_Map, "%s( size %u )\n", __func__, size );

     DIRECT_MAP_ASSERT( map );
     D_ASSERT( size > 3 );

     entries = D_CALLOC( size, sizeof(MapEntry) );
     if (!entries)
          return D_OOM();

     for (i=0; i<map->size; i++) {
          MapEntry *entry = &map->entries[i];
          MapEntry *insertElement;

          if (entry->object && entry->object != REMOVED) {
               pos = entry->hash % size;

               insertElement = &entries[pos];
               while (insertElement->object && insertElement->object != REMOVED) {
                   if (++pos == size)
                       pos = 0;
                   insertElement = &entries[pos];
               }

               entries[pos] = *entry;
          }
     }

     D_FREE( map->entries );

     map->size    = size;
     map->entries = entries;
     map->removed = 0;

     return DR_OK;
}

/**********************************************************************************************************************/

DirectResult
direct_map_create( unsigned int           initial_size,
                   DirectMapCompareFunc   compare_func,
                   DirectMapHashFunc      hash_func,
                   void                  *ctx,
                   DirectMap            **ret_map )
{
     DirectMap *map;

     D_DEBUG_AT( Direct_Map, "%s( size %u, compare %p, hash %p )\n", __func__, initial_size, compare_func, hash_func );

     D_ASSERT( compare_func != NULL );
     D_ASSERT( hash_func != NULL );
     D_ASSERT( ret_map != NULL );

     if (initial_size < 3)
          initial_size = 3;

     map = D_CALLOC( 1, sizeof (DirectMap) );
     if (!map)
          return D_OOM();

     map->entries = D_CALLOC( initial_size, sizeof(MapEntry) );
     if (!map->entries) {
          D_FREE( map );
          return D_OOM();
     }

     map->size    = initial_size;
     map->compare = compare_func;
     map->hash    = hash_func;
     map->ctx     = ctx;

     D_MAGIC_SET( map, DirectMap );

     *ret_map = map;

     return DR_OK;
}

void
direct_map_destroy( DirectMap *map )
{
     D_DEBUG_AT( Direct_Map, "%s()\n", __func__ );

     DIRECT_MAP_ASSERT( map );

     D_MAGIC_CLEAR( map );

     D_FREE( map->entries );
     D_FREE( map );
}

DirectResult
direct_map_insert( DirectMap  *map,
                   const void *key,
                   void       *object )
{
     unsigned int hash;
     int          pos;
     MapEntry    *entry;

     D_DEBUG_AT( Direct_Map, "%s( key %p, object %p )\n", __func__, key, object );

     DIRECT_MAP_ASSERT( map );
     D_ASSERT( key != NULL );
     D_ASSERT( object != NULL );

     /* Need to resize the map? */
     if ((map->count + map->removed) > map->size / 4)
          resize_map( map, map->size * 3 );

	 hash = map->hash( map, key, map->ctx );
	 pos  = hash % map->size;

     D_DEBUG_AT( Direct_Map, "  -> hash %u, pos %d\n", hash, pos );

     entry = &map->entries[pos];

     while (entry->object && entry->object != REMOVED) {
          if (entry->hash == hash && map->compare( map, key, entry->object, map->ctx )) {
               if (entry->object == object) {
                    D_DEBUG_AT( Direct_Map, "  -> same object with matching key already exists\n" );
                    return DR_BUSY;
               }
               else {
                    D_DEBUG_AT( Direct_Map, "  -> different object with matching key already exists\n" );
                    D_BUG( "different object with matching key already exists" );
                    return DR_BUG;
               }
          }

          if (++pos == map->size)
               pos = 0;

          entry = &map->entries[pos];
     }

     if (entry->object == REMOVED)
          map->removed--;

     entry->hash   = hash;
     entry->object = object;

     map->count++;

     D_DEBUG_AT( Direct_Map, "  -> new count = %d, removed = %d, size = %d\n", map->count, map->removed, map->size );

     return DR_OK;
}

DirectResult
direct_map_remove( DirectMap  *map,
                   const void *key )
{
     unsigned int hash;
     int          pos;

     D_DEBUG_AT( Direct_Map, "%s( key %p )\n", __func__, key );

     DIRECT_MAP_ASSERT( map );
     D_ASSERT( key != NULL );

     hash = map->hash( map, key, map->ctx );

     pos = locate_entry( map, hash, key );
     if (pos == -1) {
          D_WARN( "object to remove not found" );
          return DR_ITEMNOTFOUND;
     }

     map->entries[pos].object = REMOVED;

     map->count--;
     map->removed++;

     D_DEBUG_AT( Direct_Map, "  -> new count = %d, removed = %d, size = %d\n", map->count, map->removed, map->size );

     return DR_OK;
}

void *
direct_map_lookup( DirectMap  *map,
                   const void *key )
{
     unsigned int hash;
     int          pos;

     D_DEBUG_AT( Direct_Map, "%s( key %p )\n", __func__, key );

     DIRECT_MAP_ASSERT( map );
     D_ASSERT( key != NULL );

     hash = map->hash( map, key, map->ctx );

     pos = locate_entry( map, hash, key );

     return (pos != -1) ? map->entries[pos].object : NULL;
}

void
direct_map_iterate( DirectMap             *map,
                    DirectMapIteratorFunc  func,
                    void                  *ctx )
{
     unsigned int i;

     D_DEBUG_AT( Direct_Map, "%s( func %p )\n", __func__, func );

     DIRECT_MAP_ASSERT( map );
     D_ASSERT( func != NULL );

     for (i=0; i<map->size; i++) {
          MapEntry *entry = &map->entries[i];

          if (entry->object && entry->object != REMOVED) {
               switch (func( map, entry->object, ctx )) {
                    case DENUM_OK:
                         break;

                    case DENUM_CANCEL:
                         return;

                    case DENUM_REMOVE:
                         entry->object = REMOVED;

                         map->count--;
                         map->removed++;
               }
          }
     }
}