ordermap

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

README

ordermap

badge badge

Unlike an unordered map, the orderedmap package is an ordermap type. Ordermap is an inherited Map maps keys to values. Ordermap provides useful methods e.g. Store, Load, Delete , Range and so on. The remaining methods are order-aware. Big-O running times for all methods are the same as regular maps.

How To Get

Get source code by go get command:

go get cnb.cool/ordermap/ordermap@v1.1.0

Or you can write following code in go.mod for your project:

require (
	cnb.cool/ordermap/ordermap v1.1.0
)

API

Create ordermap instance

func New() *OrderMap

For example:

package main

import (
	"fmt"

	"cnb.cool/ordermap/ordermap"
)

func main() {
	o := ordermap.New()
	fmt.Printf("%T", o)
	// output: *ordermap.OrderMap
}

Store the data

Store method used to set the value for a key.

func (om *OrderMap) Store(key, value any)

For example:

package main

import (
	"fmt"

	"cnb.cool/ordermap/ordermap"
)

func main() {
	o := ordermap.New()
	o.Store("name", "Pizza")
	o.Store("price", 50)
	o.Store("size", "10#")
	fmt.Println(o)
	// output: {"name": "Pizza", "price": 50, "size": "10#"}
}

Load the data

Load method used to get the value from a key. If the key is not exist in the map, a nil and a false will be returned.

func (om *OrderMap) Load(key any) (any, bool)

For example:

package main

import (
	"fmt"

	"cnb.cool/ordermap/ordermap"
)

func main() {
	o := ordermap.New()
	o.Store("name", "Pizza")
	o.Store("price", 50)
	o.Store("size", "10#")

	size, ok := o.Load("size")
	fmt.Println(size, ok)
	// output: 10# true

	color, ok := o.Load("color")
	fmt.Println(color, ok)
	// output: <nil> false
}

Delete the data

Delete method is used to delete the value associated with a key.

func (om *OrderMap) Delete(key any)

For example:

package main

import (
	"fmt"

	"cnb.cool/ordermap/ordermap"
)

func main() {
	o := ordermap.New()
	o.Store("name", "Pizza")
	o.Store("price", 50)
	o.Store("size", "10#")
	fmt.Println("Before using delete method, the content of ordermap is: ", o)
	// Before using delete method, the content of ordermap is: {"name": "Pizza", "price": 50, "size": "10#"}

	o.Delete("size")
	fmt.Println("After using delete method, the content of ordermap is: ", o)
	// After using delete method, the content of ordermap is: {"name": "Pizza", "price": 50}
}

Range map

You can use the Range method to visit each key and value. The Range method requires an argument of type func(key, value any) bool. If returns false, the iteration stops.

func (om *OrderMap) Range(f func(key, value any) bool)

For example:

package main

import (
	"fmt"

	"cnb.cool/ordermap/ordermap"
)

func main() {
	o := ordermap.New()
	o.Store("A", "a")
	o.Store("B", "b")

	f := func(key, value interface{}) bool {
		fmt.Printf("key: %v, value: %v\n", key, value)
		return true
	}

	o.Range(f)
	// outputs:
	// key: A, value: a
	// key: B, value: b
}

Get the length of ordermap

You can use the Length method to obtain the length of the OrderMap.

func (om *OrderMap) Length() int

For example:

package main

import (
	"fmt"

	"cnb.cool/ordermap/ordermap"
)

func main() {
	o := ordermap.New()
	o.Store("A", "a")
	o.Store("B", "b")

	fmt.Println(o.Length())
	// output: 2
}

Interface

OrderMap also implements some common interfaces, which will be more convenient when actually using it.

fmt.Stringer

package main

import (
	"fmt"

	"cnb.cool/ordermap/ordermap"
)

func main() {
	o := ordermap.New()
	o.Store("A", "a")
	o.Store("B", "b")
	o.Store(1, 3.14)

	fmt.Println(o)
	// {"A": "a", "B": "b", 1: 3.14}

	fmt.Printf("The content of OrderMap object is: %s\n", o)
	// The content of OrderMap object is: {"A": "a", "B": "b", 1: 3.14}
}

fmt.GoStringer

package main

import (
	"fmt"

	"cnb.cool/ordermap/ordermap"
)

func main() {
	o := ordermap.New()
	o.Store("A", "a")
	o.Store(1, 3.14)

	fmt.Println(o)
	// {"A": "a", 1: 3.14}

	fmt.Printf("The content of OrderMap object is: %#v\n", o)
	// The content of OrderMap object is: {"A": "a", 1: 3.14}
}

Documentation

Overview

Package ordermap defines functions and methods for OrderedMap.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type OrderMap

type OrderMap struct {
	// contains filtered or unexported fields
}

OrderMap is like a Go map[any]any but is having order for each key add or delete. It is not concurrent security.

func New

func New() *OrderMap

New function returns a pointer for OrderedMap.

func (*OrderMap) Delete

func (om *OrderMap) Delete(key any)

Delete method is used to delete the value associated with a key.

func (*OrderMap) GoString

func (om *OrderMap) GoString() string

GoString method implements `fmt.GoStringer`.

func (*OrderMap) Length

func (om *OrderMap) Length() int

Length method return an integer number of OrderedMap's length.

func (*OrderMap) Load

func (om *OrderMap) Load(key any) (any, bool)

Load returns the value stored in the map for a key, or nil if no value is present. The ok result indicates whether value was found in the map.

func (*OrderMap) Range

func (om *OrderMap) Range(f func(key, value any) bool)

Range calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration.

No key will be visited more than once. Unless the same key is deleted first and then added in iteration.

func (*OrderMap) Store

func (om *OrderMap) Store(key, value any)

Store sets the value for a key. If the value of the key is an unhashable value, a panic will be raised.

func (*OrderMap) String

func (om *OrderMap) String() string

String method used to realize "fmt.Stringer".

Source Files

  • map.go

Jump to

Keyboard shortcuts

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