#ifndef __6D_HASHTABLE_H
#define __6D_HASHTABLE_H
/*
6D programming language
Copyright (C) 2011 Danny Milosavljevic
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with this program. If not, see .
*/
#include
#include
#include
#ifndef WIN32
#include
#else
typedef unsigned int uint32_t; /* FIXME */
static double log2(double n) {
// log(n)/log(2) is log2.
return log(n)/log(2.0);
}
#endif
#include "Values/Values"
#include "6D/Values"
#include
#include
BEGIN_NAMESPACE_6D(Values)
static INLINE uint32_t jenkinsOneAtATimeHash(const char* key, size_t len) {
uint32_t hash, i;
for(hash = i = 0; i < len; ++i) {
hash += key[i];
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
return hash;
}
static INLINE uint32_t jenkinsOneAtATimeHashC(const char* key) {
uint32_t hash, i;
for(hash = i = 0; key[i]; ++i) {
hash += key[i];
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
return hash;
}
static INLINE uint32_t hashSymbol(NodeT sym) {
const char* str = symbolName(sym);
return jenkinsOneAtATimeHashC(str);
}
static INLINE bool eqSymbol(NodeT a, NodeT b) {
return a == b;
}
static INLINE bool ltSymbol(NodeT a, NodeT b) {
return a < b;
}
enum HashtableEntryState {
HS_FREE,
HS_OCCUPIED,
HS_FREE_AGAIN,
};
struct HashtableEntry {
enum HashtableEntryState state;
NODET first; // key. Idiotic name because of interface compability.
NODET second; // value. Idiotic name because of interface compability.
};
BEGIN_STRUCT_6D(Hashtable)
int count;
int capacity;
bool bGC;
struct HashtableEntry* entries;
END_STRUCT_6D(Hashtable)
NodeT getHashtableValueByKey(NodeT hashtable, NodeT key, NodeT fallback);
void setHashtableEntry(NodeT hashtable, NodeT key, NodeT value);
void removeHashtableEntry(NodeT hashtable, NodeT key);
NodeT makeHashtable(void);
NodeT makeHashtableNoGC(void);
END_NAMESPACE_6D(Values)
#endif /* ndef __6D_HASH_TABLE_H */