Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Iterators #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions iterator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package orderedmap

type PairsIterator struct {
m *OrderedMap
index int
length int
}

func (it *PairsIterator) Index() int {
return it.index
}

func (it *PairsIterator) Length() int {
return it.length
}

func (it *PairsIterator) Next() (*Pair, error) {
index := it.index
it.index++
key := it.m.keys[index]
return &Pair{key, it.m.values[key]}, nil
}

func (it *PairsIterator) Done() bool {
return it.index >= it.length
}

func (it *PairsIterator) Close() error {
return nil
}

type ValuesIterator struct {
m *OrderedMap
index int
length int
}

func (it *ValuesIterator) Index() int {
return it.index
}

func (it *ValuesIterator) Length() int {
return it.length
}

func (it *ValuesIterator) Next() (interface{}, error) {
index := it.index
it.index++
return it.m.values[it.m.keys[index]], nil
}

func (it *ValuesIterator) Done() bool {
return it.index >= it.length
}

func (it *ValuesIterator) Close() error {
return nil
}
30 changes: 28 additions & 2 deletions orderedmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
)

var NoValueError = errors.New("No value for this key")
var Done = errors.New("no more items in iterator")

type KeyIndex struct {
Key string
Expand All @@ -20,7 +21,7 @@ func (a ByIndex) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByIndex) Less(i, j int) bool { return a[i].Index < a[j].Index }

type Pair struct {
key string
key string
value interface{}
}

Expand All @@ -33,14 +34,16 @@ func (kv *Pair) Value() interface{} {
}

type ByPair struct {
Pairs []*Pair
Pairs []*Pair
LessFunc func(a *Pair, j *Pair) bool
}

func (a ByPair) Len() int { return len(a.Pairs) }
func (a ByPair) Swap(i, j int) { a.Pairs[i], a.Pairs[j] = a.Pairs[j], a.Pairs[i] }
func (a ByPair) Less(i, j int) bool { return a.LessFunc(a.Pairs[i], a.Pairs[j]) }



type OrderedMap struct {
keys []string
values map[string]interface{}
Expand All @@ -58,6 +61,11 @@ func (o *OrderedMap) Get(key string) (interface{}, bool) {
return val, exists
}

func (o *OrderedMap) Has(key string) bool {
_, exists := o.values[key]
return exists
}

func (o *OrderedMap) Set(key string, value interface{}) {
_, exists := o.values[key]
if !exists {
Expand Down Expand Up @@ -106,6 +114,24 @@ func (o *OrderedMap) Sort(lessFunc func(a *Pair, b *Pair) bool) {
}
}

func (o *OrderedMap) EachValues(cb func(value interface{}) error) (err error) {
for i, max := 0, len(o.keys); i < max; i++ {
err = cb(o.values[o.keys[i]])
if err != nil {
return err
}
}
return
}

func (o *OrderedMap) IterValues() *ValuesIterator {
return &ValuesIterator{o, 0, len(o.keys)}
}

func (o *OrderedMap) Iter() *PairsIterator {
return &PairsIterator{o, 0, len(o.keys)}
}

func (o *OrderedMap) UnmarshalJSON(b []byte) error {
m := map[string]interface{}{}
if err := json.Unmarshal(b, &m); err != nil {
Expand Down