fastmap

package
v1.1.4 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AppendableHashMap added in v1.1.0

type AppendableHashMap[K comparable, V any] struct {
	*HashMap[K, []V]
}

AppendableHashMap extends HashMap to provide specialized functionality for handling slice operations. It maintains type safety through generics while providing convenient methods for appending values to existing slices within the map.

Example:

layoutMap := NewAppendableHashMap[string, Component]()
layoutMap.AppendValues("section1", component1, component2)
layoutMap.AppendValues("section1", component3) // Appends to existing slice

func NewAppendableHashMap added in v1.1.0

func NewAppendableHashMap[K comparable, V any]() *AppendableHashMap[K, V]

NewAppendableHashMap creates a new instance of AppendableHashMap that safely manages slices of values associated with keys. It initializes an underlying HashMap to store the key-value pairs where values are slices.

Example:

map := NewAppendableHashMap[string, int]()
map.AppendValues("key1", 1, 2, 3)

func (*AppendableHashMap[K, V]) AppendValues added in v1.1.0

func (h *AppendableHashMap[K, V]) AppendValues(key K, values ...V)

AppendValues appends multiple values to an existing slice for a given key. If the key doesn't exist, it creates a new slice with the provided values. This method provides a safe way to handle the spread operator equivalent in Go.

Parameters:

  • key: The key to associate the values with
  • values: Variadic parameter of values to append

Example:

map.AppendValues("components", component1, component2)
map.AppendValues("components", component3) // Appends to existing slice

func (*AppendableHashMap[K, V]) FilterValues added in v1.1.1

func (h *AppendableHashMap[K, V]) FilterValues(key K, predicate func(V) bool) bool

FilterValues applies a predicate function to filter values at the given key. Keeps only values where predicate returns true. Returns true if filtering was performed, false if key doesn't exist.

Example:

map.AppendValues("numbers", 1, 2, 3, 4, 5)
map.FilterValues("numbers", func(n int) bool { return n%2 == 0 })
// Result: [2, 4]

func (*AppendableHashMap[K, V]) RemoveDuplicates added in v1.1.1

func (h *AppendableHashMap[K, V]) RemoveDuplicates(key K, equals func(a, b V) bool) bool

RemoveDuplicates removes duplicate values at the given key while maintaining order. Uses the provided equals function to determine value equality. Returns true if deduplication was performed, false if key doesn't exist.

Example:

map.AppendValues("tags", "go", "java", "go", "python", "java")
map.RemoveDuplicates("tags", func(a, b string) bool { return a == b })
// Result: ["go", "java", "python"]

func (*AppendableHashMap[K, V]) ReverseValues added in v1.1.1

func (h *AppendableHashMap[K, V]) ReverseValues(key K) bool

ReverseValues reverses the order of values stored at the given key. Returns true if reversal was performed, false if key doesn't exist.

Example:

map.AppendValues("items", "a", "b", "c")
map.ReverseValues("items")
// Result: ["c", "b", "a"]

func (*AppendableHashMap[K, V]) SortValues added in v1.1.1

func (h *AppendableHashMap[K, V]) SortValues(key K, less func(a, b V) bool) bool

SortValues sorts the slice stored at the given key using a custom comparison function. Returns true if the key exists and sorting was performed, false otherwise. The less function should return true if a comes before b in the desired order.

Parameters:

  • key: The key whose values should be sorted
  • less: Comparison function that defines the sort order

Example:

map.AppendValues("scores", 75, 82, 90, 65)
map.SortValues("scores", func(a, b int) bool { return a < b })
// Result: [65, 75, 82, 90]

func (*AppendableHashMap[K, V]) SortValuesByField added in v1.1.1

func (h *AppendableHashMap[K, V]) SortValuesByField(key K, extractor func(V) any) bool

SortValuesByField sorts values using a field extractor function that retrieves comparable values (int, string, float64) from the slice elements. Returns true if sorting was performed, false if key doesn't exist.

Example:

type User struct {
    Name string
    Age  int
}
map.AppendValues("users", User{"Bob", 30}, User{"Alice", 25})
map.SortValuesByField("users", func(u User) any { return u.Name })
// Result: [{Alice 25} {Bob 30}]

func (*AppendableHashMap[K, V]) TransformValues added in v1.1.1

func (h *AppendableHashMap[K, V]) TransformValues(key K, transform func(V) V) bool

TransformValues applies a transformation function to all values at the given key. The transform function maps each value to a new value of the same type. Returns true if transformation was performed, false if key doesn't exist.

Example:

map.AppendValues("numbers", 1, 2, 3)
map.TransformValues("numbers", func(n int) int { return n * 2 })
// Result: [2, 4, 6]

type AppendableMultiKeyHashMap added in v1.1.1

type AppendableMultiKeyHashMap[K comparable, V any] struct {
	*MultiKeyHashMap[K, []V]
}

AppendableMultiKeyHashMap extends MultiKeyHashMap to support slice operations

func NewAppendableMultiKeyHashMap added in v1.1.1

func NewAppendableMultiKeyHashMap[K comparable, V any]() *AppendableMultiKeyHashMap[K, V]

NewAppendableMultiKeyHashMap creates a new AppendableMultiKeyHashMap instance

func (*AppendableMultiKeyHashMap[K, V]) AppendValues added in v1.1.1

func (m *AppendableMultiKeyHashMap[K, V]) AppendValues(key K, values ...V) bool

AppendValues appends values to the slice associated with the primary key or any of its aliases

func (*AppendableMultiKeyHashMap[K, V]) AppendValuesWithKeys added in v1.1.1

func (m *AppendableMultiKeyHashMap[K, V]) AppendValuesWithKeys(keys []K, values ...V) bool

AppendValuesWithKeys appends values and associates them with multiple keys

func (*AppendableMultiKeyHashMap[K, V]) GetByExactKeys added in v1.1.3

func (m *AppendableMultiKeyHashMap[K, V]) GetByExactKeys(keys []K) ([]V, bool)

GetByExactKeys retrieves values only if all provided keys are associated with the same value set Returns the values and true if an exact match is found, empty slice and false otherwise

func (*AppendableMultiKeyHashMap[K, V]) GetByExactKeysAll added in v1.1.3

func (m *AppendableMultiKeyHashMap[K, V]) GetByExactKeysAll(keyGroups [][]K) map[K][]V

GetByExactKeysAll returns a map of all entries where the provided keys match exactly Each entry in the result map contains the primary key and its values

func (*AppendableMultiKeyHashMap[K, V]) GetSlice added in v1.1.1

func (m *AppendableMultiKeyHashMap[K, V]) GetSlice(key K) ([]V, bool)

GetSlice returns the slice associated with any key (primary or alias)

func (*AppendableMultiKeyHashMap[K, V]) GetValuesByKeys added in v1.1.2

func (m *AppendableMultiKeyHashMap[K, V]) GetValuesByKeys(keys []K) map[K][]V

GetValuesByKeys retrieves values associated with an array of keys If a key doesn't exist, it will be skipped in the result

func (*AppendableMultiKeyHashMap[K, V]) GetValuesByKeysWithPrimary added in v1.1.2

func (m *AppendableMultiKeyHashMap[K, V]) GetValuesByKeysWithPrimary(keys []K) map[K][]V

GetValuesByKeysWithPrimary retrieves values associated with an array of keys The returned map uses primary keys instead of the provided aliases

func (*AppendableMultiKeyHashMap[K, V]) UpdateSlice added in v1.1.1

func (m *AppendableMultiKeyHashMap[K, V]) UpdateSlice(key K, newValues []V) bool

UpdateSlice updates the entire slice for a given key and its aliases

type ComboKeyHashMap added in v1.1.3

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

ComboKeyHashMap maps combinations of keys to a single value

func NewComboKeyHashMap added in v1.1.3

func NewComboKeyHashMap[K comparable, V any]() *ComboKeyHashMap[K, V]

NewComboKeyHashMap creates a new ComboKeyHashMap

func (*ComboKeyHashMap[K, V]) Get added in v1.1.3

func (m *ComboKeyHashMap[K, V]) Get(keys []K) (V, bool)

Get retrieves a value by its exact key combination

func (*ComboKeyHashMap[K, V]) GetByExactKeys added in v1.1.4

func (m *ComboKeyHashMap[K, V]) GetByExactKeys(keys []K) (V, bool)

GetByExactKeys retrieves a value where provided keys exactly match a key group

func (*ComboKeyHashMap[K, V]) GetByKeys added in v1.1.3

func (m *ComboKeyHashMap[K, V]) GetByKeys(keys []K) (V, bool)

GetByKeys retrieves a value where provided keys are part of the key group

func (*ComboKeyHashMap[K, V]) Put added in v1.1.3

func (m *ComboKeyHashMap[K, V]) Put(keys []K, value V)

Put associates a value with a combination of keys

type FieldConfig

type FieldConfig[T any] struct {
	RowIndex *int
	Handler  func(data map[string]interface{}) *T
}

type HashMap

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

HashMap is a generic key-value store supporting comparable keys and any value type Example:

hashMap := NewHashMap[string, int]()

func FromMap

func FromMap[K comparable, V any](m map[K]V) *HashMap[K, V]

FromMap creates a new HashMap from a regular map Example:

regularMap := map[string]int{"one": 1, "two": 2}
hashMap := FromMap(regularMap)

func NewHashMap

func NewHashMap[K comparable, V any]() *HashMap[K, V]

NewHashMap creates a new empty HashMap Example:

hashMap := NewHashMap[string, User]()

func (*HashMap[K, V]) ApplyFieldConfig

func (h *HashMap[K, V]) ApplyFieldConfig(
	key K,
	config FieldConfig[V],
	data map[string]interface{},
) bool

func (*HashMap[K, V]) Clear

func (h *HashMap[K, V]) Clear()

Clear removes all elements from the HashMap Example:

hashMap.Clear()
fmt.Printf("Size after clear: %d\n", hashMap.Size())

func (*HashMap[K, V]) Contains

func (h *HashMap[K, V]) Contains(key K) bool

Contains checks if a key exists in the HashMap Example:

if hashMap.Contains("user123") {
    fmt.Println("User exists")
}

func (*HashMap[K, V]) Filter

func (h *HashMap[K, V]) Filter(predicate func(K, V) bool) *HashMap[K, V]

Filter returns a new HashMap containing only the elements that satisfy the predicate Example:

activeUsers := hashMap.Filter(func(key string, user User) bool {
    return user.Active
})

func (*HashMap[K, V]) ForEach

func (h *HashMap[K, V]) ForEach(callback func(K, V) error) error

ForEach executes a callback function for each key-value pair and returns an error if the callback fails Example:

err := hashMap.ForEach(func(key string, value User) error {
    if value.IsInvalid() {
        return fmt.Errorf("invalid user data for key %s", key)
    }
    fmt.Printf("User %s: %v\n", key, value)
    return nil
})

func (*HashMap[K, V]) Get

func (h *HashMap[K, V]) Get(key K) (V, bool)

Get retrieves a value by key and returns whether it exists Example:

if user, exists := hashMap.Get("user123"); exists {
    fmt.Printf("Found user: %v\n", user)
}

func (*HashMap[K, V]) HandleFieldConfigs

func (h *HashMap[K, V]) HandleFieldConfigs(
	data []map[string]interface{},
	configs map[K]FieldConfig[V],
	fieldKey K,
) []V

func (*HashMap[K, V]) IsEmpty

func (h *HashMap[K, V]) IsEmpty() bool

IsEmpty returns true if the HashMap has no elements Example:

if hashMap.IsEmpty() {
    fmt.Println("HashMap is empty")
}

func (*HashMap[K, V]) Keys

func (h *HashMap[K, V]) Keys() []K

Keys returns a slice of all keys in the HashMap Example:

keys := hashMap.Keys()
for _, key := range keys {
    fmt.Printf("Key: %v\n", key)
}

func (*HashMap[K, V]) Map

func (h *HashMap[K, V]) Map(transform func(K, V) V) *HashMap[K, V]

Map transforms values using the provided function and returns a new HashMap Example:

upperNames := hashMap.Map(func(key string, user User) User {
    user.Name = strings.ToUpper(user.Name)
    return user
})

func (*HashMap[K, V]) ProcessFieldConfigs

func (h *HashMap[K, V]) ProcessFieldConfigs(
	configs map[K]FieldConfig[V],
	data []map[string]interface{},
	processor func(key K, value V, index int),
)

func (*HashMap[K, V]) Put

func (h *HashMap[K, V]) Put(key K, value V)

Put adds or updates a key-value pair in the HashMap Example:

hashMap.Put("user123", User{Name: "John"})

func (*HashMap[K, V]) PutAll

func (h *HashMap[K, V]) PutAll(other *HashMap[K, V])

PutAll adds all key-value pairs from another HashMap Example:

otherMap := NewHashMap[string, User]()
otherMap.Put("user456", newUser)
hashMap.PutAll(otherMap)

func (*HashMap[K, V]) Remove

func (h *HashMap[K, V]) Remove(key K)

Remove deletes a key-value pair from the HashMap Example:

hashMap.Remove("user123")

func (*HashMap[K, V]) Size

func (h *HashMap[K, V]) Size() int

Size returns the number of elements in the HashMap Example:

count := hashMap.Size()
fmt.Printf("HashMap contains %d elements\n", count)

func (*HashMap[K, V]) ToMap

func (h *HashMap[K, V]) ToMap() map[K]V

ToMap returns the underlying map Example:

standardMap := hashMap.ToMap()
for k, v := range standardMap {
    fmt.Printf("%v: %v\n", k, v)
}

func (*HashMap[K, V]) UpdateValue

func (h *HashMap[K, V]) UpdateValue(id K, newValue V) bool

UpdateValue updates an existing value by key, returns false if key doesn't exist Example:

if hashMap.UpdateValue("user123", updatedUser) {
    fmt.Println("User updated successfully")
}

func (*HashMap[K, V]) Values

func (h *HashMap[K, V]) Values() []V

Values returns a slice of all values in the HashMap Example:

values := hashMap.Values()
for _, value := range values {
    fmt.Printf("Value: %v\n", value)
}

type KeyGroup added in v1.1.3

type KeyGroup[K comparable] []K

type MultiKeyHashMap added in v1.1.1

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

MultiKeyHashMap extends HashMap to support multiple keys (aliases) for accessing values. It maintains a primary key and optional aliases for each value.

func NewMultiKeyHashMap added in v1.1.1

func NewMultiKeyHashMap[K comparable, V any]() *MultiKeyHashMap[K, V]

NewMultiKeyHashMap creates a new MultiKeyHashMap instance Example:

map := NewMultiKeyHashMap[string, User]()

func (*MultiKeyHashMap[K, V]) AddAlias added in v1.1.1

func (m *MultiKeyHashMap[K, V]) AddAlias(existingKey K, newAlias K) bool

AddAlias adds a new alias to an existing key Example:

success := map.AddAlias("main", "newAlias")

func (*MultiKeyHashMap[K, V]) Clear added in v1.1.1

func (m *MultiKeyHashMap[K, V]) Clear()

Clear removes all entries from the map Example:

map.Clear()

func (*MultiKeyHashMap[K, V]) Get added in v1.1.1

func (m *MultiKeyHashMap[K, V]) Get(key K) (V, bool)

Get retrieves a value using any associated key (primary or alias) Example:

if value, exists := map.Get("alias1"); exists {
    fmt.Printf("Found: %v\n", value)
}

func (*MultiKeyHashMap[K, V]) GetAllKeys added in v1.1.1

func (m *MultiKeyHashMap[K, V]) GetAllKeys(key K) []K

GetAllKeys returns all keys (primary and aliases) associated with a given key Example:

keys := map.GetAllKeys("alias1")
fmt.Printf("All keys: %v\n", keys)

func (*MultiKeyHashMap[K, V]) GetPrimaryKey added in v1.1.1

func (m *MultiKeyHashMap[K, V]) GetPrimaryKey(key K) (K, bool)

GetPrimaryKey returns the primary key for any given key (alias or primary) Example:

if primaryKey, exists := map.GetPrimaryKey("alias1"); exists {
    fmt.Printf("Primary: %v\n", primaryKey)
}

func (*MultiKeyHashMap[K, V]) Put added in v1.1.1

func (m *MultiKeyHashMap[K, V]) Put(keys []K, value V)

Put adds or updates a value with multiple keys where first key is primary Example:

map.Put([]string{"main", "alias1", "alias2"}, value)

func (*MultiKeyHashMap[K, V]) Remove added in v1.1.1

func (m *MultiKeyHashMap[K, V]) Remove(key K)

Remove removes a key and potentially its connected keys If removing primary key, all aliases are removed If removing alias, only that alias is removed Example:

map.Remove("main")  // Removes main key and aliases
map.Remove("alias") // Removes only the alias

func (*MultiKeyHashMap[K, V]) RemoveWithCascade added in v1.1.1

func (m *MultiKeyHashMap[K, V]) RemoveWithCascade(key K)

RemoveWithCascade removes a key and all connected keys through alias relationships Example:

map.RemoveWithCascade("main") // Removes main key and all connected keys

func (*MultiKeyHashMap[K, V]) Size added in v1.1.1

func (m *MultiKeyHashMap[K, V]) Size() int

Size returns the number of unique primary keys Example:

count := map.Size()
fmt.Printf("Unique entries: %d\n", count)

type ThreadSafeAppendableHashMap added in v1.1.0

type ThreadSafeAppendableHashMap[K comparable, V any] struct {
	*ThreadSafeHashMap[K, []V]
}

ThreadSafeAppendableHashMap provides thread-safe operations for handling slice values in a concurrent environment. It uses mutex locks to ensure safe access and modification of the underlying data structure.

Example:

safeMap := NewThreadSafeAppendableHashMap[string, Component]()
// Safe for concurrent access
go func() { safeMap.AppendValues("section1", component1) }()
go func() { safeMap.AppendValues("section1", component2) }()

func NewThreadSafeAppendableHashMap added in v1.1.0

func NewThreadSafeAppendableHashMap[K comparable, V any]() *ThreadSafeAppendableHashMap[K, V]

NewThreadSafeAppendableHashMap creates a new instance of ThreadSafeAppendableHashMap that provides synchronized access to slice operations. It's suitable for concurrent environments where multiple goroutines might append values simultaneously.

Example:

safeMap := NewThreadSafeAppendableHashMap[string, int]()
// Safe for concurrent operations
go func() { safeMap.AppendValues("key1", 1, 2) }()

func (*ThreadSafeAppendableHashMap[K, V]) AppendValues added in v1.1.0

func (t *ThreadSafeAppendableHashMap[K, V]) AppendValues(key K, values ...V)

AppendValues safely appends multiple values to an existing slice for a given key in a thread-safe manner. It uses mutex locks to ensure concurrent safety during the append operation.

Parameters:

  • key: The key to associate the values with
  • values: Variadic parameter of values to append

Example:

safeMap.AppendValues("users", user1, user2)
// Concurrent access is safe
go safeMap.AppendValues("users", user3)

func (*ThreadSafeAppendableHashMap[K, V]) FilterValues added in v1.1.1

func (t *ThreadSafeAppendableHashMap[K, V]) FilterValues(key K, predicate func(V) bool) bool

FilterValues filters values in a thread-safe manner using a predicate. Returns true if filtering was performed, false if key doesn't exist.

Example:

safeMap.AppendValues("numbers", 1, 2, 3, 4, 5)
safeMap.FilterValues("numbers", func(n int) bool { return n%2 == 0 })
// Result: [2, 4]

func (*ThreadSafeAppendableHashMap[K, V]) RemoveDuplicates added in v1.1.1

func (t *ThreadSafeAppendableHashMap[K, V]) RemoveDuplicates(key K, equals func(a, b V) bool) bool

RemoveDuplicates removes duplicates in a thread-safe manner. Returns true if deduplication was performed, false if key doesn't exist.

Example:

safeMap.AppendValues("tags", "go", "java", "go", "python", "java")
safeMap.RemoveDuplicates("tags", func(a, b string) bool { return a == b })
// Result: ["go", "java", "python"]

func (*ThreadSafeAppendableHashMap[K, V]) ReverseValues added in v1.1.1

func (t *ThreadSafeAppendableHashMap[K, V]) ReverseValues(key K) bool

ReverseValues reverses values order in a thread-safe manner. Returns true if reversal was performed, false if key doesn't exist.

Example:

safeMap.AppendValues("items", "a", "b", "c")
safeMap.ReverseValues("items")
// Result: ["c", "b", "a"]

func (*ThreadSafeAppendableHashMap[K, V]) SortValues added in v1.1.1

func (t *ThreadSafeAppendableHashMap[K, V]) SortValues(key K, less func(a, b V) bool) bool

SortValues sorts the slice in a thread-safe manner using the custom comparison function. Returns true if sorting was performed, false if key doesn't exist.

Example:

safeMap.AppendValues("scores", 75, 82, 90, 65)
safeMap.SortValues("scores", func(a, b int) bool { return a < b })
// Result: [65, 75, 82, 90]

func (*ThreadSafeAppendableHashMap[K, V]) SortValuesByField added in v1.1.1

func (t *ThreadSafeAppendableHashMap[K, V]) SortValuesByField(key K, extractor func(V) any) bool

SortValuesByField sorts values in a thread-safe manner using a field extractor. Returns true if sorting was performed, false if key doesn't exist.

Example:

type User struct {
    Name string
    Age  int
}
safeMap.AppendValues("users", User{"Bob", 30}, User{"Alice", 25})
safeMap.SortValuesByField("users", func(u User) any { return u.Name })
// Result: [{Alice 25} {Bob 30}]

func (*ThreadSafeAppendableHashMap[K, V]) TransformValues added in v1.1.1

func (t *ThreadSafeAppendableHashMap[K, V]) TransformValues(key K, transform func(V) V) bool

TransformValues transforms values in a thread-safe manner. Returns true if transformation was performed, false if key doesn't exist.

Example:

safeMap.AppendValues("numbers", 1, 2, 3)
safeMap.TransformValues("numbers", func(n int) int { return n * 2 })
// Result: [2, 4, 6]

type ThreadSafeHashMap

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

ThreadSafeHashMap provides thread-safe operations for HashMap through mutex synchronization Example:

safeMap := NewThreadSafeHashMap[string, User]()
safeMap.Put("user1", User{Name: "John"})

func FromThreadSafeMap

func FromThreadSafeMap[K comparable, V any](m map[K]V) *ThreadSafeHashMap[K, V]

FromThreadSafeMap creates a new ThreadSafeHashMap from a regular map Example:

regularMap := map[string]int{"one": 1, "two": 2}
safeMap := FromThreadSafeMap(regularMap)

func NewThreadSafeHashMap

func NewThreadSafeHashMap[K comparable, V any]() *ThreadSafeHashMap[K, V]

NewThreadSafeHashMap creates a new thread-safe HashMap Example:

safeMap := NewThreadSafeHashMap[string, User]()

func (*ThreadSafeHashMap[K, V]) ApplyFieldConfig

func (t *ThreadSafeHashMap[K, V]) ApplyFieldConfig(
	key K,
	config FieldConfig[V],
	data map[string]interface{},
) bool

ApplyFieldConfig applies a single field configuration to data Example:

config := FieldConfig[int]{
    Handler: func(data map[string]interface{}) *int {
        if val, ok := data["value"].(int); ok {
            return &val
        }
        return nil
    },
}
success := safeMap.ApplyFieldConfig("field1", config, data)

func (*ThreadSafeHashMap[K, V]) Clear

func (t *ThreadSafeHashMap[K, V]) Clear()

Clear removes all elements from the ThreadSafeHashMap with write lock Example:

safeMap.Clear()
fmt.Printf("Size after clear: %d\n", safeMap.Size())

func (*ThreadSafeHashMap[K, V]) Contains

func (t *ThreadSafeHashMap[K, V]) Contains(key K) bool

Contains checks if a key exists in the ThreadSafeHashMap with read lock Example:

if safeMap.Contains("user123") {
    fmt.Println("User exists")
}

func (*ThreadSafeHashMap[K, V]) Filter

func (t *ThreadSafeHashMap[K, V]) Filter(predicate func(K, V) bool) *ThreadSafeHashMap[K, V]

Filter returns a new ThreadSafeHashMap containing only the elements that satisfy the predicate with read lock Example:

activeUsers := safeMap.Filter(func(key string, user User) bool {
    return user.Active
})

func (*ThreadSafeHashMap[K, V]) ForEach

func (t *ThreadSafeHashMap[K, V]) ForEach(callback func(K, V) error) error

ForEach executes a callback function for each key-value pair with read lock Example:

err := safeMap.ForEach(func(key string, value User) error {
    if value.IsInvalid() {
        return fmt.Errorf("invalid user data for key %s", key)
    }
    fmt.Printf("User %s: %v\n", key, value)
    return nil
})

func (*ThreadSafeHashMap[K, V]) Get

func (t *ThreadSafeHashMap[K, V]) Get(key K) (V, bool)

Get retrieves a value by key and returns whether it exists with read lock Example:

if user, exists := safeMap.Get("user123"); exists {
    fmt.Printf("Found user: %v\n", user)
}

func (*ThreadSafeHashMap[K, V]) HandleFieldConfigs

func (t *ThreadSafeHashMap[K, V]) HandleFieldConfigs(
	data []map[string]interface{},
	configs map[K]FieldConfig[V],
	fieldKey K,
) []V

HandleFieldConfigs processes data using field configurations and returns results Example:

configs := map[string]FieldConfig[int]{
    "field1": {
        Handler: func(data map[string]interface{}) *int {
            if val, ok := data["value"].(int); ok {
                return &val
            }
            return nil
        },
    },
}
results := safeMap.HandleFieldConfigs(data, configs, "field1")

func (*ThreadSafeHashMap[K, V]) IsEmpty

func (t *ThreadSafeHashMap[K, V]) IsEmpty() bool

IsEmpty returns true if the ThreadSafeHashMap has no elements with read lock Example:

if safeMap.IsEmpty() {
    fmt.Println("HashMap is empty")
}

func (*ThreadSafeHashMap[K, V]) Keys

func (t *ThreadSafeHashMap[K, V]) Keys() []K

Keys returns a slice of all keys in the ThreadSafeHashMap with read lock Example:

keys := safeMap.Keys()
for _, key := range keys {
    fmt.Printf("Key: %v\n", key)
}

func (*ThreadSafeHashMap[K, V]) Map

func (t *ThreadSafeHashMap[K, V]) Map(transform func(K, V) V) *ThreadSafeHashMap[K, V]

Map transforms values using the provided function and returns a new ThreadSafeHashMap with read lock Example:

upperNames := safeMap.Map(func(key string, user User) User {
    user.Name = strings.ToUpper(user.Name)
    return user
})

func (*ThreadSafeHashMap[K, V]) ProcessFieldConfigs

func (t *ThreadSafeHashMap[K, V]) ProcessFieldConfigs(
	configs map[K]FieldConfig[V],
	data []map[string]interface{},
	processor func(key K, value V, index int),
)

ProcessFieldConfigs processes data using field configurations with a callback Example:

configs := map[string]FieldConfig[int]{
    "field1": {
        Handler: func(data map[string]interface{}) *int {
            if val, ok := data["value"].(int); ok {
                return &val
            }
            return nil
        },
    },
}
safeMap.ProcessFieldConfigs(configs, data, func(key string, value int, index int) {
    fmt.Printf("Processed: %s = %d at index %d\n", key, value, index)
})

func (*ThreadSafeHashMap[K, V]) Put

func (t *ThreadSafeHashMap[K, V]) Put(key K, value V)

Put adds or updates a key-value pair in the ThreadSafeHashMap with write lock Example:

safeMap.Put("user123", User{Name: "John"})

func (*ThreadSafeHashMap[K, V]) PutAll

func (t *ThreadSafeHashMap[K, V]) PutAll(other *ThreadSafeHashMap[K, V])

PutAll adds all key-value pairs from another ThreadSafeHashMap with write lock Example:

otherMap := NewThreadSafeHashMap[string, User]()
otherMap.Put("user456", newUser)
safeMap.PutAll(otherMap)

func (*ThreadSafeHashMap[K, V]) Remove

func (t *ThreadSafeHashMap[K, V]) Remove(key K)

Remove deletes a key-value pair from the ThreadSafeHashMap with write lock Example:

safeMap.Remove("user123")

func (*ThreadSafeHashMap[K, V]) Size

func (t *ThreadSafeHashMap[K, V]) Size() int

Size returns the number of elements in the ThreadSafeHashMap with read lock Example:

count := safeMap.Size()
fmt.Printf("HashMap contains %d elements\n", count)

func (*ThreadSafeHashMap[K, V]) ToMap

func (t *ThreadSafeHashMap[K, V]) ToMap() map[K]V

ToMap returns the underlying map with read lock Example:

standardMap := safeMap.ToMap()
for k, v := range standardMap {
    fmt.Printf("%v: %v\n", k, v)
}

func (*ThreadSafeHashMap[K, V]) UpdateValue

func (t *ThreadSafeHashMap[K, V]) UpdateValue(key K, newValue V) bool

UpdateValue updates an existing value by key with write lock, returns false if key doesn't exist Example:

if safeMap.UpdateValue("user123", updatedUser) {
    fmt.Println("User updated successfully")
}

func (*ThreadSafeHashMap[K, V]) Values

func (t *ThreadSafeHashMap[K, V]) Values() []V

Values returns a slice of all values in the ThreadSafeHashMap with read lock Example:

values := safeMap.Values()
for _, value := range values {
    fmt.Printf("Value: %v\n", value)
}

type ThreadSafeMultiKeyHashMap added in v1.1.1

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

ThreadSafeMultiKeyHashMap provides thread-safe operations for MultiKeyHashMap through mutex synchronization

func NewThreadSafeMultiKeyHashMap added in v1.1.1

func NewThreadSafeMultiKeyHashMap[K comparable, V any]() *ThreadSafeMultiKeyHashMap[K, V]

NewThreadSafeMultiKeyHashMap creates a new thread-safe MultiKeyHashMap Example:

safeMap := NewThreadSafeMultiKeyHashMap[string, User]()

func (*ThreadSafeMultiKeyHashMap[K, V]) AddAlias added in v1.1.1

func (t *ThreadSafeMultiKeyHashMap[K, V]) AddAlias(existingKey K, newAlias K) bool

AddAlias adds a new alias to an existing key with write lock Example:

if safeMap.AddAlias("main", "newAlias") {
    fmt.Println("Alias added successfully")
}

func (*ThreadSafeMultiKeyHashMap[K, V]) Clear added in v1.1.1

func (t *ThreadSafeMultiKeyHashMap[K, V]) Clear()

Clear removes all entries from the map with write lock Example:

safeMap.Clear()
fmt.Printf("Size after clear: %d\n", safeMap.Size())

func (*ThreadSafeMultiKeyHashMap[K, V]) Get added in v1.1.1

func (t *ThreadSafeMultiKeyHashMap[K, V]) Get(key K) (V, bool)

Get retrieves a value by key with read lock Example:

if user, exists := safeMap.Get("alias1"); exists {
    fmt.Printf("Found user: %v\n", user)
}

func (*ThreadSafeMultiKeyHashMap[K, V]) GetAllKeys added in v1.1.1

func (t *ThreadSafeMultiKeyHashMap[K, V]) GetAllKeys(key K) []K

GetAllKeys returns all keys (primary and aliases) associated with a given key with read lock Example:

keys := safeMap.GetAllKeys("alias1")
fmt.Printf("All associated keys: %v\n", keys)

func (*ThreadSafeMultiKeyHashMap[K, V]) GetPrimaryKey added in v1.1.1

func (t *ThreadSafeMultiKeyHashMap[K, V]) GetPrimaryKey(key K) (K, bool)

GetPrimaryKey returns the primary key for any given key (alias or primary) with read lock Example:

if primaryKey, exists := safeMap.GetPrimaryKey("alias1"); exists {
    fmt.Printf("Primary key: %v\n", primaryKey)
}

func (*ThreadSafeMultiKeyHashMap[K, V]) Put added in v1.1.1

func (t *ThreadSafeMultiKeyHashMap[K, V]) Put(keys []K, value V)

Put adds a value with multiple keys with write lock Example:

safeMap.Put([]string{"main", "alias1", "alias2"}, user)

func (*ThreadSafeMultiKeyHashMap[K, V]) Remove added in v1.1.1

func (t *ThreadSafeMultiKeyHashMap[K, V]) Remove(key K)

Remove removes a key and its associated aliases with write lock Example:

safeMap.Remove("main")  // Removes main key and its aliases
safeMap.Remove("alias") // Removes only the alias

func (*ThreadSafeMultiKeyHashMap[K, V]) RemoveWithCascade added in v1.1.1

func (t *ThreadSafeMultiKeyHashMap[K, V]) RemoveWithCascade(key K)

RemoveWithCascade removes a key and all connected keys with write lock Example:

safeMap.RemoveWithCascade("main") // Removes main key and all connected keys

func (*ThreadSafeMultiKeyHashMap[K, V]) Size added in v1.1.1

func (t *ThreadSafeMultiKeyHashMap[K, V]) Size() int

Size returns the number of unique primary keys with read lock Example:

count := safeMap.Size()
fmt.Printf("Number of unique entries: %d\n", count)

type ValueCallback

type ValueCallback[K comparable, V any] func(key K, value V)

Jump to

Keyboard shortcuts

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