2014-01-23 14:43:50 +00:00
|
|
|
package collections
|
|
|
|
|
|
|
|
import (
|
2014-05-13 17:16:06 +00:00
|
|
|
"sort"
|
2014-01-23 14:43:50 +00:00
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
// OrderedIntSet is a thread-safe sorted set and a stack.
|
|
|
|
type OrderedIntSet struct {
|
2014-05-13 17:16:06 +00:00
|
|
|
sync.Mutex
|
2014-01-23 14:43:50 +00:00
|
|
|
set []int
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewOrderedSet returns an initialized OrderedSet
|
|
|
|
func NewOrderedIntSet() *OrderedIntSet {
|
|
|
|
return &OrderedIntSet{}
|
|
|
|
}
|
|
|
|
|
2014-05-13 17:16:06 +00:00
|
|
|
// Push takes an int and adds it to the set. If the elem aready exists, it has no effect.
|
2014-01-23 14:43:50 +00:00
|
|
|
func (s *OrderedIntSet) Push(elem int) {
|
|
|
|
s.Lock()
|
2014-05-13 17:16:06 +00:00
|
|
|
if len(s.set) == 0 {
|
|
|
|
s.set = append(s.set, elem)
|
|
|
|
s.Unlock()
|
|
|
|
return
|
|
|
|
}
|
2014-01-24 01:35:39 +00:00
|
|
|
|
2014-01-23 14:43:50 +00:00
|
|
|
// Make sure the list is always sorted
|
2014-05-13 17:16:06 +00:00
|
|
|
i := sort.SearchInts(s.set, elem)
|
|
|
|
if i < len(s.set) && s.set[i] == elem {
|
|
|
|
s.Unlock()
|
|
|
|
return
|
2014-01-24 01:35:39 +00:00
|
|
|
}
|
2014-05-13 17:16:06 +00:00
|
|
|
s.set = append(s.set[:i], append([]int{elem}, s.set[i:]...)...)
|
2014-01-23 14:43:50 +00:00
|
|
|
s.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pop is an alias to PopFront()
|
|
|
|
func (s *OrderedIntSet) Pop() int {
|
|
|
|
return s.PopFront()
|
|
|
|
}
|
|
|
|
|
2014-05-13 17:16:06 +00:00
|
|
|
// Pop returns the first element from the list and removes it.
|
2014-01-23 14:43:50 +00:00
|
|
|
// If the list is empty, it returns 0
|
|
|
|
func (s *OrderedIntSet) PopFront() int {
|
2014-05-13 17:16:06 +00:00
|
|
|
s.Lock()
|
|
|
|
if len(s.set) == 0 {
|
2014-01-23 14:43:50 +00:00
|
|
|
s.Unlock()
|
2014-05-13 17:16:06 +00:00
|
|
|
return 0
|
2014-01-23 14:43:50 +00:00
|
|
|
}
|
2014-05-13 17:16:06 +00:00
|
|
|
ret := s.set[0]
|
|
|
|
s.set = s.set[1:]
|
|
|
|
s.Unlock()
|
|
|
|
return ret
|
2014-01-23 14:43:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PullBack retrieve the last element of the list.
|
|
|
|
// The element is not removed.
|
|
|
|
// If the list is empty, an empty element is returned.
|
|
|
|
func (s *OrderedIntSet) PullBack() int {
|
2014-05-13 17:16:06 +00:00
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
2014-01-23 14:43:50 +00:00
|
|
|
if len(s.set) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return s.set[len(s.set)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exists checks if the given element present in the list.
|
|
|
|
func (s *OrderedIntSet) Exists(elem int) bool {
|
2014-05-13 17:16:06 +00:00
|
|
|
s.Lock()
|
|
|
|
if len(s.set) == 0 {
|
|
|
|
s.Unlock()
|
|
|
|
return false
|
2014-01-23 14:43:50 +00:00
|
|
|
}
|
2014-05-13 17:16:06 +00:00
|
|
|
i := sort.SearchInts(s.set, elem)
|
|
|
|
res := i < len(s.set) && s.set[i] == elem
|
|
|
|
s.Unlock()
|
|
|
|
return res
|
2014-01-23 14:43:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove removes an element from the list.
|
|
|
|
// If the element is not found, it has no effect.
|
|
|
|
func (s *OrderedIntSet) Remove(elem int) {
|
2014-05-13 17:16:06 +00:00
|
|
|
s.Lock()
|
|
|
|
if len(s.set) == 0 {
|
|
|
|
s.Unlock()
|
|
|
|
return
|
2014-01-23 14:43:50 +00:00
|
|
|
}
|
2014-05-13 17:16:06 +00:00
|
|
|
i := sort.SearchInts(s.set, elem)
|
|
|
|
if i < len(s.set) && s.set[i] == elem {
|
|
|
|
s.set = append(s.set[:i], s.set[i+1:]...)
|
|
|
|
}
|
|
|
|
s.Unlock()
|
2014-01-23 14:43:50 +00:00
|
|
|
}
|