Permalink
Browse files

GoFmt FTW

  • Loading branch information...
1 parent 0c1dbb7 commit 2b759a822f810627acb1f724d7f65f9db7a9bbd9 @ajtulloch committed Jan 9, 2013
Showing with 74 additions and 73 deletions.
  1. +52 −52 unorderedset.go
  2. +22 −21 unorderedset_test.go
View
@@ -1,92 +1,92 @@
package godatastruct
import (
- "sync"
+ "sync"
)
type Set interface {
- Set(key interface{}) bool
- Exists(key interface{}) bool
- Erase(key interface{}) bool
- Len() int
- Clear()
+ Set(key interface{}) bool
+ Exists(key interface{}) bool
+ Erase(key interface{}) bool
+ Len() int
+ Clear()
}
type unorderedSet struct {
- elementMap map[interface{}] bool
+ elementMap map[interface{}]bool
}
func NewUnorderedSet() Set {
- return &unorderedSet{make(map[interface{}] bool)}
+ return &unorderedSet{make(map[interface{}]bool)}
}
-func (s* unorderedSet) Exists(key interface{}) bool {
- _, exists := s.elementMap[key]
- return exists
+func (s *unorderedSet) Exists(key interface{}) bool {
+ _, exists := s.elementMap[key]
+ return exists
}
-func (s* unorderedSet) Set(key interface{}) bool {
- if s.Exists(key) {
- return false
- }
+func (s *unorderedSet) Set(key interface{}) bool {
+ if s.Exists(key) {
+ return false
+ }
- s.elementMap[key] = true
- return true
+ s.elementMap[key] = true
+ return true
}
-func (s* unorderedSet) Erase(key interface{}) bool {
- existed := s.Exists(key)
- delete(s.elementMap, key)
- return existed
+func (s *unorderedSet) Erase(key interface{}) bool {
+ existed := s.Exists(key)
+ delete(s.elementMap, key)
+ return existed
}
-func (s* unorderedSet) Len() int {
- return len(s.elementMap)
+func (s *unorderedSet) Len() int {
+ return len(s.elementMap)
}
-func (s* unorderedSet) Clear() {
- for key := range s.elementMap {
- delete(s.elementMap, key)
- }
+func (s *unorderedSet) Clear() {
+ for key := range s.elementMap {
+ delete(s.elementMap, key)
+ }
}
type threadSafeUnorderedSet struct {
- mutex sync.RWMutex
- unorderedSet unorderedSet
+ mutex sync.RWMutex
+ unorderedSet unorderedSet
}
func NewTheadSafeUnorderedSet() Set {
- s:= new(threadSafeUnorderedSet)
- s.unorderedSet = unorderedSet{make(map[interface{}] bool)}
- return s
+ s := new(threadSafeUnorderedSet)
+ s.unorderedSet = unorderedSet{make(map[interface{}]bool)}
+ return s
}
-func (s* threadSafeUnorderedSet) Exists(key interface{}) bool {
- s.mutex.RLock()
- defer s.mutex.RUnlock()
- return s.unorderedSet.Exists(key)
+func (s *threadSafeUnorderedSet) Exists(key interface{}) bool {
+ s.mutex.RLock()
+ defer s.mutex.RUnlock()
+ return s.unorderedSet.Exists(key)
}
-func (s* threadSafeUnorderedSet) Set(key interface{}) bool {
- s.mutex.Lock()
- defer s.mutex.Unlock()
- return s.unorderedSet.Set(key)
+func (s *threadSafeUnorderedSet) Set(key interface{}) bool {
+ s.mutex.Lock()
+ defer s.mutex.Unlock()
+ return s.unorderedSet.Set(key)
}
-func (s* threadSafeUnorderedSet) Erase(key interface{}) bool {
- s.mutex.Lock()
- defer s.mutex.Unlock()
- return s.unorderedSet.Erase(key)
+func (s *threadSafeUnorderedSet) Erase(key interface{}) bool {
+ s.mutex.Lock()
+ defer s.mutex.Unlock()
+ return s.unorderedSet.Erase(key)
}
-func (s* threadSafeUnorderedSet) Len() int {
- s.mutex.RLock()
- defer s.mutex.RUnlock()
- return s.unorderedSet.Len()
+func (s *threadSafeUnorderedSet) Len() int {
+ s.mutex.RLock()
+ defer s.mutex.RUnlock()
+ return s.unorderedSet.Len()
}
-func (s* threadSafeUnorderedSet) Clear() {
- s.mutex.Lock()
- defer s.mutex.Unlock()
- s.unorderedSet.Clear()
+func (s *threadSafeUnorderedSet) Clear() {
+ s.mutex.Lock()
+ defer s.mutex.Unlock()
+ s.unorderedSet.Clear()
}
View
@@ -1,40 +1,41 @@
package godatastruct
import (
- . "launchpad.net/gocheck"
- "testing"
+ . "launchpad.net/gocheck"
+ "testing"
)
// Hook up gocheck into the gotest runner.
func Test(t *testing.T) { TestingT(t) }
type MySuite struct{}
+
var _ = Suite(&MySuite{})
func setTests(c *C, s Set) {
- c.Assert(s.Set(1), Equals, true)
- c.Assert(s.Exists(1), Equals, true)
- c.Assert(s.Exists(2), Equals, false)
- c.Assert(s.Len(), Equals, 1)
- s.Clear()
-
- c.Assert(s.Len(), Equals, 0)
- c.Assert(s.Exists(1), Equals, false)
-
- c.Assert(s.Set(1), Equals, true)
- c.Assert(s.Set("Cats"), Equals, true)
- c.Assert(s.Len(), Equals, 2)
- c.Assert(s.Erase("Cats"), Equals, true)
- c.Assert(s.Erase("Dogs"), Equals, false)
- c.Assert(s.Len(), Equals, 1)
+ c.Assert(s.Set(1), Equals, true)
+ c.Assert(s.Exists(1), Equals, true)
+ c.Assert(s.Exists(2), Equals, false)
+ c.Assert(s.Len(), Equals, 1)
+ s.Clear()
+
+ c.Assert(s.Len(), Equals, 0)
+ c.Assert(s.Exists(1), Equals, false)
+
+ c.Assert(s.Set(1), Equals, true)
+ c.Assert(s.Set("Cats"), Equals, true)
+ c.Assert(s.Len(), Equals, 2)
+ c.Assert(s.Erase("Cats"), Equals, true)
+ c.Assert(s.Erase("Dogs"), Equals, false)
+ c.Assert(s.Len(), Equals, 1)
}
func (s *MySuite) TestUnorderedSet(c *C) {
- us := NewUnorderedSet()
- setTests(c, us)
+ us := NewUnorderedSet()
+ setTests(c, us)
}
func (s *MySuite) TestThreadSafeUnorderedSet(c *C) {
- us:= NewTheadSafeUnorderedSet()
- setTests(c, us)
+ us := NewTheadSafeUnorderedSet()
+ setTests(c, us)
}

0 comments on commit 2b759a8

Please sign in to comment.