Move name hashing functions into a file of its own
[alt-git.git] / name-hash.c
blobe56eb16c2836b3e4227cffed68703e9d42b51b9e
1 /*
2 * name-hash.c
4 * Hashing names in the index state
6 * Copyright (C) 2008 Linus Torvalds
7 */
8 #define NO_THE_INDEX_COMPATIBILITY_MACROS
9 #include "cache.h"
11 static unsigned int hash_name(const char *name, int namelen)
13 unsigned int hash = 0x123;
15 do {
16 unsigned char c = *name++;
17 hash = hash*101 + c;
18 } while (--namelen);
19 return hash;
22 static void hash_index_entry(struct index_state *istate, struct cache_entry *ce)
24 void **pos;
25 unsigned int hash;
27 if (ce->ce_flags & CE_HASHED)
28 return;
29 ce->ce_flags |= CE_HASHED;
30 ce->next = NULL;
31 hash = hash_name(ce->name, ce_namelen(ce));
32 pos = insert_hash(hash, ce, &istate->name_hash);
33 if (pos) {
34 ce->next = *pos;
35 *pos = ce;
39 static void lazy_init_name_hash(struct index_state *istate)
41 int nr;
43 if (istate->name_hash_initialized)
44 return;
45 for (nr = 0; nr < istate->cache_nr; nr++)
46 hash_index_entry(istate, istate->cache[nr]);
47 istate->name_hash_initialized = 1;
50 void add_name_hash(struct index_state *istate, struct cache_entry *ce)
52 ce->ce_flags &= ~CE_UNHASHED;
53 if (istate->name_hash_initialized)
54 hash_index_entry(istate, ce);
57 int index_name_exists(struct index_state *istate, const char *name, int namelen)
59 unsigned int hash = hash_name(name, namelen);
60 struct cache_entry *ce;
62 lazy_init_name_hash(istate);
63 ce = lookup_hash(hash, &istate->name_hash);
65 while (ce) {
66 if (!(ce->ce_flags & CE_UNHASHED)) {
67 if (!cache_name_compare(name, namelen, ce->name, ce->ce_flags))
68 return 1;
70 ce = ce->next;
72 return 0;