forked from emirpasic/gods
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iteratorwithindex.go
51 lines (42 loc) · 1.34 KB
/
iteratorwithindex.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"github.com/emirpasic/gods/sets/treeset"
)
// IteratorWithIndexExample to demonstrate basic usage of IteratorWithIndex
func main() {
set := treeset.NewWithStringComparator()
set.Add("a", "b", "c")
it := set.Iterator()
fmt.Print("\nForward iteration\n")
for it.Next() {
index, value := it.Index(), it.Value()
fmt.Print("[", index, ":", value, "]") // [0:a][1:b][2:c]
}
fmt.Print("\nForward iteration (again)\n")
for it.Begin(); it.Next(); {
index, value := it.Index(), it.Value()
fmt.Print("[", index, ":", value, "]") // [0:a][1:b][2:c]
}
fmt.Print("\nBackward iteration\n")
for it.Prev() {
index, value := it.Index(), it.Value()
fmt.Print("[", index, ":", value, "]") // [2:c][1:b][0:a]
}
fmt.Print("\nBackward iteration (again)\n")
for it.End(); it.Prev(); {
index, value := it.Index(), it.Value()
fmt.Print("[", index, ":", value, "]") // [2:c][1:b][0:a]
}
if it.First() {
fmt.Print("\nFirst index: ", it.Index()) // First index: 0
fmt.Print("\nFirst value: ", it.Value()) // First value: a
}
if it.Last() {
fmt.Print("\nLast index: ", it.Index()) // Last index: 3
fmt.Print("\nLast value: ", it.Value()) // Last value: c
}
}