OrderedMap

package
v0.3.26 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 15, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FilterNilValues added in v0.3.2

func FilterNilValues[K comparable, V any](S map[K]*V) map[K]*V

FilterNilValues is a function that iterates over the map and removes elements whose value is nil.

Parameters:

  • S: map of elements.

Returns:

  • map[K]*V: map of elements that satisfy the filter function.

Behaviors:

  • If S is empty, it returns a non-nil empty map.

func KeyOfDuplicate added in v0.3.2

func KeyOfDuplicate[K, V comparable](S map[K]V) (K, bool)

KeyOfDuplicate returns the index of the first duplicate element in the map.

Parameters:

  • S: map of elements.

Returns:

  • K: key of the first duplicate element or a zero value if there are no duplicates.
  • bool: true if there are duplicates, otherwise false.

func KeyOfDuplicateFunc added in v0.3.2

func KeyOfDuplicateFunc[K comparable, V uc.Objecter](S map[K]V) (K, bool)

KeyOfDuplicateFunc returns the index of the first duplicate element in the map.

Parameters:

  • S: map of elements.

Returns:

  • K: key of the first duplicate element or a zero value if there are no duplicates.
  • bool: true if there are duplicates, otherwise false.

func MapFilter added in v0.3.2

func MapFilter[K comparable, V any](S map[K]V, filter PredicateFilter[K, V]) map[K]V

MapFilter is a function that iterates over the map and applies the filter function to each element. The returned map contains the elements that satisfy the filter function.

Parameters:

  • S: map of elements.
  • filter: function that takes an element and returns a bool.

Returns:

  • map[K]V: map of elements that satisfy the filter function.

Behaviors:

  • An element is said to satisfy the filter function if the function returns true for that element.
  • If S is empty, it returns a non-nil empty map.
  • If filter is nil, it returns S as is

func PurgeKeysNotIn added in v0.3.2

func PurgeKeysNotIn[K comparable, V any](M map[K]V, validKeys []K) map[K]V

PurgeKeysNotIn removes elements from the map whose keys are not in the validKeys slice.

Parameters:

  • M: map of elements.
  • validKeys: slice of keys to keep.

Returns:

  • map[K]V: map of elements with keys not in validKeys removed.

Behavior:

  • If validKeys is empty, the function returns an empty map.
  • If M is empty, the function returns an empty map.

func RemoveDuplicates added in v0.3.2

func RemoveDuplicates[K comparable, V comparable](S map[K]V) map[K]V

RemoveDuplicates removes duplicate elements from the map.

Parameters:

  • S: map of elements.

Returns:

  • map[K]V: map of elements with duplicates removed.

Behavior:

  • The function preserves the order of the elements in the map.
  • If there are multiple duplicates of an element, only the first occurrence is kept.
  • If there are less than two elements in the map, the function returns the original map.

func RemoveDuplicatesFunc added in v0.3.2

func RemoveDuplicatesFunc[K comparable, V uc.Objecter](S map[K]V) map[K]V

RemoveDuplicatesFunc removes duplicate elements from the map.

Parameters:

  • S: map of elements.
  • equals: function that takes two elements and returns a bool.

Returns:

  • map[K]V: map of elements with duplicates removed.

Behavior:

  • The function preserves the order of the elements in the map.
  • If there are multiple duplicates of an element, only the first occurrence is kept.
  • If there are less than two elements in the map, the function returns the original map.

func SFSeparate added in v0.3.2

func SFSeparate[K comparable, V any](S map[K]V, filter PredicateFilter[K, V]) (map[K]V, map[K]V)

SFSeparate is a function that iterates over the map and applies the filter function to each element. The returned slices contain the elements that satisfy and do not satisfy the filter function.

Parameters:

  • S: map of elements.
  • filter: function that takes an element and returns a bool.

Returns:

  • map[K]V: map of elements that satisfy the filter function.
  • map[K]V: map of elements that do not satisfy the filter function.

Behavior:

  • If S is empty, the function returns two empty slices.

func SFSeparateEarly added in v0.3.2

func SFSeparateEarly[K comparable, V any](S map[K]V, filter PredicateFilter[K, V]) (map[K]V, bool)

SFSeparateEarly is a variant of SFSeparate that returns all successful elements. If there are none, it returns the original map and false.

Parameters:

  • S: map of elements.
  • filter: function that takes an element and returns a bool.

Returns:

  • map[K]V: map of elements that satisfy the filter function or the original map.
  • bool: true if there are successful elements, otherwise false.

Behavior:

  • If S is empty, the function returns an empty map and true.

Types

type ErrKeyNotFound

type ErrKeyNotFound struct{}

ErrKeyNotFound is an error type that represents a key not found error.

func NewErrKeyNotFound

func NewErrKeyNotFound() *ErrKeyNotFound

NewErrKeyNotFound creates a new ErrKeyNotFound.

Returns:

  • *ErrKeyNotFound: A pointer to the new ErrKeyNotFound.

func (*ErrKeyNotFound) Error

func (e *ErrKeyNotFound) Error() string

Error returns the error message: "key %q not found".

Returns:

  • string: The error message.

type LessMap added in v0.3.2

type LessMap[K MapKeyer, V any] struct {
	// contains filtered or unexported fields
}

LessMap is a generic data structure that represents a sorted map.

func NewLessMap added in v0.3.2

func NewLessMap[K MapKeyer, V any](sf uts.SortFunc[K]) *LessMap[K, V]

NewLessMap creates a new sorted map.

Parameters:

  • sf: The sort function to use for sorting the keys.

Returns:

  • *LessMap[K, V]: A pointer to the newly created sorted map.

Behaviors:

  • If the sort function is nil, the function returns nil.

func (*LessMap[K, V]) AddEntry added in v0.3.2

func (s *LessMap[K, V]) AddEntry(key K, value V)

AddEntry adds an entry to the sorted map.

Parameters:

  • key: The key of the entry.
  • value: The value of the entry.

Behaviors:

  • If the key already exists, the value is updated.

func (*LessMap[K, V]) Copy added in v0.3.2

func (s *LessMap[K, V]) Copy() uc.Copier

Copy creates a deep copy of the sorted map.

Returns:

  • uc.Copier: A deep copy of the sorted map.

func (*LessMap[K, V]) Delete added in v0.3.2

func (s *LessMap[K, V]) Delete(key K)

Delete deletes the entry with the provided key from the sorted map.

Parameters:

  • key: The key of the entry to delete.

Behaviors:

  • If the key does not exist, nothing happens.

func (*LessMap[K, V]) DoFunc added in v0.3.2

func (s *LessMap[K, V]) DoFunc(f func(K, V) error) error

DoFunc performs a function on each entry in the sorted map.

Parameters:

  • f: The function to perform on each entry.

Returns:

  • error: An error if the function fails.

Behaviors:

  • The function 'f' is called for each entry in the sorted map.
  • If the function 'f' returns an error, the iteration stops.

func (*LessMap[K, V]) GetAt added in v0.3.13

func (s *LessMap[K, V]) GetAt(index int) (V, error)

GetAt returns the value at the provided index.

Parameters:

  • index: The index of the value to retrieve.

Returns:

  • V: The value at the provided index.

Errors:

  • *ue.ErrInvalidParameter: The index is out of bounds.

func (*LessMap[K, V]) GetEntries added in v0.3.2

func (s *LessMap[K, V]) GetEntries() []*uc.Pair[K, V]

GetEntries returns the entries in the sorted map.

Returns:

  • []*uc.Pair[K, V]: The entries in the sorted map.

Behaviors:

  • The entries are returned in the order of the keys.
  • There are no nil pairs in the returned slice.
  • Prefer using Iterator() method for iterating over the entries instead of this method.

func (*LessMap[K, V]) GetEntry added in v0.3.2

func (s *LessMap[K, V]) GetEntry(key K) (V, bool)

GetEntry gets the value of the entry with the provided key.

Parameters:

  • key: The key of the entry.

Returns:

  • V: The value of the entry.
  • bool: A boolean indicating if the key exists in the sorted map.

Errors:

  • One can use the error *ErrKeyNotFound from the package when the key does not exist.

func (*LessMap[K, V]) Iterator added in v0.3.2

func (s *LessMap[K, V]) Iterator() ll.Iterater[*uc.Pair[K, V]]

Iterator returns an iterator for the sorted map.

Returns:

  • ll.Iterater[*uc.Pair[K, V]]: An iterator for the sorted map.

Behaviors:

  • The iterator returns the entries in the order of the keys as pairs.

func (*LessMap[K, V]) Keys added in v0.3.2

func (s *LessMap[K, V]) Keys() []K

Keys returns the keys of the entries in the sorted map.

Returns:

  • []K: The keys of the entries in the sorted map.

func (*LessMap[K, V]) ModifyValueFunc added in v0.3.2

func (s *LessMap[K, V]) ModifyValueFunc(key K, f ModifyValueFunc[V]) error

ModifyValueFunc is a method that modifies a value of the sorted map.

Parameters:

  • key: The key of the value to modify.
  • f: The function that modifies the value.

Returns:

  • error: An error if the change fails.

Errors:

  • *ErrKeyNotFound: The key does not exist in the sorted map.
  • Any error returned by the function 'f'.

func (*LessMap[K, V]) Size added in v0.3.2

func (s *LessMap[K, V]) Size() int

Size returns the number of entries in the sorted map.

Returns:

  • int: The number of entries in the sorted map.

func (*LessMap[K, V]) Values added in v0.3.2

func (s *LessMap[K, V]) Values() []V

Values returns the values of the entries in the sorted map.

Returns:

  • []V: The values of the entries in the sorted map.

type MapIterPrinter added in v0.3.5

type MapIterPrinter[T ffs.FStringer] struct {
	// Name is the name of the second element in the key-value pair.
	Name string

	// Iter is the iterator to print.
	Iter ui.Iterater[*uc.Pair[string, T]]
}

MapIterPrinter is a type that represents a printer for a map iterator.

func NewMapIterPrinter added in v0.3.5

func NewMapIterPrinter[T ffs.FStringer](name string, iter ui.Iterater[*uc.Pair[string, T]]) *MapIterPrinter[T]

NewMapIterPrinter is a function that creates a new map iterator printer.

Parameters:

  • name: The name of the second element in the key-value pair.
  • iter: The iterator to print.

Returns:

  • *MapIterPrinter: A pointer to the newly created map iterator printer.

func (*MapIterPrinter[T]) FString added in v0.3.5

func (mip *MapIterPrinter[T]) FString(trav *ffs.Traversor, opts ...ffs.Option) error

FString is a function that prints a map iterator.

Format:

<name>:
	<key 1>:
		<value 1>
	<key 2>:
		<value 2>
	// ...

Parameters:

  • trav: The traversor to use for printing.

Returns:

  • error: An error if the printing fails.

type MapKeyer added in v0.3.13

type MapKeyer interface {
	uc.Copier
}

MapKeyer is an interface that represents a key of a map.

type ModifyValueFunc

type ModifyValueFunc[V any] func(V) (V, error)

ModifyValueFunc is a function that modifies a value.

Parameters:

  • V: The value to modify.

Returns:

  • V: The modified value.

type OrderedMap

type OrderedMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

OrderedMap is a generic data structure that represents a sorted map.

func NewOrderedMap

func NewOrderedMap[K comparable, V any]() *OrderedMap[K, V]

NewOrderedMap creates a new sorted map.

Returns:

  • *SortedMap[K, V]: A pointer to the newly created sorted map.

func (*OrderedMap[K, V]) AddEntry

func (s *OrderedMap[K, V]) AddEntry(key K, value V)

AddEntry adds an entry to the sorted map.

Parameters:

  • key: The key of the entry.
  • value: The value of the entry.

Behaviors:

  • If the key already exists, the value is updated.

func (*OrderedMap[K, V]) Copy

func (s *OrderedMap[K, V]) Copy() uc.Copier

Copy creates a shallow copy of the sorted map.

Returns:

  • uc.Copier: A shallow copy of the sorted map.

func (*OrderedMap[K, V]) Delete

func (s *OrderedMap[K, V]) Delete(key K)

Delete deletes the entry with the provided key from the sorted map.

Parameters:

  • key: The key of the entry to delete.

Behaviors:

  • If the key does not exist, nothing happens.

func (*OrderedMap[K, V]) DoFunc

func (s *OrderedMap[K, V]) DoFunc(f func(K, V) error) error

DoFunc performs a function on each entry in the sorted map.

Parameters:

  • f: The function to perform on each entry.

Returns:

  • error: An error if the function fails.

Behaviors:

  • The function 'f' is called for each entry in the sorted map.
  • If the function 'f' returns an error, the iteration stops.

func (*OrderedMap[K, V]) Equals added in v0.3.2

func (s *OrderedMap[K, V]) Equals(other uc.Equaler) bool

Equals implements common.Objecter.

func (*OrderedMap[K, V]) GetAt added in v0.3.13

func (s *OrderedMap[K, V]) GetAt(index int) (V, error)

GetAt returns the value at the provided index.

Parameters:

  • index: The index of the value to retrieve.

Returns:

  • V: The value at the provided index.

Errors:

  • *ue.ErrInvalidParameter: The index is out of bounds.

func (*OrderedMap[K, V]) GetEntries

func (s *OrderedMap[K, V]) GetEntries() []*uc.Pair[K, V]

GetEntries returns the entries in the sorted map.

Returns:

  • []*uc.Pair[K, V]: The entries in the sorted map.

Behaviors:

  • The entries are returned in the order of the keys.
  • There are no nil pairs in the returned slice.
  • Prefer using Iterator() method for iterating over the entries instead of this method.

func (*OrderedMap[K, V]) GetEntry

func (s *OrderedMap[K, V]) GetEntry(key K) (V, bool)

GetEntry gets the value of the entry with the provided key.

Parameters:

  • key: The key of the entry.

Returns:

  • V: The value of the entry.
  • bool: A boolean indicating if the key exists in the sorted map.

Errors:

  • One can use the error *ErrKeyNotFound from the package when the key does not exist.

func (*OrderedMap[K, V]) Iterator

func (s *OrderedMap[K, V]) Iterator() ll.Iterater[*uc.Pair[K, V]]

Iterator returns an iterator for the sorted map.

Returns:

  • ll.Iterater[*uc.Pair[K, V]]: An iterator for the sorted map.

Behaviors:

  • The iterator returns the entries in the order of the keys as pairs.

func (*OrderedMap[K, V]) Keys

func (s *OrderedMap[K, V]) Keys() []K

Keys returns the keys of the entries in the sorted map.

Returns:

  • []K: The keys of the entries in the sorted map.

func (*OrderedMap[K, V]) ModifyValueFunc

func (s *OrderedMap[K, V]) ModifyValueFunc(key K, f ModifyValueFunc[V]) error

ModifyValueFunc is a method that modifies a value of the sorted map.

Parameters:

  • key: The key of the value to modify.
  • f: The function that modifies the value.

Returns:

  • error: An error if the change fails.

Errors:

  • *ErrKeyNotFound: The key does not exist in the sorted map.
  • Any error returned by the function 'f'.

func (*OrderedMap[K, V]) Size

func (s *OrderedMap[K, V]) Size() int

Size returns the number of entries in the sorted map.

Returns:

  • int: The number of entries in the sorted map.

func (*OrderedMap[K, V]) SortKeys

func (s *OrderedMap[K, V]) SortKeys(less func(K, K) int)

SortKeys sorts the keys of the sorted map.

Parameters:

  • less: The function that defines the sorting order.

Behaviors:

  • The keys are sorted in place using the slice.SortFunc function.
  • The function 'less' should return < 0 if the first key is less than the second key.
  • The function 'less' should return > 0 if the first key is greater than the second key.
  • The function 'less' should return 0 if the first key is equal to the second key.

func (*OrderedMap[K, V]) String added in v0.3.2

func (s *OrderedMap[K, V]) String() string

String implements common.Objecter.

func (*OrderedMap[K, V]) Values

func (s *OrderedMap[K, V]) Values() []V

Values returns the values of the entries in the sorted map.

Returns:

  • []V: The values of the entries in the sorted map.

type OrderedMapPrinter added in v0.3.5

type OrderedMapPrinter[T ffs.FStringer] struct {
	// The name of the ordered map.
	Name string

	// The ordered map to print.
	Map *OrderedMap[string, T]

	// ValueName is the name of the value in the ordered map.
	ValueName string

	// IfEmpty is the string to print if the ordered map is empty.
	IfEmpty string
}

OrderedMapPrinter is a type that represents a printer for an ordered map.

func NewOrderedMapPrinter added in v0.3.5

func NewOrderedMapPrinter[T ffs.FStringer](name string, elem *OrderedMap[string, T], valueName string, ifEmpty string) *OrderedMapPrinter[T]

NewOrderedMapPrinter is a function that creates a new ordered map printer.

Parameters:

  • name: The name of the ordered map.
  • elem: The ordered map to print.
  • valueName: The name of the value in the ordered map.
  • ifEmpty: The string to print if the ordered map is empty.

Returns:

  • *OrderedMapPrinter: A pointer to the newly created ordered map printer.

func (*OrderedMapPrinter[T]) FString added in v0.3.5

func (p *OrderedMapPrinter[T]) FString(trav *ffs.Traversor, opts ...ffs.Option) error

FString is a function that prints an ordered map.

Format:

<name>:
	<key 1>:
		<value 1>
	<key 2>:
		<value 2>
	// ...

or

<name>: <ifEmpty>

Parameters:

  • trav: The traversor to use for printing.

Returns:

  • error: An error if the printing fails.

type PredicateFilter added in v0.3.2

type PredicateFilter[K comparable, V any] func(K, V) bool

PredicateFilter is a type that defines a map filter function.

Parameters:

  • K: The type of the elements in the map.

Returns:

  • bool: True if the element satisfies the filter function, otherwise false.

func Intersect added in v0.3.2

func Intersect[K comparable, V any](funcs ...PredicateFilter[K, V]) PredicateFilter[K, V]

Intersect returns a PredicateFilter function that checks if an element satisfies all the PredicateFilter functions in funcs.

Parameters:

  • funcs: A map of PredicateFilter functions.

Returns:

  • PredicateFilter: A PredicateFilter function that checks if a element satisfies all the PredicateFilter functions in funcs.

Behaviors:

  • It returns false as soon as it finds a function in funcs that the element does not satisfy.
  • If no functions are provided, it returns a function that always returns true.

func ParallelIntersect added in v0.3.2

func ParallelIntersect[K comparable, V any](funcs ...PredicateFilter[K, V]) PredicateFilter[K, V]

ParallelIntersect returns a PredicateFilter function that checks if an element satisfies all the PredicateFilter functions in funcs. It runs the PredicateFilter functions in parallel and returns false as soon as it finds a function in funcs that the element does not satisfy.

Parameters:

  • funcs: A map of PredicateFilter functions.

Returns:

  • PredicateFilter: A PredicateFilter function that checks if a element satisfies all the PredicateFilter functions in funcs.

Behaviors:

  • It returns false as soon as it finds a function in funcs that the element does not satisfy.
  • If no functions are provided, it returns a function that always returns true.

func ParallelUnion added in v0.3.2

func ParallelUnion[K comparable, V any](funcs ...PredicateFilter[K, V]) PredicateFilter[K, V]

ParallelUnion returns a PredicateFilter function that checks if an element satisfies at least one of the PredicateFilter functions in funcs. It runs the PredicateFilter functions in parallel and returns true as soon as it finds a function in funcs that the element satisfies.

Parameters:

  • funcs: A map of PredicateFilter functions.

Returns:

  • PredicateFilter: A PredicateFilter function that checks if a element satisfies at least one of the PredicateFilter functions in funcs.

Behaviors:

  • It returns true as soon as it finds a function in funcs that the element satisfies.
  • If no functions are provided, it returns a function that always returns false.

func Union added in v0.3.2

func Union[K comparable, V any](funcs ...PredicateFilter[K, V]) PredicateFilter[K, V]

Union returns a PredicateFilter function that checks if an element satisfies at least one of the PredicateFilter functions in funcs. It returns true as soon as it finds a function in funcs that the element satisfies.

Parameters:

  • funcs: A map of PredicateFilter functions.

Returns:

  • PredicateFilter: A PredicateFilter function that checks if a element satisfies at least one of the PredicateFilter functions in funcs.

Behaviors:

  • It returns true as soon as it finds a function in funcs that the element satisfies.
  • If no functions are provided, it returns a function that always returns false.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL