github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/structure/maps/hashmap/hashmap.go (about)

     1  // Package hashmap implements a map backed by a hash table.
     2  //
     3  // Elements are unordered in the map.
     4  //
     5  // Structure is not thread safe.
     6  //
     7  // Reference: http://en.wikipedia.org/wiki/Associative_array
     8  package hashmap
     9  
    10  import (
    11  	"encoding/json"
    12  	"fmt"
    13  
    14  	banytostring "github.com/songzhibin97/go-baseutils/base/banytostring"
    15  	"github.com/songzhibin97/go-baseutils/structure/maps"
    16  )
    17  
    18  // Assert Map implementation
    19  var _ maps.Map[int, any] = (*Map[int, any])(nil)
    20  
    21  // Map holds the elements in go's native map
    22  type Map[K comparable, V any] struct {
    23  	m map[K]V
    24  }
    25  
    26  // New instantiates a hash map.
    27  func New[K comparable, V any]() *Map[K, V] {
    28  	return &Map[K, V]{m: make(map[K]V)}
    29  }
    30  
    31  // Put inserts element into the map.
    32  func (m *Map[K, V]) Put(key K, value V) {
    33  	m.m[key] = value
    34  }
    35  
    36  // Get searches the element in the map by key and returns its value or nil if key is not found in map.
    37  // Second return parameter is true if key was found, otherwise false.
    38  func (m *Map[K, V]) Get(key K) (value V, found bool) {
    39  	value, found = m.m[key]
    40  	return
    41  }
    42  
    43  // Remove removes the element from the map by key.
    44  func (m *Map[K, V]) Remove(key K) {
    45  	delete(m.m, key)
    46  }
    47  
    48  // Empty returns true if map does not contain any elements
    49  func (m *Map[K, V]) Empty() bool {
    50  	return m.Size() == 0
    51  }
    52  
    53  // Size returns number of elements in the map.
    54  func (m *Map[K, V]) Size() int {
    55  	return len(m.m)
    56  }
    57  
    58  // Keys returns all keys (random order).
    59  func (m *Map[K, V]) Keys() []K {
    60  	keys := make([]K, m.Size())
    61  	count := 0
    62  	for key := range m.m {
    63  		keys[count] = key
    64  		count++
    65  	}
    66  	return keys
    67  }
    68  
    69  // Values returns all values (random order).
    70  func (m *Map[K, V]) Values() []V {
    71  	values := make([]V, m.Size())
    72  	count := 0
    73  	for _, value := range m.m {
    74  		values[count] = value
    75  		count++
    76  	}
    77  	return values
    78  }
    79  
    80  // Clear removes all elements from the map.
    81  func (m *Map[K, V]) Clear() {
    82  	m.m = make(map[K]V)
    83  }
    84  
    85  // String returns a string representation of container
    86  func (m *Map[K, V]) String() string {
    87  	str := "HashMap\n"
    88  	str += fmt.Sprintf("%v", m.m)
    89  	return str
    90  }
    91  
    92  // UnmarshalJSON @implements json.Unmarshaler
    93  func (m *Map[K, V]) UnmarshalJSON(bytes []byte) error {
    94  	elements := make(map[K]V)
    95  	err := json.Unmarshal(bytes, &elements)
    96  	if err == nil {
    97  		m.Clear()
    98  		for key, value := range elements {
    99  			m.m[key] = value
   100  		}
   101  	}
   102  	return err
   103  }
   104  
   105  // MarshalJSON @implements json.Marshaler
   106  func (m *Map[K, V]) MarshalJSON() ([]byte, error) {
   107  	elements := make(map[string]interface{})
   108  	for key, value := range m.m {
   109  		elements[banytostring.ToString(key)] = value
   110  	}
   111  	return json.Marshal(&elements)
   112  }