| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
This package is not in the latest version of its module.
Go to latest Published: Mar 25, 2026 License: MITThe Go module system was introduced in Go 1.11 and is the official dependency management solution for Go.
Redistributable licenses place minimal restrictions on how software can be used, modified, and redistributed.
Modules with tagged versions give importers more predictable builds.
When a project reaches major version v1 it is considered stable.
The fastests sorted map possible for searching by ranges.
Example:
kv:=omap.NewCenterTree[string,string](2,cmp.Compare)
// Save a value
kv.Put("Hello"," ")
kv.Put("World","!\n")
// Itertor
for k,v :=range kv.All {
fmt.Printf("%s%s",k,v)
}
The resulting output will be:
"Hello World!\n"
We can now make things a bit smaller by removing things by a range.
kv.RemoveBetween("Sell","Zoo")
// Itertor
for k,v :=range kv.All {
fmt.Printf("%s%s\n",k,v)
}
The resulting output will now be:
"Hello \n"
The index lookup creates 2 values for each potential key:
Since lookups create both an index position and offsett, it becomes possible to look for the following:
const ( FIRST_KEY = 1 LAST_KEY = 2 )
const MIN_GROWTH = 1
This section is empty.
Returns the index and offset of a given key.
The index is the current relative position in the slice.
The offset represents where the item would be placed:
Complexity: o(log n)
func Merge[K comparable, V any](dst OrderedMap[K, V], src map[K]V) int
Merges a map into an OrderedMap instance.
func ToMap[K comparable, V any](src OrderedMap[K, V]) map[K]V
Utility method, to convert from an OrderedMap instance to a regular map. Due to constraints placed on maps in go, this feature is implemented as a function, not a method.
Takes an existing K,V iterator and returns a thread safe version.
type CenterTree[K any, V any] struct { *SliceTree[K, V] CenteredSlice []KvSet[K, V] Begin int End int }
func NewCenterTree[K any, V any](growth int, cmp func(a, b K) int) *CenterTree[K, V]
func (s *CenterTree[K, V]) Clone() OrderedMap[K, V]
func (s *CenterTree[K, V]) FastMerge(set OrderedMap[K, V]) int
func (s *CenterTree[K, V]) Filter(cb func(K, V) bool)
Deletes all elements that return true
func (s *CenterTree[K, V]) FilterBetween(cb func(k K, v V) bool, a, b K, opt ...int)
func (s *CenterTree[K, V]) Get(k K) (value V, found bool)
Tries to fetch value based on key of k, if k does not exist, found is false.
func (s *CenterTree[K, V]) MassRemove(keys ...K) (total int)
MassRemove implemenets OrderedMap
func (s *CenterTree[K, V]) MassRemoveKV(keys ...K) iter.Seq2[K, V]
MassRemoveKV implemenets OrderedMap
func (s *CenterTree[K, V]) Merge(set OrderedMap[K, V]) int
Merge implements OrderedMap
func (s *CenterTree[K, V]) Put(k K, v V)
Put implements OrderedMap
func (s CenterTree[K, V]) Remove(key K) (value V, ok bool)
Remove implemenets OrderedMap
func (s *CenterTree[K, V]) RemoveAll() (size int)
RemoveAll implemenets OrderedMap
func (s *CenterTree[K, V]) RemoveBetween(a, b K, opt ...int) (total int)
RemoveBetween implements OrderedMap
func (s *CenterTree[K, V]) RemoveBetweenKV(a, b K, opt ...int) (removed iter.Seq2[K, V])
RemoveBetween implements OrderedMap
func (s *CenterTree[K, V]) SetGrowth(grow int)
SetGrowth implements OrderedMap
func (s *CenterTree[K, V]) ToTs() OrderedMap[K, V]
type OrderedMap[K any, V any] interface { // Should return an iterator for all key/value pairs All() iter.Seq2[K, V] // Returns all keys, the int just expected to be a sequential number Keys() iter.Seq[K] // Returns all the values, the int is expected to be a sequential number Values() iter.Seq[V] // Returns true if the key exists, false if it does not. Exists(key K) bool // Returns true if the key is between the first and last key Contains(key K) bool // Sets the key to the value, returns the index id. Put(key K, value V) // Tries to get the value using the given key. // If the key exists, found is set to true, if the key does not exists then found is set to false. Get(key K) (value V, found bool) // Tries to remove the given key, returns value is set if ok is true. Remove(key K) (value V, ok bool) // Removes all elements. // Returns how many element were removed. RemoveAll() int // Attempts to remove all keys, returns the number of keys removed. MassRemove(keys ...K) (total int) // Attempts to remove all keys, returns an iterator with the key/value pairs MassRemoveKV(keys ...K) iter.Seq2[K, V] // Returns the current number of key/value pairs. Size() int // If ok is true, returns the first key. FirstKey() (key K, ok bool) // If ok is true, returns the last key. LastKey() (key K, ok bool) // Returns total number of elements between a and b. // Neither a or b are required to exist. // // The the optional opt argument: // - When: opt[0]==omap.FIRST_KEY, a is ignored and the FirstKey is used. // - When: opt[0]==omap.LAST_KEY, b is ignored and the LastKey is used. // - To Ignore both a and b, set: opt[0]==omap.FIRST_KEY|omap.LAST_KEY. Between(a, b K, opt ...int) (total int) // Returns an iterator that contains the key value sets between a and b. // Neither a or b are required to exist. // // The the optional opt argument: // - When: opt[0]==omap.FIRST_KEY, a is ignored and the FirstKey is used. // - When: opt[0]==omap.LAST_KEY, b is ignored and the LastKey is used. // - To Ignore both a and b, set: opt[0]==omap.FIRST_KEY|omap.LAST_KEY. BetweenKV(a, b K, opt ...int) (seq iter.Seq2[K, V]) // Trys to delete the elements between a and b, returns the total number of elements deleted. // Neither a or b are required to exist. // // The the optional opt argument: // - When: opt[0]==omap.FIRST_KEY, a is ignored and the FirstKey is used. // - When: opt[0]==omap.LAST_KEY, b is ignored and the LastKey is used. // - To Ignore both a and b, set: opt[0]==omap.FIRST_KEY|omap.LAST_KEY. RemoveBetween(a, b K, opt ...int) (total int) // Trys to delete the elements between a and b, returns an iterator that contains removed K,V pairs. // Neither a or b are required to exist. // // The the optional opt argument: RemoveBetweenKV(a, b K, opt ...int) (seq iter.Seq2[K, V]) // Should return true if the instance is thread safe, fakse if not. ThreadSafe() bool // Merges a given [OrderedMap] into this one. // Returns the number of keys added Merge(set OrderedMap[K, V]) int // Sets the overwrite notice. Its good to know when things change! SetOverwrite(cb func(key K, oldValue, newValue V)) // Sets the growth value. SetGrowth(grow int) // Returns a thread safe instance. // If the instance is all ready thread safe, then the current instance is returned. ToTs() OrderedMap[K, V] // Deletes the given element when the callback returns true Filter(func(k K, v V) bool) // Like filter but only runs between elemetns a and b FilterBetween(cb func(k K, v V) bool, a, b K, args ...int) // Returns the current full Array GetKvSlice() []KvSet[K, V] // Fast merge operation always o(1). // This requires both this instance and the set are in the same order. FastMerge(set OrderedMap[K, V]) int // Returns a slice containing the keys and values between a and b GetBetweenKvSlice(a, b K, opt ...int) []KvSet[K, V] // Returns a new instance based on the current instance Clone() OrderedMap[K, V] }
func NewTs[K any, V any](Cmp func(a, b K) int) (Map OrderedMap[K, V])
Creates a new thread safe OrderedMap instance.
type SliceTree[K any, V any] struct { // Internally managed keys slice Slices []KvSet[K, V] // Compare function. Cmp func(a, b K) int // Required non 0 value, determines by what capacity we grow the internal // Slice. Default is 1. Growth int // Required non nil value, called when ever a value is overwritten. // Seting this function saves on having to write a check when data is overwritten. OnOverWrite func(key K, oldValue V, newValue V) }
Creates a new SliceTee with the default Slices size of 100. If you require more control over the starting size of the slice use the NewSliceTree function in stead.
func NewFromMap[K comparable, V any](m map[K]V, cb func(a, b K) int) *SliceTree[K, V]
Creatss a new SliceTree with the internal Slice set to "size".
Returns an iterator for key/value pars. The internals of this iterator do not lock the tree or prevent updates. You can safely call an iterator from with an iterator. and not run into deadlocks.
Between implements OrderedMap
BetweenKV implements OrderedMap
func (s *SliceTree[K, V]) Clone() OrderedMap[K, V]
func (s *SliceTree[K, V]) FastMerge(set OrderedMap[K, V]) int
Deletes the given element when the callback returns true
GetFirstKey implements OrderedMap
Tries to fetch value based on key of k, if k does not exist, found is false.
Returns a slice containing the elements between a and b
Returns an iterator for the current keys. The internals of this iterator do not lock the tree or prevent updates. You can safely call an iterator from with an iterator. and not run into deadlocks.
GetLastKey implements OrderedMap
Attempts to remove the keys from the tree in bulk. Returns the number of keys removed.
This is almost always faster than just looping over a list of keys and calling Remove one key at a time. The internals of The MassRemove method deletes elements in squential contiguys blocks: reducing on the number of internal splice operations.
Complexity:
Worst case shown (Per key removed or k): o(log(n) + o(log k) + 2*k).
In truth, the real world complexity is drastically reduced by the following:
The complexity is defined by the steps required:
func (s *SliceTree[K, V]) Merge(set OrderedMap[K, V]) int
Merge implements OrderedMap
func (s *SliceTree[K, V]) Put(k K, v V)
Sets the key/vale pair and returns the index id. Comlexity: o(log n)
Tries to remove the element of k, returns false if it fails. Complexity: o(log n)
Removes all elements in the slice, but keeps the memory allocated.
RemoveBetween implements OrderedMap
RemoveBetween implements OrderedMap
Sets the value in the index to v. The last index value returned from Put to update the last index point. This lets you bypass the o(log n) update complexity for writing to the same element over and over again. The internals still call s.OnOverWrite for you.
SetGrowth implements OrderedMap
Sets the given k,v pair based on the index and offset provided by a call to GetIndex. Returns the resulting array index id.
Using a combination of GetIndex and SetIndex lets you bypass the o(log n) comlexity when wiring to the same node over and over again. The value reutrned from Put can be used to update the internals using SetIndex with the offset being 0.
func (s *SliceTree[K, V]) SetOverwrite(cb func(key K, oldValue, newValue V))
Sets the internal OnOverWrite function.
func (s *SliceTree[K, V]) ToTs() OrderedMap[K, V]
Returns a thread safe instnace from the current instance.
func (s *SliceTree[K, V]) UnSafeMassRemove(keys ...K)
This method is by defenition, unsafe, but fast.
Only use if the keys being removed meet all of the following requirements:
Complexity: key=keys; o(log n +k)
type ThreadSafeOrderedMap[K any, V any] struct { // Instance to wrap for locking Tree OrderedMap[K, V] // contains filtered or unexported fields }
func (s *ThreadSafeOrderedMap[K, V]) All() iter.Seq2[K, V]
All implements OrderedMap.
func (s *ThreadSafeOrderedMap[K, V]) Between(a, b K, opt ...int) (total int)
Between implements OrderedMap
func (s *ThreadSafeOrderedMap[K, V]) BetweenKV(a, b K, opt ...int) (seq iter.Seq2[K, V])
BetweenKV implements OrderedMap
func (s *ThreadSafeOrderedMap[K, V]) Clone() OrderedMap[K, V]
Returns a new thread safe copy of this instance
func (s *ThreadSafeOrderedMap[K, V]) Contains(key K) bool
func (s *ThreadSafeOrderedMap[K, V]) Exists(key K) bool
Exists implements OrderedMap.
func (s *ThreadSafeOrderedMap[K, V]) FastMerge(set OrderedMap[K, V]) int
func (s *ThreadSafeOrderedMap[K, V]) Filter(cb func(K, V) bool)
func (s *ThreadSafeOrderedMap[K, V]) FilterBetween(cb func(k K, v V) bool, a, b K, opt ...int)
func (s *ThreadSafeOrderedMap[K, V]) FirstKey() (key K, ok bool)
GetFirstKey implements OrderedMap
func (s *ThreadSafeOrderedMap[K, V]) Get(key K) (value V, found bool)
Get implements OrderedMap.
func (s *ThreadSafeOrderedMap[K, V]) GetBetweenKvSlice(a, b K, opt ...int) []KvSet[K, V]
func (s *ThreadSafeOrderedMap[K, V]) GetKvSlice() []KvSet[K, V]
func (s *ThreadSafeOrderedMap[K, V]) Keys() iter.Seq[K]
Keys implements OrderedMap.
func (s *ThreadSafeOrderedMap[K, V]) LastKey() (key K, ok bool)
GetLastKey implements OrderedMap
func (s *ThreadSafeOrderedMap[K, V]) MassRemove(keys ...K) (total int)
MassRemove implements OrderedMap.
func (s *ThreadSafeOrderedMap[K, V]) MassRemoveKV(keys ...K) iter.Seq2[K, V]
func (s *ThreadSafeOrderedMap[K, V]) Merge(set OrderedMap[K, V]) int
Merge implements OrderedMap
func (s *ThreadSafeOrderedMap[K, V]) Put(key K, value V)
Put implements OrderedMap.
func (s *ThreadSafeOrderedMap[K, V]) Remove(key K) (V, bool)
Remove implements OrderedMap.
func (s *ThreadSafeOrderedMap[K, V]) RemoveAll() int
RemoveAll implements OrderedMap.
func (s *ThreadSafeOrderedMap[K, V]) RemoveBetween(a, b K, opt ...int) (total int)
RemoveBetween implements OrderedMap.
func (s *ThreadSafeOrderedMap[K, V]) RemoveBetweenKV(a, b K, opt ...int) (seq iter.Seq2[K, V])
RemoveBetweenKV implements OrderedMap.
func (s *ThreadSafeOrderedMap[K, V]) SetGrowth(grow int)
func (s *ThreadSafeOrderedMap[K, V]) SetOverwrite(cb func(key K, oldValue, newValue V))
SetOverwrite implements OrderedMap
func (s *ThreadSafeOrderedMap[K, V]) Size() int
Size implements OrderedMap.
func (s *ThreadSafeOrderedMap[K, V]) ThreadSafe() bool
Always returns true.
func (s *ThreadSafeOrderedMap[K, V]) ToTs() OrderedMap[K, V]
Always returns this instance.
func (s *ThreadSafeOrderedMap[K, V]) Values() iter.Seq[V]
Values implements OrderedMap.
| Back | FazBrowse Home | New Git URL |