Example #1
0
bool MonomialHashTable<ValueType>::find_or_insert(value m, value &result)
  // return true if the value already exists in the table.
  // otherwise, result is set to the new value.
{
  long hashval = HASHVALUE(m) & hashmask;
  if (!hashtab[hashval])
    {
      // No entry is there.  So, we insert it directly.
      hashtab[hashval] = m;
      result = m;
      count++;
      if (count > threshold) grow();
      return false;
    }
  else
    {
      // Something is there, so we need to find either this value,
      // or a free spot, whichever comes first.
      long mhash = HASHVALUE(m);
      value *hashtop = hashtab + size;
      long run_len = 1;
      for (value *i = hashtab + hashval; ; i++, run_len++)
        {
          if (run_len > max_run_length)
            max_run_length = run_len;
          if (i == hashtop) i = hashtab;
          if (!(*i))
            {
              // Spot is empty, so m is a new value
              *i = m;
              result = m;
              count++;
              if (count > threshold) grow();
              return false;
            }
          if (HASHVALUE(*i) == mhash)
            {
              monequal_count++;
              if (MONOMIAL_EQUAL(m, *i))
                {
                  monequal_count++;
                  result = *i;
                  return true;
                }
              monequal_fails++;
            }
          nclashes++;
        }
    }
}
Example #2
0
void decrease_refcount(void *pointer)
{
    int i=HASHVALUE(pointer);
    RefElem **re=&(hash_table[i]);

    if (!pointer) return;
    while (*re && (*re)->pointer!=pointer)
        re=&(*re)->next;
    if (*re) {
        (*re)->count--;
        LOG_MESSAGE("setting counter to %i\n", (*re)->count);
        if (!(*re)->count) {
            RefElem *h;
            LOG_MESSAGE("counter at 0, freeing %x\n", pointer);
            h=*re;
            if (h->freefunc) {
                (*h->freefunc)(pointer);
            }
            *re = h->next;
            FreeList_free(&refelems, h);
        }
    } else {
        LOG_MESSAGE("No reference counting on supplied pointer %x\n",pointer);
        refcount_break(16);
    }
}
Example #3
0
void MonomialHashTable<ValueType>::insert(value m)
{
  long hashval = HASHVALUE(m) & hashmask;
  while (hashtab[hashval]) {
    hashval++;
    nclashes++;
    if (hashval == size)
      hashval = 0;
  }
  hashtab[hashval] = m;
  count++;
  if (count > threshold) grow();
}
Example #4
0
void increase_refcount(void *pointer, void (*freefunc)(void*))
{
    int i=HASHVALUE(pointer);
    RefElem *re= hash_table[i];
    if (!pointer) return;
    while (re && re->pointer!=pointer) re=re->next;
    if (re) {
        re->count++;
        LOG_MESSAGE("setting counter to %i\n", re->count);
    } else {
        LOG_MESSAGE("new pointer %x\n", pointer);
        re=FreeList_malloc(&refelems);
        re->next=hash_table[i];
        re->pointer=pointer;
        re->count=1;
        re->freefunc=freefunc;
        hash_table[i]=re;
    }
}