예제 #1
0
파일: hashtable.c 프로젝트: Alexoner/dsa
/* ht_search(H, k) returns NULL if key k not present in H */
ht_elem ht_search(ht H, ht_key k)
{
  REQUIRES(is_ht(H));
  int h = H->key_hash(k, H->size);
  list l = H->A[h];
  while (l != NULL)
    //@loop_invariant is_chain(H, l, h);
    {
      if (H->key_equal(H->elem_key(l->data), k))
	return l->data;
      l = l->next;
    }
  return NULL;
}
예제 #2
0
파일: hashtable.c 프로젝트: Alexoner/dsa
void ht_insert(ht H, ht_elem e)
{ REQUIRES(is_ht(H));
  assert(e != NULL);		/* cannot insert NULL element */
  ht_key k = H->elem_key(e);
  int h = H->key_hash(k, H->size);
  list l = H->A[h];
  while (l != NULL)
    //@loop_invariant is_chain(H, l, h);
    {
      if (H->key_equal(H->elem_key(l->data), k)) {
	l->data = e;		/* modify in place if k already there */
	return;
      }
      l = l->next;
    }
  /* k is not already in the hash table */
  /* insert at the beginning of the chain at A[h] */
  H->A[h] = list_new(e, H->A[h]);
  ENSURES(is_ht(H));
  return;
}