//Sets key of given id //Returns 1 if id is not in heap //Returns 0 on succcess //Percolates up/down depending on new key's size compared to old key int heap::setKey(const std::string &id, int key) { node* keynode = static_cast<node *> (mapping->getPointer(id)); if (keynode == NULL) return 1; else { int tmpkey = keynode->key; keynode->key = key; if (key < tmpkey) percup(getPos(keynode)); else if (key > tmpkey) { percdown(getPos(keynode)); } return 0; } }
//Inserts given id, key, and pointer as a node into the heap and percolates up //Returns 1 if heap is filled //Returns 2 if heap already contains the id //Returns 0 on Success //Inserts into the mapping the id and a pointer to the nopde int heap::insert(const std::string &id, int key, void *pv) { if (capacity <= 0) return 1; else if (mapping->contains(id) == false) { data[++size].id = id; data[size].key = key; data[size].pData = pv; mapping->insert(id, &data[size]); percup(size); --capacity; return 0; } else return 2; }
//Deletes node with given id, removes from mapping //Returns 1 given id does not exist in heap, returns 0 on success //Replaces deleted node space with node at the end, percolates it up/down int heap::remove(const std::string &id, int *pKey, void *ppData) { node* tmpnode = static_cast<node *> (mapping->getPointer(id)); if (tmpnode == NULL) return 1; int hole = getPos(tmpnode); if (pKey != NULL) *pKey = data[hole].key; if (ppData != NULL) *(static_cast<void **> (ppData)) = data[hole].pData; mapping->remove(data[hole].id); data[hole] = data[size--]; percdown(hole); percup(hole); ++capacity; return 0; }
void push_heap(heap *h, void *v) { ensure_heap(h, h->size + 1); h->buffer[h->size] = v; percup(h, h->size++); }