git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] Document khash

From
Thomas Rast <tr@thomasrast.ch>
Date
Nov 25, 2013, 15:04 UTC
Message-ID
<ccce08818b6856f67a5316eba148d5a860d1d8f7.1385391631.git.tr@thomasrast.ch>
In-Reply-To
<87fvqlfpmw.fsf@linux-k42r.v.cablecom.net>
For squashing into a commit that adds khash.h.
Signed-off-by: Thomas Rast <tr@thomasrast.ch>
---
> I think I'll also lend you a hand writing Documentation/technical/api-khash.txt
> (expect it tomorrow) so that we also have documentation in the git
> style, where gitters can be expected to find it on their own.
Here goes.
> Furthermore, would it be a problem to name the second hash sha1_int
> instead?  I have another use for such a hash, and I can't imagine I'm
> the only one.  (That's not critical however, I can do the required
> editing in that other series.)

Actually, let's not do that. Since everything is 'static inline' anyway, there's no cost to simply instantiating more hashes as needed.

 Documentation/technical/api-khash.txt | 109 ++++++++++++++++++++++++++++++++++
 1 file changed, 109 insertions(+)
 create mode 100644 Documentation/technical/api-khash.txt
diff --git a/Documentation/technical/api-khash.txt b/Documentation/technical/api-khash.txt
new file mode 100644
index 0000000..e7ca883
--- /dev/null
+++ b/Documentation/technical/api-khash.txt
@@ -0,0 +1,109 @@
+khash
+=====
+
+The khash API is a collection of macros that instantiate hash table
+routines for arbitrary key/value types.
+
+It lives in 'khash.h', which we imported from
+  https://github.com/attractivechaos/klib/blob/master/khash.h
+and then gitified somewhat.
+
+Usage
+-----
+
+------------
+#import "khash.h"
+KHASH_INIT(NAME, key_t, value_t, is_map, key_hash_fn, key_equal_fn)
+------------
+
+The arguments are as follows:
+
+`NAME`::
+	Used to give your hash and its API a unique name.  Spelled
+	`NAME` here to set it apart from the rest of the names, but
+	generally should be a lowercase C identifier.
+
+`key_t`::
+	Type of the keys for your hashes.  Generally should be
+	`const`.
+
+`value_t`::
+	Type of the values of your hashes.
+
+`is_map`::
+	Whether the hash holds values.  Set to 0 to create a hash for
+	use as a set.
+
+`khint_t key_hash_fn(key_t)`::
+	Hash function.
+
+`int key_equal_fn(key_t a, key_t b)`::
+	Comparison function.  Return 1 if the two keys are the same, 0
+	otherwise.
+
+These two functions may also be macros.
+
+
+API
+---
+
+The above instantiation defines a single type:
+
+`kh_NAME_t`::
+	A struct that holds your hash.  You should only ever use
+	pointers to it.
+
+After the above instantiation, the following functions and
+function-like macros are defined:
+
+`kh_NAME_t *kh_init_NAME(void)`::
+	Allocate and initialize a new hash table.
+
+`void kh_destroy_NAME(kh_NAME_t *)`::
+	Free the hash table.
+
+`void kh_clear_NAME(kh_NAME_t *)`::
+	Clear (but do not free) the hash table.
+
+`khint_t kh_get_NAME(const kh_NAME_t *hash, key_t key)`::
+	Find the given key in the hash table.  The returned khint_t
+	should be treated as an opaque iterator token that indexes
+	into the hash.  Use `kh_value` to get the value from the
+	token.  If the key does not exist, returns kh_end(hash).
+
+`khint_t kh_put_NAME(const kh_NAME_t *hash, key_t key, int *ret)`::
+	Put the given key in the hash table.  The returned khint_t
+	should be treated as an opaque iterator token that indexes
+	into the hash.  Use `kh_value(hash, token) = value` to assign
+	a value after putting the key.
++
+'ret' tells you whether the key already existed: it is -1 if the
+operation failed; 0 if the key was already present; 1 if the bucket is
+empty; 2 if the bucket has been deleted.
+
+`kh_key(kh_NAME_t *, khint_t)`::
+	The key slot for the given iterator token.  This can be used
+	as an lvalue, but you must not change the key!
+
+`kh_value(kh_NAME_t *, khint_t)`::
+`kh_val(kh_NAME_t *, khint_t)`::
+	The value slot for the given iterator token.  This can be used
+	as an lvalue.
+
+`int kh_exist(const kh_NAME_t *, key_t)`::
+	Tests if the given bucket contains data.
+
+`khint_t kh_begin(const kh_NAME_t *)`::
+	The smallest iterator token.
+
+`khint_t kh_end(const kh_NAME_t *)`::
+	The beyond-the-hash iterator token; it is one larger than the
+	largest token that points to a hash member.
+
+`khint_t kh_size(const kh_NAME_t *)`::
+	The number of elements in the hash.
+
+`kh_foreach(const kh_NAME_t *, keyvar, valuevar, { ... loop body ... })`::
+	Iterate over every key-value pair in the hash.  This is a
+	macro, and keyvar and valuevar must be pre-declared of type
+	key_t and value_t, respectively.
-- 
1.8.5.rc3.397.g2a3acd5
Previous: Thomas RastNext: Jeff King
Message 13 of 55 in “pack bitmaps”
  1. 0/21 pack bitmapsJeff King, Nov 14, 2013
  2. 01/21 sha1write: make buffer const-correctJeff King, Nov 14, 2013
  3. 02/21 revindex: Export new APIsJeff King, Nov 14, 2013
  4. 03/21 pack-objects: Refactor the packing listJeff King, Nov 14, 2013
  5. 04/21 pack-objects: factor out name_hashJeff King, Nov 14, 2013
  6. 05/21 revision: allow setting custom limiter functionJeff King, Nov 14, 2013
  7. 06/21 sha1_file: export `git_open_noatime`Jeff King, Nov 14, 2013
  8. 07/21 compat: add endianness helpersJeff King, Nov 14, 2013
  9. 08/21 ewah: compressed bitmap implementationJeff King, Nov 14, 2013
  10. 09/21 documentation: add documentation for the bitmap formatJeff King, Nov 14, 2013
  11. 10/21 pack-bitmap: add support for bitmap indexesJeff King, Nov 14, 2013
  12. Thomas RastNov 24, 2013
  13. Document khashThomas Rast, Nov 25, 2013
  14. Jeff KingNov 28, 2013
  15. Karsten BleesNov 27, 2013
  16. Jeff KingNov 28, 2013
  17. Karsten BleesDec 3, 2013
  18. Jeff KingDec 3, 2013
  19. Karsten BleesDec 7, 2013
  20. Thomas RastNov 29, 2013
  21. Jeff KingDec 2, 2013
  22. Junio C HamanoDec 2, 2013
  23. Jeff KingDec 2, 2013
  24. Junio C HamanoDec 2, 2013
  25. 11/21 pack-objects: use bitmaps when packing objectsJeff King, Nov 14, 2013
  26. Thomas RastDec 7, 2013
  27. Jeff KingDec 21, 2013
  28. 12/21 rev-list: add bitmap mode to speed up object listsJeff King, Nov 14, 2013
  29. Thomas RastDec 7, 2013
  30. 13/21 pack-objects: implement bitmap writingJeff King, Nov 14, 2013
  31. Thomas RastDec 7, 2013
  32. Jeff KingDec 21, 2013
  33. 14/21 repack: stop using magic number for ARRAY_SIZE(exts)Jeff King, Nov 14, 2013
  34. Thomas RastDec 7, 2013
  35. 15/21 repack: turn exts array into array-of-structJeff King, Nov 14, 2013
  36. Thomas RastDec 7, 2013
  37. 16/21 repack: handle optional files created by pack-objectsJeff King, Nov 14, 2013
  38. Thomas RastDec 7, 2013
  39. 17/21 repack: consider bitmaps when performing repacksJeff King, Nov 14, 2013
  40. Thomas RastDec 7, 2013
  41. 18/21 count-objects: recognize .bitmap in garbage-checkingJeff King, Nov 14, 2013
  42. Thomas RastDec 7, 2013
  43. 19/21 t: add basic bitmap functionality testsJeff King, Nov 14, 2013
  44. Thomas RastDec 7, 2013
  45. Jeff KingDec 21, 2013
  46. 20/21 t/perf: add tests for pack bitmapsJeff King, Nov 14, 2013
  47. Thomas RastDec 7, 2013
  48. Jeff KingDec 21, 2013
  49. 21/21 pack-bitmap: implement optional name_hash cacheJeff King, Nov 14, 2013
  50. Thomas RastDec 7, 2013
  51. Ramsay JonesNov 14, 2013
  52. Jeff KingNov 14, 2013
  53. Ramsay JonesNov 14, 2013
  54. Ramsay JonesNov 18, 2013
  55. Thomas RastNov 16, 2013

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.