From a09f2d1c418f42521bec67190ec8096f584e61b0 Mon Sep 17 00:00:00 2001 From: Alexandr Morozov Date: Wed, 28 May 2014 13:55:10 +0400 Subject: [PATCH] Remove collections package MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It doesn't needed anymore аfter port and ip allocators refactoring Docker-DCO-1.1-Signed-off-by: Alexandr Morozov (github: LK4D4) --- collections/orderedintset.go | 95 ------------------------------- collections/orderedintset_test.go | 71 ----------------------- 2 files changed, 166 deletions(-) delete mode 100644 collections/orderedintset.go delete mode 100644 collections/orderedintset_test.go diff --git a/collections/orderedintset.go b/collections/orderedintset.go deleted file mode 100644 index 7442f2e..0000000 --- a/collections/orderedintset.go +++ /dev/null @@ -1,95 +0,0 @@ -package collections - -import ( - "sort" - "sync" -) - -// OrderedIntSet is a thread-safe sorted set and a stack. -type OrderedIntSet struct { - sync.Mutex - set []int -} - -// NewOrderedSet returns an initialized OrderedSet -func NewOrderedIntSet() *OrderedIntSet { - return &OrderedIntSet{} -} - -// Push takes an int and adds it to the set. If the elem aready exists, it has no effect. -func (s *OrderedIntSet) Push(elem int) { - s.Lock() - if len(s.set) == 0 { - s.set = append(s.set, elem) - s.Unlock() - return - } - - // Make sure the list is always sorted - i := sort.SearchInts(s.set, elem) - if i < len(s.set) && s.set[i] == elem { - s.Unlock() - return - } - s.set = append(s.set[:i], append([]int{elem}, s.set[i:]...)...) - s.Unlock() -} - -// Pop is an alias to PopFront() -func (s *OrderedIntSet) Pop() int { - return s.PopFront() -} - -// Pop returns the first element from the list and removes it. -// If the list is empty, it returns 0 -func (s *OrderedIntSet) PopFront() int { - s.Lock() - if len(s.set) == 0 { - s.Unlock() - return 0 - } - ret := s.set[0] - s.set = s.set[1:] - s.Unlock() - return ret -} - -// 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 { - s.Lock() - defer s.Unlock() - 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 { - s.Lock() - if len(s.set) == 0 { - s.Unlock() - return false - } - i := sort.SearchInts(s.set, elem) - res := i < len(s.set) && s.set[i] == elem - s.Unlock() - return res -} - -// Remove removes an element from the list. -// If the element is not found, it has no effect. -func (s *OrderedIntSet) Remove(elem int) { - s.Lock() - if len(s.set) == 0 { - s.Unlock() - return - } - 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() -} diff --git a/collections/orderedintset_test.go b/collections/orderedintset_test.go deleted file mode 100644 index 0ac4ca5..0000000 --- a/collections/orderedintset_test.go +++ /dev/null @@ -1,71 +0,0 @@ -package collections - -import ( - "math/rand" - "testing" -) - -func BenchmarkPush(b *testing.B) { - var testSet []int - for i := 0; i < 1000; i++ { - testSet = append(testSet, rand.Int()) - } - s := NewOrderedIntSet() - b.ResetTimer() - for i := 0; i < b.N; i++ { - for _, elem := range testSet { - s.Push(elem) - } - } -} - -func BenchmarkPop(b *testing.B) { - var testSet []int - for i := 0; i < 1000; i++ { - testSet = append(testSet, rand.Int()) - } - s := NewOrderedIntSet() - for _, elem := range testSet { - s.Push(elem) - } - b.ResetTimer() - for i := 0; i < b.N; i++ { - for j := 0; j < 1000; j++ { - s.Pop() - } - } -} - -func BenchmarkExist(b *testing.B) { - var testSet []int - for i := 0; i < 1000; i++ { - testSet = append(testSet, rand.Intn(2000)) - } - s := NewOrderedIntSet() - for _, elem := range testSet { - s.Push(elem) - } - b.ResetTimer() - for i := 0; i < b.N; i++ { - for j := 0; j < 1000; j++ { - s.Exists(j) - } - } -} - -func BenchmarkRemove(b *testing.B) { - var testSet []int - for i := 0; i < 1000; i++ { - testSet = append(testSet, rand.Intn(2000)) - } - s := NewOrderedIntSet() - for _, elem := range testSet { - s.Push(elem) - } - b.ResetTimer() - for i := 0; i < b.N; i++ { - for j := 0; j < 1000; j++ { - s.Remove(j) - } - } -}