Example #1
0
void HashStore(VALUE key_identifier, VALUE store, HASH_TABLE* table)
{
    unsigned int index;
    KEY_VALUE_PAIR* table_entries;

    if (key_identifier.type == VAL_NIL) return;
    if (table->size > 5*table->capacity/7) 
    {
        ResizeHashTable(table);
    }

    index = HashFunction(key_identifier);
    index = index % table->capacity;
    table_entries = table->table;

    if (key_identifier.type == VAL_STRING)
    {
        while (table_entries[index].key.type != VAL_NIL &&
               !(table_entries[index].key.type == VAL_STRING &&
                 strcmp(table_entries[index].key.data.string,
                        key_identifier.data.string) == 0))
        {
            index = (index+1) % table->capacity;
        }
    }
    else
    {
        while (table_entries[index].key.type != VAL_NIL &&
               !(key_identifier.type == table_entries[index].key.type &&
                 key_identifier.data.primitive == table_entries[index].key.data.primitive))
        {
            index = (index+1) % table->capacity;
        }
    }

    if (table_entries[index].key.type == VAL_NIL) table->size++;
    table_entries[index].key = key_identifier;
    table_entries[index].value = store;
    return;    
}
Example #2
0
SC_FUNC void
AddToHashTable(HashTable *ht, symbol *sym)
{
    uint32_t bucket = sym->hash & ht->bucketmask;
    HashEntry **hep, *he;

    hep = &ht->buckets[bucket];
    while (*hep) {
        assert((*hep)->sym != sym);
        hep = &(*hep)->next;
    }

    he = (HashEntry *)malloc(sizeof(HashEntry));
    if (!he)
      error(163);
    he->sym = sym;
    he->next = NULL;
    *hep = he;
    ht->nused++;

    if (ht->nused > ht->nbuckets && ht->nbuckets <= INT_MAX / 2)
        ResizeHashTable(ht);
}
Example #3
0
Atom 
MakeAtom(char *string, unsigned len, int makeit)
{
    AtomListPtr	a;
    int		hash;
    int		h = 0;
    int		r;

    hash = Hash (string, len);
    if (hashTable)
    {
    	h = hash & hashMask;
	if (hashTable[h])
	{
	    if (hashTable[h]->hash == hash && hashTable[h]->len == len &&
	    	NameEqual (hashTable[h]->name, string, len))
	    {
	    	return hashTable[h]->atom;
	    }
	    r = (hash % rehash) | 1;
	    for (;;)
	    {
		h += r;
		if (h >= hashSize)
		    h -= hashSize;
		if (!hashTable[h])
		    break;
		if (hashTable[h]->hash == hash && hashTable[h]->len == len &&
		    NameEqual (hashTable[h]->name, string, len))
		{
		    return hashTable[h]->atom;
		}
	    }
    	}
    }
    if (!makeit)
	return None;
    a = (AtomListPtr) xalloc (sizeof (AtomListRec) + len + 1);
    if (a == NULL) {
	fprintf(stderr, "MakeAtom(): Error: Couldn't allocate AtomListRec"
		" (%ld)\n", (unsigned long)sizeof (AtomListRec) + len + 1);
	return None;
    }
    a->name = (char *) (a + 1);
    a->len = len;
    strncpy (a->name, string, len);
    a->name[len] = '\0';
    a->atom = ++lastAtom;
    a->hash = hash;
    if (hashUsed >= hashSize / 2)
    {
	ResizeHashTable ();
	h = hash & hashMask;
	if (hashTable[h])
	{
	    r = (hash % rehash) | 1;
	    do {
		h += r;
		if (h >= hashSize)
		    h -= hashSize;
	    } while (hashTable[h]);
	}
    }
    hashTable[h] = a;
    hashUsed++;
    if (reverseMapSize <= a->atom) {
	if (!ResizeReverseMap())
	    return None;
    }
    reverseMap[a->atom] = a;
    return a->atom;
}