2 * Copyright (C) 2007 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
32 typedef struct Hashmap Hashmap
;
35 * Creates a new hash map. Returns NULL if memory allocation fails.
37 * @param initialCapacity number of expected entries
38 * @param hash function which hashes keys
39 * @param equals function which compares keys for equality
41 Hashmap
* hashmapCreate(size_t initialCapacity
,
42 int (*hash
)(void* key
), bool (*equals
)(void* keyA
, void* keyB
));
45 * Frees the hash map. Does not free the keys or values themselves.
47 void hashmapFree(Hashmap
* map
);
50 * Hashes the memory pointed to by key with the given size. Useful for
51 * implementing hash functions.
53 int hashmapHash(void* key
, size_t keySize
);
56 * Puts value for the given key in the map. Returns pre-existing value if
59 * If memory allocation fails, this function returns NULL, the map's size
60 * does not increase, and errno is set to ENOMEM.
62 void* hashmapPut(Hashmap
* map
, void* key
, void* value
);
65 * Gets a value from the map. Returns NULL if no entry for the given key is
66 * found or if the value itself is NULL.
68 void* hashmapGet(Hashmap
* map
, void* key
);
71 * Returns true if the map contains an entry for the given key.
73 bool hashmapContainsKey(Hashmap
* map
, void* key
);
76 * Gets the value for a key. If a value is not found, this function gets a
77 * value and creates an entry using the given callback.
79 * If memory allocation fails, the callback is not called, this function
80 * returns NULL, and errno is set to ENOMEM.
82 void* hashmapMemoize(Hashmap
* map
, void* key
,
83 void* (*initialValue
)(void* key
, void* context
), void* context
);
86 * Removes an entry from the map. Returns the removed value or NULL if no
89 void* hashmapRemove(Hashmap
* map
, void* key
);
92 * Gets the number of entries in this map.
94 size_t hashmapSize(Hashmap
* map
);
97 * Invokes the given callback on each entry in the map. Stops iterating if
98 * the callback returns false.
100 void hashmapForEach(Hashmap
* map
,
101 bool (*callback
)(void* key
, void* value
, void* context
),
105 * Concurrency support.
109 * Locks the hash map so only the current thread can access it.
111 void hashmapLock(Hashmap
* map
);
114 * Unlocks the hash map so other threads can access it.
116 void hashmapUnlock(Hashmap
* map
);
123 * Hashes int keys. 'key' is a pointer to int.
125 int hashmapIntHash(void* key
);
128 * Compares two int keys for equality.
130 bool hashmapIntEquals(void* keyA
, void* keyB
);
137 * Gets current capacity.
139 size_t hashmapCurrentCapacity(Hashmap
* map
);
142 * Counts the number of entry collisions.
144 size_t hashmapCountCollisions(Hashmap
* map
);
150 #endif /* __HASHMAP_H */