Generic collections for golang
For still not documented collections check out code and test/example, to understand more.
We'll assume that in each file you'll have import of collection package with qx prefix, to make examples more readable:
import (
gx "github.com/jtomasevic/gonerix/collections"
)
- List[T]
- SimpleSortedList[T]
- SortedList[K, V]
- SortedsSructList[T]
- LinkedList[T] check unit tests until documentation is done
- Stack[T] check unit tests until documentation is done
- Queue[T] check unit tests until documentation is done
- PriorityQueue[T] check unit tests until documentation is done
- Dictionary[K,V] check unit tests until documentation is done
- LRUCache[K, V] [check unit tests until documentation is done] (https://github.com/jtomasevic/gonerix/blob/main/collections/lru_cache.go)
list := gx.List[int]{}
list.Add(1)
list.Add(2)
list.Add(4)
list.Add(8)
fmt.Println(list)
// [1 2 4 8]
First parameter is index where to insert an element of more of them. Second parameter is one or vmore alues to be added to list.
list.Insert(2, 3)
fmt.Println(list)
// [1 2 3 4 8]
list.Insert(4, 5, 6, 7)
fmt.Println(list)
// [1 2 3 4 5 6 7 8]
list.Reverse()
fmt.Println(list)
// [8 7 6 5 4 3 2 1]
list.Remove(7)
fmt.Println(list)
// [8 6 5 4 3 2 1]
list.RemoveAt(2)
fmt.Println(list)
// [8 6 4 3 2 1]
list.RemoveAt(len(list) - 1)
fmt.Println(list)
// [8 6 4 3 2]
res, index := list.Exist(4)
fmt.Printf("element: %v, index: %v\n", res, index)
// element: true, index: 2
res, index = list.Exist(14)
fmt.Printf("element: %v, index: %v\n", res, index)
// element: false, index: -1
This operation applies only to first element found under certain condition.
- Try to find element that satisfied condition in isEqual function (second parameter)
- If found replace with new provided element (first parameter).
- If not found, add it to list.
// Create some structs to be part of List
type CupSize string
const (
Small CupSize = "small"
Medium CupSize = "medium"
Large CupSize = "large"
ExtraLarge CupSize = "extraLarge"
)
type BlackCoffeeOffer struct {
Price int
Size CupSize
}
// Initialise list
products := gx.List[BlackCoffeeOffer]{
{
Price: 100,
Size: Small,
},
{
Price: 150,
Size: Medium,
},
{
Price: 200,
Size: Large,
},
}
products.AddOrReplace(BlackCoffeeOffer{
Price: 250,
Size: ExtraLarge,
}, func(left BlackCoffeeOffer, right BlackCoffeeOffer) bool {
return left.Size == right.Size
})
fmt.Println(products)
// [{100 small} {150 medium} {200 large} {250 extraLarge}]
// define isEqual function as variable
var isEqual gx.IsEqual[BlackCoffeeOffer] = func(left BlackCoffeeOffer, right BlackCoffeeOffer) bool {
return left.Size == right.Size
}
products.AddOrReplace(BlackCoffeeOffer{
Price: 95,
Size: Small,
}, isEqual)
fmt.Println(products)
// [{95 small} {150 medium} {200 large} {250 extraLarge}]
Return list of elements that satisfy function check, provided by expression parameter
cheaperThan200 := products.Find(func(left BlackCoffeeOffer) bool {
return left.Price < 200
})
fmt.Println(cheaperThan200)
// [{95 small} {150 medium}]
Represents a collection of simple values like int, float or string, anything where oprerator '<' is applicable.
list := gx.SimpleSortedList[int](gx.ASC)
list.Add(30)
list.Add(50)
list.Add(20)
list.Add(10)
list.Add(40)
fmt.Println(list)
// {[10 20 30 40 50] asc}
fmt.Println(list.ToList())
// NOTE: ToList is O(1), no transformation just pass internal reference.
// [10 20 30 40 50]
list.Remove(10)
fmt.Println(list.ToList())
// [20 30 40 50]
first := list.First()
fmt.Println(*first)
// 20
last := list.Last()
fmt.Println(*last)
// 50
list.RemoveAt(2)
// before [20 30 40 50]
fmt.Println(list.ToList())
// [20 30 50]
res:= list.IsEmpty()
fmt.Println(list.res)
// false
count := list.Count()
// currently [20 30 50]
fmt.Println(count)
// 3
Represents a collection of key/value pairs that are sorted by the keys and are accessible by key and by index.
list := gx.SortedList[int, Order](gx.ASC)
list.Add(3, Order{
Customer: "C",
OrderNo: 52,
})
list.Add(1, Order{
Customer: "A",
OrderNo: 50,
})
list.Add(4, Order{
Customer: "D",
OrderNo: 53,
})
list.Add(2, Order{
Customer: "B",
OrderNo: 51,
})
fmt.Println(list.ToList())
// [{A 50} {B 51} {C 52} {D 53}]
...
...
Mandatory parameter for creating sorted struct list is comapare function.
- If left parameter is lower than right parameter, this is ASC ordered list.
- If left parameter is higher than right parameter, this is DESC ordered list.
type Brand struct {
Name string
}
brands := []Brand{
{
Name: "Diadora",
},
{
Name: "Nike",
},
{
Name: "Addidas",
},
{
Name: "Puma",
},
{
Name: "Nokka",
},
}
sorted := gx.SortedStructList(func(left Brand, right Brand) bool {
return left.Name > right.Name
})
for _, brand := range brands {
sorted.Add(brand)
}
// this is O(1) operation.
fmt.Println(sorted.Values())
// [{Puma} {Nokka} {Nike} {Diadora} {Addidas}]
sorted.Remove(Brand{
Name: "Puma",
})
fmt.Println(sorted.Values())
// [{Nokka} {Nike} {Diadora} {Addidas}]
// non existing
result := sorted.Remove(Brand{
Name: "BOSS",
})
fmt.Println(result)
// false
sorted.RemoveAt(0)
fmt.Println(sorted.Values())
// [{Nike} {Diadora} {Addidas}]
sorted.Size()
// return size of element.