github.com/m4gshm/gollections@v0.0.13-0.20240331203319-a34a86e58a24/collection/mutable/api.go (about)

     1  // Package mutable provides implementations of mutable containers.
     2  package mutable
     3  
     4  import (
     5  	"github.com/m4gshm/gollections/c"
     6  	"github.com/m4gshm/gollections/loop"
     7  	"github.com/m4gshm/gollections/map_"
     8  	"github.com/m4gshm/gollections/slice"
     9  )
    10  
    11  // NewSet instantiates set and copies elements to it
    12  func NewSet[T comparable](elements ...T) *Set[T] {
    13  	uniques := make(map[T]struct{}, len(elements))
    14  	for _, e := range elements {
    15  		uniques[e] = struct{}{}
    16  	}
    17  	return WrapSet(uniques)
    18  }
    19  
    20  // NewSetCap creates a set with a predefined capacity
    21  func NewSetCap[T comparable](capacity int) *Set[T] {
    22  	return WrapSet(make(map[T]struct{}, capacity))
    23  }
    24  
    25  // SetFromLoop creates a set with elements retrieved by the 'next' function.
    26  // The next returns an element with true or zero value with false if there are no more elements.
    27  func SetFromLoop[T comparable](next func() (T, bool)) *Set[T] {
    28  	uniques := map[T]struct{}{}
    29  	for e, ok := next(); ok; e, ok = next() {
    30  		uniques[e] = struct{}{}
    31  	}
    32  	return WrapSet(uniques)
    33  }
    34  
    35  // NewMap instantiates an map using key/value pairs
    36  func NewMap[K comparable, V any](elements ...c.KV[K, V]) *Map[K, V] {
    37  	return WrapMap(map_.Of(elements...))
    38  }
    39  
    40  // NewMapCap instantiates Map with a predefined capacity
    41  func NewMapCap[K comparable, V any](capacity int) *Map[K, V] {
    42  	return WrapMap(make(map[K]V, capacity))
    43  }
    44  
    45  // NewMapOf instantiates Map populated by the 'elements' map key/values
    46  func NewMapOf[K comparable, V any](elements map[K]V) *Map[K, V] {
    47  	uniques := make(map[K]V, len(elements))
    48  	for key, val := range elements {
    49  		uniques[key] = val
    50  	}
    51  	return WrapMap(uniques)
    52  }
    53  
    54  // MapFromLoop creates a map with elements retrieved converter the 'next' function
    55  func MapFromLoop[K comparable, V any](next func() (K, V, bool)) *Map[K, V] {
    56  	uniques := map[K]V{}
    57  	for key, val, ok := next(); ok; key, val, ok = next() {
    58  		uniques[key] = val
    59  	}
    60  	return WrapMap(uniques)
    61  }
    62  
    63  // NewVector instantiates Vector populated by the 'elements' slice
    64  func NewVector[T any](elements ...T) *Vector[T] {
    65  	return WrapVector(slice.Clone(elements))
    66  }
    67  
    68  // NewVectorCap instantiates Vector with a predefined capacity
    69  func NewVectorCap[T any](capacity int) *Vector[T] {
    70  	return WrapVector(make([]T, 0, capacity))
    71  }
    72  
    73  // VectorFromLoop creates a vector with elements retrieved by the 'next' function.
    74  // The next returns an element with true or zero value with false if there are no more elements.
    75  func VectorFromLoop[T any](next func() (T, bool)) *Vector[T] {
    76  	return WrapVector(loop.Slice(next))
    77  }