added remove and len functions

master
Pixdigit 4 years ago
parent 45b487eb32
commit 307731ea11

@ -13,15 +13,30 @@ func TestList(t *testing.T) {
list.Insert("c", 3)
list.Insert("d", 5)
list.Insert("e", 4)
value, err := list.Nearest(3.2); if err != nil {tools.WrapErr(err, "could not get element from list", t)}
value, err := list.Nearest(3.2)
if err != nil {
tools.WrapErr(err, "could not get element from list", t)
}
tools.Test(value == "c", "did not get corrent element", t)
value, err = list.Nearest(3.8); if err != nil {tools.WrapErr(err, "could not get element from list", t)}
value, err = list.Nearest(3.8)
if err != nil {
tools.WrapErr(err, "could not get element from list", t)
}
tools.Test(value == "e", "did not get corrent element", t)
value, err = list.Nearest(8); if err != nil {tools.WrapErr(err, "could not get element from list", t)}
value, err = list.Nearest(8)
if err != nil {
tools.WrapErr(err, "could not get element from list", t)
}
tools.Test(value == "d", "did not get corrent element", t)
value, err = list.Nearest(-2); if err != nil {tools.WrapErr(err, "could not get element from list", t)}
value, err = list.Nearest(-2)
if err != nil {
tools.WrapErr(err, "could not get element from list", t)
}
tools.Test(value == "a", "did not get corrent element", t)
value, err = list.Nearest(2); if err != nil {tools.WrapErr(err, "could not get element from list", t)}
value, err = list.Nearest(2)
if err != nil {
tools.WrapErr(err, "could not get element from list", t)
}
tools.Test(value == "b", "did not get corrent element", t)
t.Log(list.String())

@ -8,7 +8,7 @@ import (
// Set is optimized for Set.Elems() to be fast.
// This is why it doesnt implement the functionality with a map
// This of course makes all other operations slow
// This of course makes almost all other operations slow
type Set struct {
elems []*setElem
@ -127,9 +127,26 @@ func (s *Set) Elems() []interface{} {
return copy
}
func (s *Set) Contains(testID uniqueID.ID) bool {
func (s *Set) Len() int {
return len(s.elems)
}
func (s *Set) Contains(testElem uniqueElem) bool {
elemID := testElem.ID()
for _, elem := range s.elems {
if elem.ID() == testID {
if elem.ID() == elemID {
return true
}
}
return false
}
func (s *Set) Remove(elemToRemove uniqueElem) (success bool) {
elemID := elemToRemove.ID()
for i, elem := range s.elems {
if elem.ID() == elemID {
s.elems = append(s.elems[:i], s.elems[i+1:]...)
//element can only occur once so exit if one has been found
return true
}
}

@ -12,18 +12,23 @@ type testElem struct {
Name string
}
var lastID uniqueID.ID
type identifiable interface {
ID() uniqueID.ID
}
var lastElem identifiable
func NewSampleElem(IDSpace *uniqueID.IDSpace, name string) *testElem {
newID := IDSpace.NewID()
lastID = newID
return &testElem{
elem := testElem{
newID,
name,
}
lastElem = elem
return &elem
}
func (te *testElem) ID() uniqueID.ID {
return te.id
func (t testElem) ID() uniqueID.ID {
return t.id
}
func TestNearest(t *testing.T) {
@ -31,23 +36,55 @@ func TestNearest(t *testing.T) {
testIDSpace := &uniqueID.IDSpace{}
set := Set{}
err := set.Insert(NewSampleElem(testIDSpace, "a"), 1); if err != nil {tools.WrapErr(err, "could not insert test element", t)}
err = set.Insert(NewSampleElem(testIDSpace, "b"), 2); if err != nil {tools.WrapErr(err, "could not insert test element", t)}
err = set.Insert(NewSampleElem(testIDSpace, "c"), 3); if err != nil {tools.WrapErr(err, "could not insert test element", t)}
err = set.Insert(NewSampleElem(testIDSpace, "d"), 5); if err != nil {tools.WrapErr(err, "could not insert test element", t)}
err = set.Insert(NewSampleElem(testIDSpace, "e"), 4); if err != nil {tools.WrapErr(err, "could not insert test element", t)}
value, err := set.Nearest(3.2); if err != nil {tools.WrapErr(err, "could not get element from set", t)}
err := set.Insert(NewSampleElem(testIDSpace, "a"), 1)
if err != nil {
tools.WrapErr(err, "could not insert test element", t)
}
err = set.Insert(NewSampleElem(testIDSpace, "b"), 2)
if err != nil {
tools.WrapErr(err, "could not insert test element", t)
}
err = set.Insert(NewSampleElem(testIDSpace, "c"), 3)
if err != nil {
tools.WrapErr(err, "could not insert test element", t)
}
err = set.Insert(NewSampleElem(testIDSpace, "d"), 5)
if err != nil {
tools.WrapErr(err, "could not insert test element", t)
}
err = set.Insert(NewSampleElem(testIDSpace, "e"), 4)
if err != nil {
tools.WrapErr(err, "could not insert test element", t)
}
value, err := set.Nearest(3.2)
if err != nil {
tools.WrapErr(err, "could not get element from set", t)
}
tools.Test(value.(*testElem).Name == "c", "did not get corrent element", t)
value, err = set.Nearest(3.8); if err != nil {tools.WrapErr(err, "could not get element from set", t)}
value, err = set.Nearest(3.8)
if err != nil {
tools.WrapErr(err, "could not get element from set", t)
}
tools.Test(value.(*testElem).Name == "e", "did not get corrent element", t)
value, err = set.Nearest(8); if err != nil {tools.WrapErr(err, "could not get element from set", t)}
value, err = set.Nearest(8)
if err != nil {
tools.WrapErr(err, "could not get element from set", t)
}
tools.Test(value.(*testElem).Name == "d", "did not get corrent element", t)
value, err = set.Nearest(-2); if err != nil {tools.WrapErr(err, "could not get element from set", t)}
value, err = set.Nearest(-2)
if err != nil {
tools.WrapErr(err, "could not get element from set", t)
}
tools.Test(value.(*testElem).Name == "a", "did not get corrent element", t)
value, err = set.Nearest(2); if err != nil {tools.WrapErr(err, "could not get element from set", t)}
value, err = set.Nearest(2)
if err != nil {
tools.WrapErr(err, "could not get element from set", t)
}
tools.Test(value.(*testElem).Name == "b", "did not get corrent element", t)
tools.Test(set.Contains(lastID), "set did not contain added element", t)
tools.Test(set.Contains(lastElem), "set did not contain added element", t)
tools.Test(set.Remove(lastElem), "could not remove element in set", t)
tools.Test(set.Len() == 4, "set length not correct", t)
t.Log(set.String())
}
@ -62,11 +99,26 @@ func TestSetValue(t *testing.T) {
d := NewSampleElem(testIDSpace, "d")
e := NewSampleElem(testIDSpace, "e")
err := set.Insert(a, 1); if err != nil {tools.WrapErr(err, "could not insert test element", t)}
err = set.Insert(b, 2); if err != nil {tools.WrapErr(err, "could not insert test element", t)}
err = set.Insert(c, 3); if err != nil {tools.WrapErr(err, "could not insert test element", t)}
err = set.Insert(d, 5); if err != nil {tools.WrapErr(err, "could not insert test element", t)}
err = set.Insert(e, 4); if err != nil {tools.WrapErr(err, "could not insert test element", t)}
err := set.Insert(a, 1)
if err != nil {
tools.WrapErr(err, "could not insert test element", t)
}
err = set.Insert(b, 2)
if err != nil {
tools.WrapErr(err, "could not insert test element", t)
}
err = set.Insert(c, 3)
if err != nil {
tools.WrapErr(err, "could not insert test element", t)
}
err = set.Insert(d, 5)
if err != nil {
tools.WrapErr(err, "could not insert test element", t)
}
err = set.Insert(e, 4)
if err != nil {
tools.WrapErr(err, "could not insert test element", t)
}
set.SetValue(a.ID(), 3.5)
set.SetValue(e.ID(), -2)

Loading…
Cancel
Save