Objectively
Ultra-lightweight object oriented framework for GNU C.
Loading...
Searching...
No Matches
HashTable.h
Go to the documentation of this file.
1/*
2 * Objectively: Ultra-lightweight object oriented framework for GNU C.
3 * Copyright (C) 2014 Jay Dolan <jay@jaydolan.com>
4 *
5 * This software is provided 'as-is', without any express or implied
6 * warranty. In no event will the authors be held liable for any damages
7 * arising from the use of this software.
8 *
9 * Permission is granted to anyone to use this software for any purpose,
10 * including commercial applications, and to alter it and redistribute it
11 * freely, subject to the following restrictions:
12 *
13 * 1. The origin of this software must not be misrepresented; you must not
14 * claim that you wrote the original software. If you use this software
15 * in a product, an acknowledgment in the product documentation would be
16 * appreciated but is not required.
17 *
18 * 2. Altered source versions must be plainly marked as such, and must not be
19 * misrepresented as being the original software.
20 *
21 * 3. This notice may not be removed or altered from any source distribution.
22 */
23
24#pragma once
25
26#include <Objectively/Object.h>
27
33typedef struct HashTable HashTable;
34typedef struct HashTableInterface HashTableInterface;
35
39typedef size_t (*HashTableHashFunc)(const ident key);
40
44typedef bool (*HashTableEqualFunc)(const ident a, const ident b);
45
53typedef void (*HashTableEnumerator)(const HashTable *table, ident key, ident value, ident data);
54
59OBJECTIVELY_EXPORT bool HashTableEqualStr(const ident a, const ident b);
64
69typedef struct HashTableEntry {
70 ident key;
71 ident value;
72 struct HashTableEntry *next;
73} HashTableEntry;
74
80struct HashTable {
81
86
91 HashTableInterface *interface;
92
96 size_t count;
97
102 size_t capacity;
103
108 HashTableEntry **buckets;
109
114
119
124
129};
130
134struct HashTableInterface {
135
139 ObjectInterface objectInterface;
140
148 bool (*containsKey)(const HashTable *self, const ident key);
149
158 void (*enumerate)(const HashTable *self, HashTableEnumerator enumerator, ident data);
159
167 ident (*get)(const HashTable *self, const ident key);
168
179
190 HashTable *(*initWithCapacity)(HashTable *self, HashTableHashFunc hash, HashTableEqualFunc equal, size_t capacity);
191
199 void (*remove)(HashTable *self, const ident key);
200
207 void (*removeAll)(HashTable *self);
208
217 void (*set)(HashTable *self, const ident key, const ident value);
218};
219
static int hash(const Object *self)
Definition Array.c:129
static Data * data(void)
Definition Data.c:286
bool(* HashTableEqualFunc)(const ident a, const ident b)
A function that tests equality of two keys.
Definition HashTable.h:44
OBJECTIVELY_EXPORT size_t HashTableHashStr(const ident key)
Common hash functions for use with HashTable.
Definition HashTable.c:45
OBJECTIVELY_EXPORT Class * _HashTable(void)
Definition HashTable.c:328
size_t(* HashTableHashFunc)(const ident key)
A function that computes a hash for the given key.
Definition HashTable.h:39
OBJECTIVELY_EXPORT bool HashTableEqualDirect(const ident a, const ident b)
Definition HashTable.c:92
OBJECTIVELY_EXPORT bool HashTableEqualStr(const ident a, const ident b)
Definition HashTable.c:58
OBJECTIVELY_EXPORT bool HashTableEqualStri(const ident a, const ident b)
Definition HashTable.c:78
void(* HashTableEnumerator)(const HashTable *table, ident key, ident value, ident data)
The HashTableEnumerator function type.
Definition HashTable.h:53
OBJECTIVELY_EXPORT size_t HashTableHashDirect(const ident key)
Definition HashTable.c:85
OBJECTIVELY_EXPORT size_t HashTableHashStri(const ident key)
Definition HashTable.c:65
Object is the root Class of The Objectively Class hierarchy.
static Unicode next(StringReader *self, StringReaderMode mode)
void * ident
The identity type, similar to Objective-C id.
Definition Types.h:49
#define OBJECTIVELY_EXPORT
Definition Types.h:36
void(* Consumer)(ident data)
The Consumer function type.
Definition Types.h:88
The runtime representation of a Class.
Definition Class.h:95
Hash tables with user-supplied hash and equality functions.
Definition HashTable.h:80
Object object
The superclass.
Definition HashTable.h:85
HashTableInterface * interface
The interface.
Definition HashTable.h:91
size_t capacity
The number of buckets.
Definition HashTable.h:102
HashTableEntry ** buckets
The buckets.
Definition HashTable.h:108
size_t count
The number of entries.
Definition HashTable.h:96
Consumer destroyKey
Optional destructor called when a key is removed or replaced.
Definition HashTable.h:123
HashTableHashFunc hash
The hash function.
Definition HashTable.h:113
Consumer destroyValue
Optional destructor called when a value is removed or replaced.
Definition HashTable.h:128
HashTableEqualFunc equal
The equality function.
Definition HashTable.h:118
Object is the root Class of The Objectively Class hierarchy.
Definition Object.h:46