2008-03-21 21:16:24 +01:00
|
|
|
/*
|
|
|
|
* name-hash.c
|
|
|
|
*
|
|
|
|
* Hashing names in the index state
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008 Linus Torvalds
|
|
|
|
*/
|
|
|
|
#define NO_THE_INDEX_COMPATIBILITY_MACROS
|
|
|
|
#include "cache.h"
|
|
|
|
|
|
|
|
static unsigned int hash_name(const char *name, int namelen)
|
|
|
|
{
|
|
|
|
unsigned int hash = 0x123;
|
|
|
|
|
|
|
|
do {
|
|
|
|
unsigned char c = *name++;
|
|
|
|
hash = hash*101 + c;
|
|
|
|
} while (--namelen);
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void hash_index_entry(struct index_state *istate, struct cache_entry *ce)
|
|
|
|
{
|
|
|
|
void **pos;
|
|
|
|
unsigned int hash;
|
|
|
|
|
|
|
|
if (ce->ce_flags & CE_HASHED)
|
|
|
|
return;
|
|
|
|
ce->ce_flags |= CE_HASHED;
|
|
|
|
ce->next = NULL;
|
|
|
|
hash = hash_name(ce->name, ce_namelen(ce));
|
|
|
|
pos = insert_hash(hash, ce, &istate->name_hash);
|
|
|
|
if (pos) {
|
|
|
|
ce->next = *pos;
|
|
|
|
*pos = ce;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void lazy_init_name_hash(struct index_state *istate)
|
|
|
|
{
|
|
|
|
int nr;
|
|
|
|
|
|
|
|
if (istate->name_hash_initialized)
|
|
|
|
return;
|
|
|
|
for (nr = 0; nr < istate->cache_nr; nr++)
|
|
|
|
hash_index_entry(istate, istate->cache[nr]);
|
|
|
|
istate->name_hash_initialized = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void add_name_hash(struct index_state *istate, struct cache_entry *ce)
|
|
|
|
{
|
|
|
|
ce->ce_flags &= ~CE_UNHASHED;
|
|
|
|
if (istate->name_hash_initialized)
|
|
|
|
hash_index_entry(istate, ce);
|
|
|
|
}
|
|
|
|
|
2008-03-21 23:53:00 +01:00
|
|
|
struct cache_entry *index_name_exists(struct index_state *istate, const char *name, int namelen)
|
2008-03-21 21:16:24 +01:00
|
|
|
{
|
|
|
|
unsigned int hash = hash_name(name, namelen);
|
|
|
|
struct cache_entry *ce;
|
|
|
|
|
|
|
|
lazy_init_name_hash(istate);
|
|
|
|
ce = lookup_hash(hash, &istate->name_hash);
|
|
|
|
|
|
|
|
while (ce) {
|
|
|
|
if (!(ce->ce_flags & CE_UNHASHED)) {
|
|
|
|
if (!cache_name_compare(name, namelen, ce->name, ce->ce_flags))
|
2008-03-21 23:53:00 +01:00
|
|
|
return ce;
|
2008-03-21 21:16:24 +01:00
|
|
|
}
|
|
|
|
ce = ce->next;
|
|
|
|
}
|
2008-03-21 23:53:00 +01:00
|
|
|
return NULL;
|
2008-03-21 21:16:24 +01:00
|
|
|
}
|