sortmap

package module
Version: v0.0.0-...-a96bee2 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2016 License: MIT Imports: 1 Imported by: 0

README

SortMap

Build Status GoDoc Coverage Status

SortMap illustrates how to build a sortable map in Golang. Also, this library can be used in a practical project.

Install

Install the package:

$ go get github.com/mikespook/sortmap

Authors

Open Source - MIT Software License

See LICENSE.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CompareFunc

type CompareFunc func(ka, va, kb, vb interface{}) bool

CompareFunc has 4 paramaters in two pair: `a(ka=>va)` and `b(kb=>vb)`.

type SortMap

type SortMap struct {

	// mutex of data
	sync.RWMutex
	// contains filtered or unexported fields
}

SortMap illustrates how to sort a map

Example
package main

import (
	"fmt"
	"sort"
	"sync"

	"github.com/mikespook/sortmap"
)

func main() {
	var wg sync.WaitGroup
	by := func(ka, va, kb, vb interface{}) bool {
		return ka.(int) < kb.(int)
	}
	sm := sortmap.New(by)
	for i := 0; i < 100; i++ {
		wg.Add(1)
		go func() {
			for i := 0; i < 10000; i++ {
				sm.Set(i, i)
			}
			wg.Done()
		}()
	}
	wg.Wait()
	sort.Sort(sm)
	for i := 0; i < 100; i++ {
		wg.Add(1)
		go func() {
			for a, ok := sm.Begin(); ok; a, ok = sm.Next() {
				fmt.Println(a, ok)
			}
			wg.Done()
		}()
	}
	wg.Wait()
}
Output:

func New

func New(by CompareFunc) *SortMap

New returns an instance of SortMap

func (*SortMap) Begin

func (sm *SortMap) Begin() (interface{}, bool)

Begin resets the position indicator and return the first data and true, or nil and false if not data has been added into the map

func (*SortMap) Delete

func (sm *SortMap) Delete(k interface{}) interface{}

Delete removes the data with key `k` and returns it

func (*SortMap) End

func (sm *SortMap) End() (interface{}, bool)

End sets the position indicator to the end of data and return it and true, or nil and false if not data has been added into the map

func (*SortMap) Get

func (sm *SortMap) Get(k interface{}) interface{}

Get returns the data with key `k`

func (*SortMap) Len

func (sm *SortMap) Len() int

Len is part of sort.Interface and return the lengh of data

func (*SortMap) Less

func (sm *SortMap) Less(i, j int) bool

Less is part of sort.Interface. It is implemented by calling the "By" closure in the sorter.

func (*SortMap) Next

func (sm *SortMap) Next() (v interface{}, has bool)

Next returns the next data and true, or nil and false if it's the end of data

func (*SortMap) Set

func (sm *SortMap) Set(k interface{}, v interface{})

Set asserts data `v` with key `k`

func (*SortMap) Swap

func (sm *SortMap) Swap(i, j int)

Swap is part of sort.Interface and should not be used directly

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL