Add inserts an item, and puts it in the queue. The item is updated if it already exists.
(obj interface{})
| 139 | // Add inserts an item, and puts it in the queue. The item is updated if it |
| 140 | // already exists. |
| 141 | func (h *Heap) Add(obj interface{}) error { |
| 142 | key, err := h.data.keyFunc(obj) |
| 143 | if err != nil { |
| 144 | return KeyError{obj, err} |
| 145 | } |
| 146 | h.lock.Lock() |
| 147 | defer h.lock.Unlock() |
| 148 | if h.closed { |
| 149 | return fmt.Errorf(closedMsg) |
| 150 | } |
| 151 | if _, exists := h.data.items[key]; exists { |
| 152 | h.data.items[key].obj = obj |
| 153 | heap.Fix(h.data, h.data.items[key].index) |
| 154 | } else { |
| 155 | h.addIfNotPresentLocked(key, obj) |
| 156 | } |
| 157 | h.cond.Broadcast() |
| 158 | return nil |
| 159 | } |
| 160 | |
| 161 | // Adds all the items in the list to the queue and then signals the condition |
| 162 | // variable. It is useful when the caller would like to add all of the items |