Help us shape the future of Go! Take the 2026 Go Developer Survey

topmap

package module
v1.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 25, 2025 License: MIT Imports: 2 Imported by: 1

README

Go version

go-topmap

Top items map (string of int), can be used to maintain a list of counters and get the top N items.

Installation

go get -u github.com/dmachard/go-topmap

Usage example

Example to use this module

// init topmap with 5 max items
tmap := NewTopMap(5)

// add items to the map
tmap.Record("oranges", 2)
tmap.Record("apples", 10)

// incremente items
tmap.Inc("oranges")


// get items
items = tmap.Get()
fmt.Println(items)
[{apples 10} {oranges 3}]

Testing

=== RUN   TestTopMapRecord
--- PASS: TestTopMapRecord (0.00s)
=== RUN   TestTopMapInc
--- PASS: TestTopMapInc (0.00s)
=== RUN   TestTopMapGetSorted
--- PASS: TestTopMapGetSorted (0.00s)
=== RUN   TestTopMapRecordMax
--- PASS: TestTopMapRecordMax (0.00s)
PASS
ok      dmachard/go-topmap      0.002s

Benchmark

goos: linux
goarch: amd64
pkg: dmachard/go-topmap
cpu: Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz
BenchmarkTopMapRecord/TopMapSize:_10_-4                  4745701               247.1 ns/op
BenchmarkTopMapRecord/TopMapSize:_100_-4                  604754              1761 ns/op
BenchmarkTopMapRecord/TopMapSize:_1000_-4                  56764             21874 ns/op
BenchmarkTopMapRecord/TopMapSize:_10000_-4                 13870      

BenchmarkTopMapRecord/TopMapSize:_10_-4                  4745701               247.1 ns/op
BenchmarkTopMapRecord/TopMapSize:_100_-4                  604754              1761 ns/op
BenchmarkTopMapRecord/TopMapSize:_1000_-4                  56764             21874 ns/op
BenchmarkTopMapRecord/TopMapSize:_10000_-4                 13870            113485 ns/op

BenchmarkTopMapGet/TopMapSize:_10_-4                     1012234              1264 ns/op
BenchmarkTopMapGet/TopMapSize:_100_-4                      75252             15170 ns/op
BenchmarkTopMapGet/TopMapSize:_1000_-4                      5414            199167 ns/op
BenchmarkTopMapGet/TopMapSize:_10000_-4                      429           2792158 ns/op
PASS
ok      dmachard/go-topmap      14.896s

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ByHit

type ByHit []TopMapItem

sort interface

func (ByHit) Len

func (a ByHit) Len() int

func (ByHit) Less

func (a ByHit) Less(i, j int) bool

func (ByHit) Swap

func (a ByHit) Swap(i, j int)

type TopMap

type TopMap struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewTopMap

func NewTopMap(maxitems int) *TopMap

func (*TopMap) FindMin

func (q *TopMap) FindMin()

func (*TopMap) Get

func (q *TopMap) Get() []TopMapItem

func (*TopMap) Inc added in v0.4.0

func (q *TopMap) Inc(name string)

func (*TopMap) Record

func (q *TopMap) Record(name string, hit int)

type TopMapItem

type TopMapItem struct {
	Name string `json:"key"`
	Hit  int    `json:"hit"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL