2014-01-23 09:43:50 -05:00
|
|
|
package collections
|
2014-01-22 19:00:53 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2014-01-23 09:43:50 -05:00
|
|
|
// OrderedIntSet is a thread-safe sorted set and a stack.
|
|
|
|
type OrderedIntSet struct {
|
2014-01-22 19:00:53 -05:00
|
|
|
sync.RWMutex
|
2014-01-22 21:05:20 -05:00
|
|
|
set []int
|
2014-01-22 19:00:53 -05:00
|
|
|
}
|
|
|
|
|
2014-01-23 09:43:50 -05:00
|
|
|
// NewOrderedSet returns an initialized OrderedSet
|
|
|
|
func NewOrderedIntSet() *OrderedIntSet {
|
|
|
|
return &OrderedIntSet{}
|
|
|
|
}
|
|
|
|
|
2014-01-22 19:00:53 -05:00
|
|
|
// Push takes a string and adds it to the set. If the elem aready exists, it has no effect.
|
2014-01-23 09:43:50 -05:00
|
|
|
func (s *OrderedIntSet) Push(elem int) {
|
2014-01-22 19:00:53 -05:00
|
|
|
s.RLock()
|
2014-01-22 21:05:20 -05:00
|
|
|
for _, e := range s.set {
|
2014-01-22 19:00:53 -05:00
|
|
|
if e == elem {
|
|
|
|
s.RUnlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.RUnlock()
|
|
|
|
|
|
|
|
s.Lock()
|
2014-01-23 20:35:39 -05:00
|
|
|
|
2014-01-22 19:00:53 -05:00
|
|
|
// Make sure the list is always sorted
|
2014-01-23 20:35:39 -05:00
|
|
|
for i, e := range s.set {
|
|
|
|
if elem < e {
|
|
|
|
s.set = append(s.set[:i], append([]int{elem}, s.set[i:]...)...)
|
|
|
|
s.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If we reach here, then elem is the biggest elem of the list.
|
|
|
|
s.set = append(s.set, elem)
|
2014-01-22 19:00:53 -05:00
|
|
|
s.Unlock()
|
|
|
|
}
|
|
|
|
|
2014-01-22 20:12:28 -05:00
|
|
|
// Pop is an alias to PopFront()
|
2014-01-23 09:43:50 -05:00
|
|
|
func (s *OrderedIntSet) Pop() int {
|
2014-01-22 20:12:28 -05:00
|
|
|
return s.PopFront()
|
|
|
|
}
|
|
|
|
|
2014-01-22 19:00:53 -05:00
|
|
|
// Pop returns the first elemen from the list and removes it.
|
2014-01-22 20:20:19 -05:00
|
|
|
// If the list is empty, it returns 0
|
2014-01-23 09:43:50 -05:00
|
|
|
func (s *OrderedIntSet) PopFront() int {
|
2014-01-22 19:00:53 -05:00
|
|
|
s.RLock()
|
|
|
|
|
|
|
|
for i, e := range s.set {
|
|
|
|
ret := e
|
|
|
|
s.RUnlock()
|
|
|
|
s.Lock()
|
|
|
|
s.set = append(s.set[:i], s.set[i+1:]...)
|
|
|
|
s.Unlock()
|
2014-01-22 21:05:20 -05:00
|
|
|
return ret
|
2014-01-22 19:00:53 -05:00
|
|
|
}
|
|
|
|
s.RUnlock()
|
2014-01-22 21:05:20 -05:00
|
|
|
|
|
|
|
return 0
|
2014-01-22 19:00:53 -05:00
|
|
|
}
|
|
|
|
|
2014-01-22 20:12:28 -05:00
|
|
|
// PullBack retrieve the last element of the list.
|
|
|
|
// The element is not removed.
|
|
|
|
// If the list is empty, an empty element is returned.
|
2014-01-23 09:43:50 -05:00
|
|
|
func (s *OrderedIntSet) PullBack() int {
|
2014-01-22 20:12:28 -05:00
|
|
|
if len(s.set) == 0 {
|
2014-01-22 21:05:20 -05:00
|
|
|
return 0
|
2014-01-22 20:12:28 -05:00
|
|
|
}
|
|
|
|
return s.set[len(s.set)-1]
|
|
|
|
}
|
|
|
|
|
2014-01-22 19:13:28 -05:00
|
|
|
// Exists checks if the given element present in the list.
|
2014-01-23 09:43:50 -05:00
|
|
|
func (s *OrderedIntSet) Exists(elem int) bool {
|
2014-01-22 19:13:28 -05:00
|
|
|
for _, e := range s.set {
|
|
|
|
if e == elem {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-01-22 19:00:53 -05:00
|
|
|
// Remove removes an element from the list.
|
|
|
|
// If the element is not found, it has no effect.
|
2014-01-23 09:43:50 -05:00
|
|
|
func (s *OrderedIntSet) Remove(elem int) {
|
2014-01-22 19:00:53 -05:00
|
|
|
for i, e := range s.set {
|
|
|
|
if e == elem {
|
|
|
|
s.set = append(s.set[:i], s.set[i+1:]...)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|