The following function is used to generate test data in the examples:
func randRecords(n int) []sortedmap.Record {
mrand.Seed(time.Now().UTC().UnixNano())
records := make([]sortedmap.Record, n)
for i := range records {
year := mrand.Intn(2058)
for year < 2000 {
year = mrand.Intn(2058)
}
mth := time.Month(mrand.Intn(12))
if mth < 1 {
mth++
}
day := mrand.Intn(28)
if day < 1 {
day++
}
hour := mrand.Intn(23)
min := mrand.Intn(59)
sec := mrand.Intn(59)
t := time.Date(year, mth, day, hour, min, sec, 0, time.UTC)
records[i] = sortedmap.Record{
Key: t.Format(time.UnixDate),
Val: t,
}
}
return records
}
Below is an example containing common operations that are used with a single record.
package main
import (
"fmt"
"time"
mrand "math/rand"
"github.com/umpc/go-sortedmap"
"github.com/umpc/go-sortedmap/asc"
)
func main() {
const n = 1
records := randRecords(n)
rec := records[0]
// Create a new collection. This reserves memory for one item
// before allocating a new backing array and appending to it:
sm := sortedmap.New(n, asc.Time)
// Insert the example record:
if !sm.Insert(rec.Key, rec.Val) {
fmt.Printf("The key already exists: %+v", rec.Key)
}
// Get and print the value:
if val, ok := sm.Get(rec.Key); ok {
fmt.Printf("%+v\n", val)
}
// Replace the example record:
sm.Replace(rec.Key, time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC))
// Remove the example record:
sm.Delete(rec.Key)
// Check if the index has the key:
const recHasFmt = "The key %v.\n"
if sm.Has(rec.Key) {
fmt.Printf(recHasFmt, "exists")
} else {
fmt.Printf(recHasFmt, "does not exist")
}
}
SortedMap supports three specific ways of processing list data:
- Channels
- Callback Functions
- Maps & Slices
IterCh
is a simple way of iterating over the entire set, in order. The returned value should be closed when finished using it, to close any sending goroutines.
package main
import (
"fmt"
"time"
mrand "math/rand"
"github.com/umpc/go-sortedmap"
"github.com/umpc/go-sortedmap/asc"
)
func main() {
const n = 25
records := randRecords(n)
// Create a new collection.
sm := sortedmap.New(n, asc.Time)
// BatchInsert the example records:
sm.BatchInsert(records)
iterCh, err := sm.IterCh()
if err != nil {
fmt.Println(err)
} else {
defer iterCh.Close()
for rec := range iterCh.Records() {
fmt.Printf("%+v\n", rec)
}
}
}
BoundedIterCh
selects values that are greater than the lower bound and are less than or equal to the upper bound. Its first argument allows for reversing the order of the returned records.
package main
import (
"fmt"
"time"
mrand "math/rand"
"github.com/umpc/go-sortedmap"
"github.com/umpc/go-sortedmap/asc"
)
func main() {
const n = 25
records := randRecords(n)
// Create a new collection.
sm := sortedmap.New(n, asc.Time)
// BatchInsert the example records:
sm.BatchInsert(records)
iterCh, err := sm.BoundedIterCh(false, time.Time{}, time.Now())
if err != nil {
fmt.Println(err)
} else {
defer iterCh.Close()
for rec := range iterCh.Records() {
fmt.Printf("%+v\n", rec)
}
}
}
CustomIterCh
provides all IterCh
functionality and accepts a special IterChParams
value to read settings.
This method offers a deadline timeout for channel sends and it is highly recommended that this method, or Map + Slice, be used where reliability over a long run-time counts.
package main
import (
"fmt"
"time"
mrand "math/rand"
"github.com/umpc/go-sortedmap"
"github.com/umpc/go-sortedmap/asc"
)
func main() {
const n = 25
records := randRecords(n)
// Create a new collection.
sm := sortedmap.New(n, asc.Time)
// BatchInsert the example records:
sm.BatchInsert(records)
params := sortedmap.IterChParams{
SendTimeout: 5 * time.Minute,
Reversed: true,
}
iterCh, err := sm.CustomIterCh(params)
if err != nil {
fmt.Println(err)
} else {
defer iterCh.Close()
for rec := range iterCh.Records() {
fmt.Printf("%+v\n", rec)
}
}
}
IterFunc
is like IterCh
, but runs a callback function passing in each record instead of sending the records on a channel.
package main
import (
"fmt"
"time"
mrand "math/rand"
"github.com/umpc/go-sortedmap"
"github.com/umpc/go-sortedmap/asc"
)
func main() {
const n = 25
records := randRecords(n)
// Create a new collection.
sm := sortedmap.New(n, asc.Time)
// BatchInsert the example records:
sm.BatchInsert(records)
sm.IterFunc(false, func(rec sortedmap.Record) bool {
fmt.Printf("%+v\n", rec)
return true
})
}
BoundedIterFunc
is the callback function equivalent to BoundedIterCh
/CustomIterCh
.
package main
import (
"fmt"
"time"
mrand "math/rand"
"github.com/umpc/go-sortedmap"
"github.com/umpc/go-sortedmap/asc"
)
func main() {
const n = 25
records := randRecords(n)
// Create a new collection.
sm := sortedmap.New(n, asc.Time)
// BatchInsert the example records:
sm.BatchInsert(records)
if err := sm.BoundedIterFunc(false, time.Time{}, time.Now(), func(rec sortedmap.Record) bool {
fmt.Printf("%+v\n", rec)
return true
})
err != nil {
fmt.Println(err)
}
}
The Map
and Keys
methods offer a way of iterating throughout the map using a combination of Go's native map and slice types.
package main
import (
"fmt"
"time"
mrand "math/rand"
"github.com/umpc/go-sortedmap"
"github.com/umpc/go-sortedmap/asc"
)
func main() {
const n = 25
records := randRecords(n)
// Create a new collection.
sm := sortedmap.New(n, asc.Time)
// BatchInsert the example records:
sm.BatchInsert(records)
m, keys := sm.Map(), sm.Keys()
for _, k := range keys {
fmt.Printf("%+v\n", m[k])
}
}
Like the above Bounded
methods, this method allows for selecting only the necessary data for processing.
package main
import (
"fmt"
"time"
mrand "math/rand"
"github.com/umpc/go-sortedmap"
"github.com/umpc/go-sortedmap/asc"
)
func main() {
const n = 25
records := randRecords(n)
// Create a new collection.
sm := sortedmap.New(n, asc.Time)
// BatchInsert the example records:
sm.BatchInsert(records)
// Copy the map + slice headers.
m := sm.Map()
keys, err := sm.BoundedKeys(time.Time{}, time.Now())
if err != nil {
fmt.Println(err)
} else {
for _, k := range keys {
fmt.Printf("%+v\n", m[k])
}
}
}
BoundedDelete
is a similar pattern as the above Bounded
methods. BoundedDelete
removes values that are greater than the lower bound and lower than or equal to the upper bound.
package main
import (
"fmt"
"time"
mrand "math/rand"
"github.com/umpc/go-sortedmap"
"github.com/umpc/go-sortedmap/asc"
)
func main() {
const n = 25
records := randRecords(n)
// Create a new collection.
sm := sortedmap.New(n, asc.Time)
// BatchInsert the example records:
sm.BatchInsert(records)
// Delete values after the lower bound value and before or equal to the upper bound value.
if err := sm.BoundedDelete(time.Time{}, time.Now()); err != nil {
fmt.Println(err)
}
}
For more options and features, check out the documentation: