tommytrie.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010, Andrea Mazzoleni. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27
125#ifndef __TOMMYTRIE_H
126#define __TOMMYTRIE_H
127
128#include "tommytypes.h"
129#include "tommyalloc.h"
130
131/******************************************************************************/
132/* trie */
133
142#define TOMMY_TRIE_BIT 32
143
151#define TOMMY_TRIE_TREE_MAX (64 / sizeof(void*))
152
157#define TOMMY_TRIE_BLOCK_SIZE (TOMMY_TRIE_TREE_MAX * sizeof(void*))
158
162#define TOMMY_TRIE_TREE_BIT TOMMY_ILOG2(TOMMY_TRIE_TREE_MAX)
163
167#define TOMMY_TRIE_BUCKET_BIT ((TOMMY_TRIE_BIT % TOMMY_TRIE_TREE_BIT) + TOMMY_TRIE_TREE_BIT)
168
173#define TOMMY_TRIE_BUCKET_MAX (1 << TOMMY_TRIE_BUCKET_BIT)
174
180
185typedef struct tommy_trie_struct {
186 tommy_trie_node* bucket[TOMMY_TRIE_BUCKET_MAX];
191
200TOMMY_API void tommy_trie_init(tommy_trie* trie, tommy_allocator* alloc);
201
210TOMMY_API void tommy_trie_insert(tommy_trie* trie, tommy_trie_node* node, void* data, tommy_key_t key);
211
220TOMMY_API void* tommy_trie_remove(tommy_trie* trie, tommy_key_t key);
221
230
238tommy_inline void* tommy_trie_search(tommy_trie* trie, tommy_key_t key)
239{
240 tommy_trie_node* i = tommy_trie_bucket(trie, key);
241
242 if (!i)
243 return 0;
244
245 return i->data;
246}
247
254
259{
260 return trie->count;
261}
262
268
269#endif
Allocator of fixed size blocks.
Definition: tommyalloc.h:51
Data structure node.
Definition: tommytypes.h:211
void * data
Pointer to the object containing the node.
Definition: tommytypes.h:228
Trie container type.
Definition: tommytrie.h:185
tommy_allocator * alloc
Allocator for internal nodes.
Definition: tommytrie.h:189
tommy_size_t count
Number of elements.
Definition: tommytrie.h:187
tommy_trie_node * bucket[TOMMY_TRIE_BUCKET_MAX]
First tree level.
Definition: tommytrie.h:186
tommy_size_t node_count
Number of nodes.
Definition: tommytrie.h:188
Allocator of fixed size blocks.
TOMMY_API void * tommy_trie_remove(tommy_trie *trie, tommy_key_t key)
Searches and removes the first element with the specified key.
struct tommy_trie_struct tommy_trie
Trie container type.
TOMMY_API void tommy_trie_init(tommy_trie *trie, tommy_allocator *alloc)
Initializes the trie.
TOMMY_API tommy_size_t tommy_trie_memory_usage(tommy_trie *trie)
Gets the size of allocated memory.
TOMMY_API void tommy_trie_insert(tommy_trie *trie, tommy_trie_node *node, void *data, tommy_key_t key)
Inserts an element in the trie.
tommy_node tommy_trie_node
Trie node.
Definition: tommytrie.h:179
void * tommy_trie_search(tommy_trie *trie, tommy_key_t key)
Searches an element in the trie.
Definition: tommytrie.h:238
TOMMY_API void * tommy_trie_remove_existing(tommy_trie *trie, tommy_trie_node *node)
Removes an element from the trie.
tommy_size_t tommy_trie_count(tommy_trie *trie)
Gets the number of elements.
Definition: tommytrie.h:258
TOMMY_API tommy_trie_node * tommy_trie_bucket(tommy_trie *trie, tommy_key_t key)
Gets the bucket of the specified key.
Generic types.
uint64_t tommy_size_t
Generic size_t type.
Definition: tommytypes.h:60
tommy_size_t tommy_key_t
Type used in indexed data structures to store the key of an object.
Definition: tommytypes.h:188