fastmap

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2025 License: MIT Imports: 2 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

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 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)

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 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