/** * Remove t from queue. * * @param t */ public void remove(T t) { SHMEntry<T, Value<U>> e = map.getEntry(t); if (e == null) return; int i = e.getValue().idx; U ou = heap.get(i).getValue().val; U u = heap.get(heap.size() - 1).getValue().val; swap(i, heap.size() - 1); map.remove(heap.get(heap.size() - 1)); heap.remove(heap.size() - 1); if (u.compareTo(ou) < 0) { // new key in i is smaller bubble_up(i); } else { // new key in i is larger bubble_down(i); } }
/** * Insert new items to queue. If t is already in queue, modifies its value and adjusts heap * appropriately * * @param t item to add */ public void put(T t, U u) { SHMEntry<T, Value<U>> e = map.getEntry(t); if (e == null) { Value<U> v = new Value<U>(); v.val = u; v.idx = heap.size(); e = map.put(t, v); heap.add(e); bubble_up(heap.size() - 1); } else { U ou = e.getValue().val; e.getValue().val = u; if (ou.compareTo(u) < 0) { // new value is larger, bubble down bubble_down(e.getValue().idx); } else { // new value is smaller, bubble up bubble_up(e.getValue().idx); } } }
private int cmp(int x, int y) { U a = heap.get(x).getValue().val; U b = heap.get(y).getValue().val; return a.compareTo(b); }