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