GithubHelp home page GithubHelp logo

jwangsadinata / go-multimap Goto Github PK

View Code? Open in Web Editor NEW
36.0 2.0 12.0 23 KB

Go-Multimap is an implementation of the `multimap` data structure in Go.

License: MIT License

Go 100.00%
go golang golang-package data-structures multimap map

go-multimap's Introduction

GoDoc Build Status Go Report Card Coverage Status License: MIT

Go-Multimap

This is the missing multimap collection for the Go language (also a simple practice in creating a proper library/package).

A multimap (sometimes also multihash or multidict) is a generalization of a map or associative array abstract data type in which more than one value may be associated with and returned for a given key.

Some use cases and examples for this data type includes:

  • The index of a book may report any number of references for a given index term, and thus may be coded as a multimap from index terms to any number of reference locations or pages.
  • Address location, such as ZIP code, that maps to any number of people living in that area.

There are two different multimap implementations, slicemultimap and setmultimap, which has slices and sets as the map values respectively. slicemultimap is useful when duplicate key/value pairs is allowed and insertion ordering is important. On the other hand, setmultimap is suitable when duplicates of key/value pairs are not allowed.

This package was heavily inspired by the Google Guava interface of MultiMap and written in the style of the container package.

References: Wikipedia, Guava

Installation

Install the package via the following:

go get -u github.com/jwangsadinata/go-multimap

Usage

The go-multimap package can be used similarly to the following:

// example/example.go
package main

import (
	"fmt"

	"github.com/jwangsadinata/go-multimap/slicemultimap"
)

func main() {
	usPresidents := []struct {
		firstName  string
		middleName string
		lastName   string
		termStart  int
		termEnd    int
	}{
		{"George", "", "Washington", 1789, 1797},
		{"John", "", "Adams", 1797, 1801},
		{"Thomas", "", "Jefferson", 1801, 1809},
		{"James", "", "Madison", 1809, 1817},
		{"James", "", "Monroe", 1817, 1825},
		{"John", "Quincy", "Adams", 1825, 1829},
		{"John", "", "Tyler", 1841, 1845},
		{"James", "", "Polk", 1845, 1849},
		{"Grover", "", "Cleveland", 1885, 1889},
		{"Benjamin", "", "Harrison", 1889, 1893},
		{"Grover", "", "Cleveland", 1893, 1897},
		{"George", "Herbert Walker", "Bush", 1989, 1993},
		{"George", "Walker", "Bush", 2001, 2009},
		{"Barack", "Hussein", "Obama", 2009, 2017},
	}

	m := slicemultimap.New()

	for _, president := range usPresidents {
		m.Put(president.firstName, president.lastName)
	}

	for _, firstName := range m.KeySet() {
		lastNames, _ := m.Get(firstName)
		fmt.Printf("%v: %v\n", firstName, lastNames)
	}
}

Example output:

$ go run example.go
George: [Washington Bush Bush]
John: [Adams Adams Tyler]
Thomas: [Jefferson]
James: [Madison Monroe Polk]
Grover: [Cleveland Cleveland]
Benjamin: [Harrison]
Barack: [Obama]

Benchmarks

To see the benchmark, run the following on each of the sub-packages:

go test -run=NO_TEST -bench . -benchmem -benchtime 1s ./...

Please see the GoDoc API page for a full API listing. For more examples, please consult example_test.go file located in each subpackages.

go-multimap's People

Contributors

jwangsadinata avatar waterschen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

go-multimap's Issues

A small bug with multimap.Entries()

When playing around with the package, entries return type is currently interface{}. Although it works, it is much harder to get the values out into key/value pair from entries, as the current implementation hides the fields of the struct.

A suggestion is to make the return type of Entries to be:

struct {Key interface{}; value interface{}}

This way, the key and value can be accessed relatively easy, and there is a clear type assertions. Thank you.

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.