github.com/djordje200179/extendedlibrary/datastructures@v1.7.1-0.20240227175559-d09520a92dd4/maps/hashmap/map.go (about)

     1  package hashmap
     2  
     3  import (
     4  	"github.com/djordje200179/extendedlibrary/datastructures/iter"
     5  	"github.com/djordje200179/extendedlibrary/datastructures/maps"
     6  	"github.com/djordje200179/extendedlibrary/misc"
     7  	"unsafe"
     8  )
     9  
    10  // Map is a hash map with builtin map as a base.
    11  type Map[K comparable, V any] map[K]V
    12  
    13  // New creates an empty Map.
    14  func New[K comparable, V any]() Map[K, V] {
    15  	return NewWithCapacity[K, V](0)
    16  }
    17  
    18  // NewWithCapacity creates an empty Map with the specified capacity.
    19  func NewWithCapacity[K comparable, V any](capacity int) Map[K, V] {
    20  	return FromMap(make(map[K]V, capacity))
    21  }
    22  
    23  // NewFromIterable creates a Map from the specified iter.Iterable.
    24  func NewFromIterable[K comparable, V any](iterable iter.Iterable[misc.Pair[K, V]]) Map[K, V] {
    25  	var m Map[K, V]
    26  
    27  	if finiteIter, ok := any(iterable).(iter.FiniteIterable[misc.Pair[K, V]]); ok {
    28  		m = NewWithCapacity[K, V](finiteIter.Size())
    29  	} else {
    30  		m = New[K, V]()
    31  	}
    32  
    33  	for it := iterable.Iterator(); it.Valid(); it.Move() {
    34  		entry := it.Get()
    35  
    36  		m[entry.First] = entry.Second
    37  	}
    38  
    39  	return m
    40  }
    41  
    42  // FromMap creates a new Map from the specified map.
    43  func FromMap[K comparable, V any](m map[K]V) Map[K, V] {
    44  	return m
    45  }
    46  
    47  // Size returns the number of entries in the map.
    48  func (m Map[K, V]) Size() int {
    49  	return len(m)
    50  }
    51  
    52  // Contains returns true if the map contains the specified key.
    53  func (m Map[K, V]) Contains(key K) bool {
    54  	_, ok := m[key]
    55  	return ok
    56  }
    57  
    58  // TryGet returns the value associated with the specified key,
    59  // or zero value and false if the key is not present.
    60  func (m Map[K, V]) TryGet(key K) (V, bool) {
    61  	value, ok := m[key]
    62  	return value, ok
    63  }
    64  
    65  // Get returns the value associated with the specified key.
    66  // Panics if the key is not present.
    67  func (m Map[K, V]) Get(key K) V {
    68  	value, ok := m[key]
    69  	if !ok {
    70  		panic(maps.MissingKeyError[K]{Key: key})
    71  	}
    72  
    73  	return value
    74  }
    75  
    76  // GetRef returns a reference to the value associated with the specified key.
    77  // Panics if the key is not present.
    78  func (m Map[K, V]) GetRef(key K) *V {
    79  	mt, mv := mapTypeAndValue(m)
    80  	ptr, ok := internalMapGet(mt, mv, unsafe.Pointer(&key))
    81  
    82  	if !ok {
    83  		panic(maps.MissingKeyError[K]{Key: key})
    84  	}
    85  
    86  	return (*V)(ptr)
    87  }
    88  
    89  // Set sets the value associated with the specified key.
    90  func (m Map[K, V]) Set(key K, value V) {
    91  	m[key] = value
    92  }
    93  
    94  // Remove removes the entry with the specified key.
    95  // Does nothing if the key is not present.
    96  func (m Map[K, V]) Remove(key K) {
    97  	delete(m, key)
    98  }
    99  
   100  // Clear removes all entries from the map.
   101  func (m Map[K, V]) Clear() {
   102  	clear(m)
   103  }
   104  
   105  // Clone returns a shallow copy of the map.
   106  func (m Map[K, V]) Clone() maps.Map[K, V] {
   107  	cloned := New[K, V]()
   108  	for k, v := range m {
   109  		cloned[k] = v
   110  	}
   111  
   112  	return cloned
   113  }
   114  
   115  // Iterator returns an iter.Iterator over the map.
   116  func (m Map[K, V]) Iterator() iter.Iterator[misc.Pair[K, V]] {
   117  	return m.MapIterator()
   118  }
   119  
   120  // MapIterator returns an iterator over the map.
   121  func (m Map[K, V]) MapIterator() maps.Iterator[K, V] {
   122  	// TODO: Use builtin function
   123  	keys := make([]K, 0, len(m))
   124  	for k := range m {
   125  		keys = append(keys, k)
   126  	}
   127  
   128  	return &Iterator[K, V]{
   129  		m:     m,
   130  		keys:  keys,
   131  		index: 0,
   132  	}
   133  }
   134  
   135  // Stream2 streams over the entries in the Map.
   136  func (m Map[K, V]) Stream2(yield func(K, V) bool) {
   137  	for k, v := range m {
   138  		if !yield(k, v) {
   139  			break
   140  		}
   141  	}
   142  }
   143  
   144  // RefsStream2 streams over the keys and references to the values in the Map.
   145  func (m Map[K, V]) RefsStream2(yield func(K, *V) bool) {
   146  	for it := m.MapIterator(); it.Valid(); it.Move() {
   147  		if !yield(it.Key(), it.ValueRef()) {
   148  			break
   149  		}
   150  	}
   151  }
   152  
   153  // Map returns the builtin map used as a base.
   154  func (m Map[K, V]) Map() map[K]V {
   155  	return m
   156  }