Skip to content

peculiar‐map

Muhammad Usama edited this page Sep 7, 2023 · 1 revision

GoDoc

Peculiar-Map is an implementation of Map which preserves insertion order while using struct and exposes general functions for re-usability

Here is a quick summary of what peculiar-map offers

Function Description
Set Adds value to collection
SetIfAbsent Adds value to collection only if not exists
Get Gets Value from collection
ContainsKey Checks if "key" is present in the collection
IsEmpty Checks if collection is empty
Size Returns current size of collection
Foreach Executes given function over a collection (in insertion order) without modifications
Entry Executes given function over a collection (in insertion order) without modifications and exposes key & value
Map Executes given function over a collection (in insertion order) with modifications
Keys Returns current keys by insertion order
Clear Clears all contents in the collection
ConcatWithPrePrecedence Concats with provided collection without overwriting conflicting keys
ConcatWithPostPrecedence Concats with provided collection by overwriting conflicting keys

peculiar-map usage

To create a new key value pair instance for int

collection := peculiar.NewMap[int, SampleStruct]()

OR

peculiar.NewMap[string, SampleStruct]()

You can also initialise map with specific size

Or you can create a new instance with existing values by using following functions

values := make(map[string]SampleStruct)
peculiar.NewMap[string, SampleStruct]( values )
peculiar.NewMapOfSize[string, SampleStruct](4)

Where SampleStruct is

type SampleStruct struct {
	FirstName string
	LastName  string
	Age       int
}

Insert Values

To add values to collection

...
collection := peculiar.NewMap[int, SampleStruct]()

sample1 := SampleStruct{
	FirstName: "John",
	LastName:  "Wick",
	Age:       33,
}

sample2 := SampleStruct{
	FirstName: "Chriss",
	LastName:  "Evans",
	Age:       32,
}

sample3 := SampleStruct{
	FirstName: "Johnny",
	LastName:  "Bravo",
	Age:       20,
}

collection.Set(1, sample1)
collection.Set(2, sample2)
collection.Set(3, sample3)
...

You can use the same function to update existing values without disturbing the insertion order

...
sample, _ := collection.Get(2)
sample.Age++
collection.Set(2, sample)
...

Get Values

To get a value from collection

NOTE: Get function returns a value and an error

...
v, e := collection.Get(2)
if e != nil {
	// error case
	...
} else {
	fmt.Println(v)
}
...

Remove Values

To remove the values from collection

...
collection.Remove(2)
...

Miscellaneous Functions

You can iterate over collection using Foreach function

...
var sumOfAges = 0
collection.Foreach(func(v SampleStruct) {
	sumOfAges += v.Age
	fmt.Println(v)
})
fmt.Println("Sum of Ages:", sumOfAges)
...

Or if you wish to get keys too while iterating, you may use Entry function

...
collection.Entry(func(k int, v SampleStruct) {
	fmt.Println(k, v)
})
fmt.Println("Sum of Ages:", sumOfAges)
...

Or you may use Map function if you want to modify underlying values

...
collection.Map(func(v SampleStruct) SampleStruct {
	v.Age++
	fmt.Println(v)
	return v
})
...

Note: Above function will modify the underlying collection, proceed with caution!

If you wish to get all keys from the collection, you may use

...
collection.Keys()
...

...

If you are looking for information on linear collection, here is Peculiar-List wiki

Clone this wiki locally