// Put put value with priority into queue
Put put value with priority into queue
// Put put value with priority into queue
func (pq *PriorityQueue) Put(priority int, value PriorityQueueItem) bool {
	id := value.ID()
	entry, ok := pq.items[id]
	if !ok {
		entry = &Entry{Priority: priority, Value: value}
		if pq.Len() >= pq.capacity {
			min := pq.btree.Min()
			if min == nil || !min.Less(entry) {
				return false
			}
			pq.Remove(min.(*Entry).Value.ID())
		}
	} else {
		if entry.Priority != priority {
			pq.btree.Delete(entry)
			entry.Priority = priority
		}
	}
	pq.btree.ReplaceOrInsert(entry)
	pq.items[id] = entry
	return true
}
func ( pq * PriorityQueue ) Put ( priority int , value PriorityQueueItem ) bool { id := value . ID ( ) entry , ok := pq . items [ id ] if ! ok { entry = & Entry { Priority : priority , Value : value } if pq . Len ( ) >= pq . capacity { min := pq . btree . Min ( ) if min == nil || ! min . Less ( entry ) { return false } pq . Remove ( min . ( * Entry ) . Value . ID ( ) ) } } else { if entry . Priority != priority { pq . btree . Delete ( entry ) entry . Priority = priority } } pq . btree . ReplaceOrInsert ( entry ) pq . items [ id ] = entry return true }
Put put value with priority into queue
Put put value with priority into queue
// avoid to capacity equal 0 // delete before update