Giter Club home page Giter Club logo

linkedmap's Introduction

Doubly Linked Hash Table

This implementation uses built-in map, and extends it to track order in which (key, value) pairs are added.

This library, and built-in map type are not safe for concurrent use.

Example usage

package main

import (
	"fmt"
	"github.com/mihgen/linkedmap"
)

func main() {
	lm := linkedmap.New()

	lm.Add(1, "value1")
	lm.Add(false, "value2")
	lm.Add("key3", "value3")
	lm.Add(false, "00updated00") // update doesn't change order

	// Show value of previously added k-v pair
	fmt.Println("Value of prev added k-v pair: ", lm.Last().Prev().Value())

	// Get value knowing a key
	fmt.Println("Value for key1 is", lm.Get(1))

	keys := []string{"key3", "no-key"}
	for _, k := range keys {
		v, ok := lm.GetWithOk(k) // returns false if key doesn't exist in a map
		fmt.Println("v, ok =", v, ",", ok)
	}

	// List all stored (k,v) pairs
	for e := lm.First(); e != nil; e = e.Next() {
		fmt.Println(e.Key(), "->", e.Value())
	}

	// Pairs before the first and after the last are nil
	fmt.Println("Before the first -", lm.First().Prev())
	fmt.Println("After the last -", lm.Last().Next())
}

Specifying the size of map

If it is required to specify allocation size for map, then new function can be added to linkedmap.go, and used instead of New():

func NewAllocation(size int64) *LinkedMap {
	return &LinkedMap{Map: make(map[interface{}]mapValue, size)}
}

linkedmap's People

Contributors

mihgen avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.