github.com/enetx/g@v1.0.80/examples/maps_ordered.go (about) 1 package main 2 3 import ( 4 "fmt" 5 6 "github.com/enetx/g" 7 "github.com/enetx/g/cmp" 8 "github.com/enetx/g/pkg/ref" 9 ) 10 11 func main() { 12 // var mo g.MapOrd[int, string] 13 // mo := g.MapOrd[int, string]{} 14 // mo := make(g.MapOrd[int, string], 0) 15 16 gos := g.NewMapOrd[int, *g.Slice[int]]() 17 18 for i := range 5 { 19 gos.GetOrSet(i, ref.Of(g.NewSlice[int]())).AppendInPlace(i) 20 } 21 22 for i := range 10 { 23 gos.GetOrSet(i, ref.Of(g.NewSlice[int]())).AppendInPlace(i) 24 } 25 26 gos.Print() 27 28 ////////////////////////////////////////////////////////////////////////// 29 30 for _, m := range gos { 31 fmt.Println(m.Key, m.Value) 32 } 33 34 ////////////////////////////////////////////////////////////////////////// 35 36 god := g.NewMapOrd[int, g.Slice[int]]() 37 38 for i := range 5 { 39 god.Set(i, god.Get(i).UnwrapOrDefault().Append(i)) 40 } 41 42 for i := range 10 { 43 god.Set(i, god.Get(i).UnwrapOrDefault().Append(i)) 44 } 45 46 god.Print() 47 48 ////////////////////////////////////////////////////////////////////////// 49 50 ms := g.NewMapOrd[g.Int, g.Int]() 51 ms. 52 Set(11, 99). 53 Set(12, 2). 54 Set(1, 22). 55 Set(2, 32). 56 Set(222, 2) 57 58 ms1 := ms.Clone() 59 ms1. 60 Set(888, 000). 61 Set(888, 300) 62 63 if r := ms1.Get(888); r.IsSome() { 64 r.Some().Print() 65 } 66 67 if r := ms1.Get(11); r.IsSome() { 68 r.Some().Print() 69 } 70 71 ms1.Set(1, 223) 72 73 ms.Print() 74 ms1.Print() 75 76 fmt.Println(ms.Eq(ms1)) 77 fmt.Println(ms.Contains(12)) 78 79 ms.Iter().ForEach(func(k, v g.Int) { fmt.Println(k, v) }) 80 81 ms = ms.Iter().Map(func(k, v g.Int) (g.Int, g.Int) { return k.Mul(2), v.Mul(2) }).Collect() 82 ms.Print() 83 84 ms.Delete(22) 85 fmt.Println(ms.Contains(22)) 86 87 msstr := g.NewMapOrd[g.String, g.String]() 88 msstr. 89 Set("aaa", "CCC"). 90 Set("ccc", "AAA"). 91 Set("bbb", "DDD"). 92 Set("ddd", "BBB") 93 94 fmt.Println("before sort:", msstr) 95 96 // msstr.SortBy(func(a, b g.Pair[g.String, g.String]) cmp.Ordering { return a.Key.Cmp(b.Key) }) 97 msstr.SortByKey(func(a, b g.String) cmp.Ordering { return a.Cmp(b) }) 98 fmt.Println("after sort:", msstr) 99 100 // msstr.SortBy(func(a, b g.Pair[g.String, g.String]) cmp.Ordering { return a.Value.Cmp(b.Value) }) 101 msstr.SortByValue(g.String.Cmp) 102 fmt.Println("after sort by value:", msstr) 103 104 mss := g.NewMapOrd[g.Int, g.Slice[int]]() 105 mss.Set(22, g.Slice[int]{4, 0, 9, 6, 7}) 106 mss.Set(11, g.Slice[int]{1, 2, 3, 4}) 107 108 fmt.Println("before sort: ", mss) 109 // mss.SortBy(func(a, b g.Pair[g.Int, g.Slice[int]]) cmp.Ordering { return a.Key.Cmp(b.Key) }) 110 mss.SortByKey(g.Int.Cmp) 111 fmt.Println("after sort by key: ", mss) 112 113 // mss.SortBy(func(a, b g.Pair[g.Int, g.Slice[int]]) cmp.Ordering { return cmp.Cmp(a.Value[1], b.Value[1]) }) 114 mss.SortByValue(func(a, b g.Slice[int]) cmp.Ordering { return cmp.Cmp(a[1], b[1]) }) 115 fmt.Println("after sort by second value: ", mss) 116 117 // g.MapOrdFromStd(mss.ToMap().Std()).Print() 118 }