diff --git a/list/arraylist/arraylist.go b/list/arraylist/arraylist.go new file mode 100644 index 0000000..f82fb7c --- /dev/null +++ b/list/arraylist/arraylist.go @@ -0,0 +1,107 @@ +package arraylist + +type ArrayList struct { + data []interface{} + + headIndex uint + nextIndex uint + + size uint + + reserveHead uint + reserveLimit uint + + growSize uint + shrinkSize uint +} + +func New() *ArrayList { + al := &ArrayList{} + al.reserveHead = 2 + al.reserveLimit = 256 + al.headIndex = al.reserveHead + al.nextIndex = al.headIndex + + al.data = make([]interface{}, 8, 8) + return al +} + +func (l *ArrayList) Size() uint { + return l.size +} + +func (l *ArrayList) grow() { + newsize := uint(len(l.data)) << 1 + + l.reserveHead = al.reserveHead << 1 + + l.headIndex = al.reserveHead + l.nextIndex = al.headIndex + + l.data = make([]interface{}, 8, 8) +} + +// Add add value to the tail of list +func (l *ArrayList) Add(v interface{}) { + + if l.nextIndex >= uint(len(l.data)) { + l.grow() + } + + l.size++ + l.data[l.nextIndex] = v + + // grow +} + +// Push push is equal to add +func (l *ArrayList) Push(v interface{}) { + l.data = append(l.data, v) +} + +func (l *ArrayList) Set(idx uint, value interface{}) { + l.data[idx] = value +} + +func (l *ArrayList) Get(idx uint) (result interface{}, isfound bool) { + if idx >= l.Size() { + return nil, false + } + return l.data[idx], true +} + +func (l *ArrayList) Pop() (result interface{}, found bool) { + if l.Size() == 0 { + return nil, false + } + rindex := len(l.data) - 1 + result = l.data[rindex] + l.data = l.data[0:rindex] + return result, true +} + +func (l *ArrayList) Remove(idx uint) (rvalue interface{}, isfound bool) { + + if idx >= l.Size() { + return nil, false + } + + rvalue = l.data[idx] + l.data = append(l.data[0:idx], l.data[idx+1:]) + + return rvalue, true +} + +func (l *ArrayList) Values() (result []interface{}) { + values := make([]interface{}, l.Size(), l.Size()) + copy(values, l.data) + return values +} + +func (l *ArrayList) Traversal(every func(index int, cur interface{}) bool) { + for i, cur := range l.data { + if !every(i, cur) { + return + } + } +} diff --git a/list/list_test.go b/list/arraylist/arraylist_test.go similarity index 97% rename from list/list_test.go rename to list/arraylist/arraylist_test.go index da0ce10..92ea55d 100644 --- a/list/list_test.go +++ b/list/arraylist/arraylist_test.go @@ -1,4 +1,4 @@ -package list +package arraylist import ( "testing" @@ -73,7 +73,7 @@ func TestRemove(t *testing.T) { for i := 0; i < 5; i++ { if n, ok := l.Remove(0); ok { - if n.Value() != 4-i { + if n != 4-i { t.Error(n) } } else { diff --git a/list/list.go b/list/list.go deleted file mode 100644 index 6c1b29c..0000000 --- a/list/list.go +++ /dev/null @@ -1,108 +0,0 @@ -package list - -type Node struct { - next *Node - value interface{} -} - -func (node *Node) Value() interface{} { - return node.value -} - -type List struct { - head *Node - size uint -} - -func New() *List { - return &List{} -} - -func (l *List) Size() uint { - return l.size -} - -func (l *List) Push(v interface{}) { - l.size++ - if l.head == nil { - l.head = &Node{value: v} - return - } - l.head = &Node{value: v, next: l.head} -} - -func (l *List) PushNode(n *Node) { - l.size++ - if l.head == nil { - l.head = n - return - } - - n.next = l.head - l.head = n -} - -func (l *List) Pop() (result interface{}, found bool) { - if n, ok := l.PopNode(); ok { - return n.value, ok - } - return nil, false -} - -func (l *List) PopNode() (result *Node, found bool) { - if l.head == nil { - return nil, false - } - - result = l.head - found = true - l.head = result.next - result.next = nil - l.size-- - return -} - -func (l *List) Remove(idx uint) (result *Node, found bool) { - if l.size == 0 { - return nil, false - } - - if idx == 0 { - result = l.head - found = true - l.head = result.next - result.next = nil - l.size-- - return - } - - for cur := l.head; cur.next != nil; cur = cur.next { - if idx == 1 { - l.size-- - result = cur.next - found = true - cur.next = result.next - result.next = nil - return - } - idx-- - } - - return nil, false -} - -func (l *List) Values() (result []interface{}) { - l.Traversal(func(cur *Node) bool { - result = append(result, cur.value) - return true - }) - return -} - -func (l *List) Traversal(every func(*Node) bool) { - for cur := l.head; cur != nil; cur = cur.next { - if !every(cur) { - break - } - } -} diff --git a/map/hashmap/hashmap.go b/map/hashmap/hashmap.go index 56cd19f..3b3a147 100644 --- a/map/hashmap/hashmap.go +++ b/map/hashmap/hashmap.go @@ -58,6 +58,6 @@ func (hm *HashMap) Clear() { } func (hm *HashMap) String() string { - str := fmt.Sprintf("%v", hm.hm) - return str + content := fmt.Sprintf("%v", hm.hm) + return content } diff --git a/priority_list/iterator.go b/priority_list/iterator.go deleted file mode 100644 index cc1738d..0000000 --- a/priority_list/iterator.go +++ /dev/null @@ -1,87 +0,0 @@ -package plist - -type Iterator struct { - pl *PriorityList - cur *Node -} - -func (iter *Iterator) Value() interface{} { - return iter.cur.value -} - -func (iter *Iterator) Prev() bool { - if iter.cur == iter.pl.head { - return false - } - iter.cur = iter.cur.prev - return iter.cur != iter.pl.head -} - -func (iter *Iterator) Next() bool { - if iter.cur == iter.pl.tail { - return false - } - iter.cur = iter.cur.next - return iter.cur != iter.pl.tail -} - -func (iter *Iterator) MoveToHead() { - iter.cur = iter.pl.head -} - -func (iter *Iterator) MoveToTail() { - iter.cur = iter.pl.tail -} - -type CircularIterator struct { - pl *PriorityList - cur *Node -} - -func (iter *CircularIterator) Value() interface{} { - return iter.cur.value -} - -func (iter *CircularIterator) Prev() bool { - if iter.pl.size == 0 { - return false - } - - if iter.cur == iter.pl.head { - iter.cur = iter.pl.tail.prev - return true - } - - iter.cur = iter.cur.prev - if iter.cur == iter.pl.head { - iter.cur = iter.pl.tail.prev - } - - return true -} - -func (iter *CircularIterator) Next() bool { - if iter.pl.size == 0 { - return false - } - - if iter.cur == iter.pl.tail { - iter.cur = iter.pl.head.next - return true - } - - iter.cur = iter.cur.next - if iter.cur == iter.pl.tail { - iter.cur = iter.pl.head.next - } - - return true -} - -func (iter *CircularIterator) MoveToHead() { - iter.cur = iter.pl.head -} - -func (iter *CircularIterator) MoveToTail() { - iter.cur = iter.pl.tail -} diff --git a/priority_list/priority_list.go b/priority_list/priority_list.go deleted file mode 100644 index bb5897b..0000000 --- a/priority_list/priority_list.go +++ /dev/null @@ -1,165 +0,0 @@ -package plist - -import ( - "strings" - - "github.com/474420502/focus/compare" - "github.com/davecgh/go-spew/spew" -) - -type Node struct { - prev, next *Node - value interface{} -} - -type PriorityList struct { - head, tail *Node - size int - Compare compare.Compare -} - -func New(Compare compare.Compare) *PriorityList { - pl := &PriorityList{head: &Node{}, tail: &Node{}, size: 0, Compare: Compare} - pl.head.next = pl.tail - pl.tail.prev = pl.head - return pl -} - -func (pl *PriorityList) String() string { - content := "" - - cur := pl.head.next - - for ; cur != pl.tail; cur = cur.next { - content += spew.Sprint(cur.value) + " " - } - content = strings.TrimRight(content, " ") - return content -} - -func (pl *PriorityList) RString() string { - content := "" - - cur := pl.tail.prev - - for ; cur != pl.head; cur = cur.prev { - content += spew.Sprint(cur.value) + " " - } - content = strings.TrimRight(content, " ") - return content -} - -func (pl *PriorityList) Iterator() *Iterator { - return &Iterator{pl: pl, cur: pl.head} -} - -func (pl *PriorityList) CircularIterator() *CircularIterator { - return &CircularIterator{pl: pl, cur: pl.head} -} - -func (pl *PriorityList) Size() int { - return pl.size -} - -func (pl *PriorityList) Push(value interface{}) { - pl.size++ - pnode := &Node{value: value} - if pl.size == 1 { - pl.head.next = pnode - pl.tail.prev = pnode - pnode.prev = pl.head - pnode.next = pl.tail - return - } - - cur := pl.head - for ; cur.next != pl.tail; cur = cur.next { - if pl.Compare(value, cur.next.value) > 0 { - cnext := cur.next - - cur.next = pnode - cnext.prev = pnode - pnode.prev = cur - pnode.next = cnext - - return - } - } - - cur.next = pnode - pnode.prev = cur - pnode.next = pl.tail - pl.tail.prev = pnode -} - -func (pl *PriorityList) Top() (result interface{}, ok bool) { - if pl.size > 0 { - return pl.head.next.value, true - } - return nil, false -} - -func (pl *PriorityList) Pop() (result interface{}, ok bool) { - if pl.size > 0 { - pl.size-- - temp := pl.head.next - temp.next.prev = pl.head - pl.head.next = temp.next - return temp.value, true - } - return nil, false -} - -func (pl *PriorityList) Get(idx int) (interface{}, bool) { - if n, ok := pl.GetNode(idx); ok { - return n.value, true - } - return nil, false -} - -func (pl *PriorityList) GetNode(idx int) (*Node, bool) { - if idx >= 0 { - cur := pl.head.next - for i := 0; cur != pl.tail; i++ { - if i == idx { - return cur, true - } - cur = cur.next - } - } else { - cur := pl.tail.prev - for i := -1; cur != pl.head; i-- { - if i == idx { - return cur, true - } - cur = cur.prev - } - } - return nil, false -} - -func (pl *PriorityList) RemoveWithIndex(idx int) { - if n, ok := pl.GetNode(idx); ok { - pl.Remove(n) - } -} - -func (pl *PriorityList) Remove(node *Node) { - - prev := node.prev - next := node.next - prev.next = next - next.prev = prev - - node.prev = nil - node.next = nil - - pl.size-- -} -func (pl *PriorityList) Values() []interface{} { - values := make([]interface{}, pl.size, pl.size) - for i, cur := 0, pl.head.next; cur != pl.tail; i, cur = i+1, cur.next { - values[i] = cur.value - } - return values -} diff --git a/priority_list/priority_list_test.go b/priority_list/priority_list_test.go deleted file mode 100644 index 2ea0129..0000000 --- a/priority_list/priority_list_test.go +++ /dev/null @@ -1,260 +0,0 @@ -package plist - -import ( - "bytes" - "encoding/gob" - "io/ioutil" - "log" - "testing" - - "github.com/474420502/focus/compare" -) - -func loadTestData() []int { - data, err := ioutil.ReadFile("../l.log") - if err != nil { - log.Println(err) - } - var l []int - decoder := gob.NewDecoder(bytes.NewReader(data)) - decoder.Decode(&l) - return l -} - -func TestInsert(t *testing.T) { - pl := New(compare.Int) - for i := 0; i < 10; i++ { - pl.Push(i) - } - - if pl.size != 10 { - t.Error(pl.size) - } - - if pl.String() != "9 8 7 6 5 4 3 2 1 0" { - t.Error(pl.String()) - } - - if pl.RString() != "0 1 2 3 4 5 6 7 8 9" { - t.Error(pl.RString()) - } - - for i := 0; i < 10; i++ { - pl.Push(i) - } - - if pl.String() != "9 9 8 8 7 7 6 6 5 5 4 4 3 3 2 2 1 1 0 0" { - t.Error(pl.String()) - } - - if pl.RString() != "0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9" { - t.Error(pl.RString()) - } -} - -func TestIterator(t *testing.T) { - pl := New(compare.Int) - for i := 0; i < 10; i++ { - pl.Push(i) - } - - iter := pl.Iterator() - - for i := 0; iter.Next(); i++ { - if iter.Value() != 9-i { - t.Error("iter.Next() ", iter.Value(), "is not equal ", 9-i) - } - } - - if iter.cur != iter.pl.tail { - t.Error("current point is not equal tail ", iter.pl.tail) - } - - for i := 0; iter.Prev(); i++ { - if iter.Value() != i { - t.Error("iter.Prev() ", iter.Value(), "is not equal ", i) - } - } -} - -func TestCircularIterator(t *testing.T) { - pl := New(compare.Int) - for i := 0; i < 10; i++ { - pl.Push(i) - } - - iter := pl.CircularIterator() - - for i := 0; i != 10; i++ { - iter.Next() - if iter.Value() != 9-i { - t.Error("iter.Next() ", iter.Value(), "is not equal ", 9-i) - } - } - - if iter.cur != iter.pl.tail.prev { - t.Error("current point is not equal tail ", iter.pl.tail.prev) - } - - if iter.Next() { - if iter.Value() != 9 { - t.Error("iter.Value() != ", iter.Value()) - } - } - - iter.MoveToTail() - for i := 0; i != 10; i++ { - iter.Prev() - if iter.Value() != i { - t.Error("iter.Prev() ", iter.Value(), "is not equal ", i) - } - } - - if iter.cur != iter.pl.head.next { - t.Error("current point is not equal tail ", iter.pl.tail.prev) - } - - if iter.Prev() { - if iter.Value() != 0 { - t.Error("iter.Value() != ", iter.Value()) - } - } -} - -func TestGet(t *testing.T) { - pl := New(compare.Int) - for i := 0; i < 10; i++ { - pl.Push(i) - } - - for _, v := range []int{0, 9, 5, 7} { - if g, ok := pl.Get(v); ok { - if g != (9 - v) { - t.Error(v, "Get == ", g) - } - } - } - - if n, ok := pl.Get(10); ok { - t.Error("index 10 is over size", n) - } -} - -func TestTop(t *testing.T) { - pl := New(compare.Int) - for i := 0; i < 10; i++ { - pl.Push(i) - } - - i := 0 - for n, ok := pl.Pop(); ok; n, ok = pl.Pop() { - if (9 - i) != n { - t.Error("value is not equal ", i) - } - if top, tok := pl.Top(); tok { - if (9 - i - 1) != top { - t.Error("top is error cur i = ", i, "top is ", top) - } - } - - i++ - } - - if pl.Size() != 0 { - t.Error("list size is not zero") - } -} - -func TestPop(t *testing.T) { - pl := New(compare.Int) - for i := 0; i < 10; i++ { - pl.Push(i) - } - - i := 0 - for n, ok := pl.Pop(); ok; n, ok = pl.Pop() { - if (9 - i) != n { - t.Error("value is not equal ", i) - } - i++ - } - - if pl.Size() != 0 { - t.Error("list size is not zero") - } - - for i := 9; i >= 0; i-- { - pl.Push(i) - } - - i = 0 - for n, ok := pl.Pop(); ok; n, ok = pl.Pop() { - if (9 - i) != n { - t.Error("value is not equal ", i) - } - i++ - } - - if pl.Size() != 0 { - t.Error("list size is not zero") - } -} - -func TestRemove(t *testing.T) { - pl := New(compare.Int) - for i := 0; i < 10; i++ { - pl.Push(i) - } - - pl.RemoveWithIndex(0) - if g, ok := pl.Get(0); ok { - if g != 8 { - t.Error(g) - } - } - - pl.RemoveWithIndex(-1) - if g, ok := pl.Get(-1); ok { - if g != 1 { - t.Error(g) - } - } - -} - -// func BenchmarkGet(b *testing.B) { -// pl := New(compare.Int) - -// l := loadTestData() - -// for _, v := range l { -// pl.Push(v) -// } - -// b.ResetTimer() -// b.StartTimer() -// b.N = len(l) - -// for i := 0; i < b.N; i++ { -// if i%2 == 0 { -// pl.Get(i) -// } -// } - -// } -// func BenchmarkInsert(b *testing.B) { - -// l := loadTestData() - -// b.ResetTimer() -// b.StartTimer() - -// execCount := 1 -// b.N = len(l) * execCount -// for i := 0; i < execCount; i++ { -// pl := New(compare.Int) -// for _, v := range l { -// pl.Push(v) -// } -// } -// }