void UHashTable::Register (void* key, UHashElem* elem) { int n = Hash(key); if (_slot[n] == nil) { _slot[n] = new UList; } if (elem == nil) { elem = CreateElem(); } elem->SetKey(key); _slot[n]->Prepend(new UList(elem)); }
void PairData::AddPair(int id1, int id2, //add a pair to the set. collision_pair *Rcontact, int Rnum_contacts) { int temp_id1 = id1; int exchanged = 0; // Equals 1 iff id1 and id2 are swapped. // It tells whether we need to swap the // triangles' ids in each collision_pair. OrderIds(id1, id2); //order the ids if (temp_id1 != id1 ) exchanged = 1; if (id1 >= size) //increase the size of "arr", if necessary. { int newsize = (id1 >= 2*size) ? (id1+1) : 2*size; Elem **temp = new Elem*[newsize]; int i; for (i=0; i<size; i++) temp[i] = arr[i]; for (i=size; i<newsize; i++) temp[i] = NULL; delete [] arr; arr = temp; size = newsize; } Elem *current = arr[id1]; //select the right list from "arr". if (current == NULL) //if the list is empty, insert the { //element in the front. current = CreateElem( id2, Rcontact, Rnum_contacts, exchanged); current->next = NULL; arr[id1] = current; } else if (current->id > id2) //if the list is not empty but all { //elements are greater than id2, then //insert id2 in the front. current = CreateElem( id2, Rcontact, Rnum_contacts, exchanged); current->next = arr[id1]; arr[id1] = current; } else { while (current->next != NULL) //otherwise, find the correct location { //in the sorted list (ascending order) if (current->next->id > id2) //and insert id2 there. break; current = current->next; } if (current->id == id2) { return; } else { Elem *temp = CreateElem( id2, Rcontact, Rnum_contacts, exchanged); temp->next = current->next; current->next = temp; } } }