gitlab.com/evatix-go/core@v1.3.55/coredata/corestr/newHashmapCreator.go (about)

     1  package corestr
     2  
     3  import (
     4  	"gitlab.com/evatix-go/core/constants"
     5  	"gitlab.com/evatix-go/core/converters"
     6  )
     7  
     8  type newHashmapCreator struct{}
     9  
    10  func (it *newHashmapCreator) Empty() *Hashmap {
    11  	return it.Cap(constants.Zero)
    12  }
    13  
    14  func (it *newHashmapCreator) Cap(length int) *Hashmap {
    15  	hashset := make(map[string]string, length)
    16  
    17  	return &Hashmap{
    18  		items:         hashset,
    19  		hasMapUpdated: false,
    20  		length:        length,
    21  		isEmptySet:    true,
    22  	}
    23  }
    24  
    25  func (it *newHashmapCreator) KeyAnyValuesPtr(
    26  	keyAnyValues *[]KeyAnyValuePair,
    27  ) *Hashmap {
    28  	if keyAnyValues == nil || *keyAnyValues == nil {
    29  		return it.Cap(defaultHashsetItems)
    30  	}
    31  
    32  	length := len(*keyAnyValues)
    33  	hashMap := it.Cap(length + constants.ArbitraryCapacity10)
    34  
    35  	return hashMap.AddOrUpdateKeyAnyValsPtr(keyAnyValues)
    36  }
    37  
    38  func (it *newHashmapCreator) KeyValuesPtr(
    39  	keyValues *[]KeyValuePair,
    40  ) *Hashmap {
    41  	if keyValues == nil || *keyValues == nil {
    42  		return it.Cap(defaultHashsetItems)
    43  	}
    44  
    45  	length := len(*keyValues)
    46  	hashMap := it.Cap(length + constants.ArbitraryCapacity10)
    47  
    48  	return hashMap.AddOrUpdateKeyValsPtr(keyValues)
    49  }
    50  
    51  func (it *newHashmapCreator) KeyValuesCollection(
    52  	keys, values *Collection,
    53  ) *Hashmap {
    54  	if keys == nil || keys.IsEmpty() {
    55  		return it.Empty()
    56  	}
    57  
    58  	itemsMap := converters.KeysValuesStringsToMapPtr(
    59  		keys.ListPtr(),
    60  		values.ListPtr())
    61  
    62  	return it.UsingMap(
    63  		*itemsMap)
    64  }
    65  
    66  func (it *newHashmapCreator) KeyValuesStrings(
    67  	keys, values []string,
    68  ) *Hashmap {
    69  	if len(keys) == 0 {
    70  		return it.Empty()
    71  	}
    72  
    73  	itemsMap := converters.KeysValuesStringsToMapPtr(
    74  		&keys,
    75  		&values)
    76  
    77  	return it.UsingMap(
    78  		*itemsMap)
    79  }
    80  
    81  func (it *newHashmapCreator) UsingMap(
    82  	itemsMap map[string]string,
    83  ) *Hashmap {
    84  	length := len(itemsMap)
    85  
    86  	return &Hashmap{
    87  		items:      itemsMap,
    88  		length:     length,
    89  		isEmptySet: length == constants.Zero,
    90  	}
    91  }
    92  
    93  // UsingMapOptions
    94  // isMakeClone : copies itemsMap or else use the same one as pointer assign.
    95  func (it *newHashmapCreator) UsingMapOptions(
    96  	isMakeClone bool,
    97  	addCapacity int,
    98  	itemsMap map[string]string,
    99  ) *Hashmap {
   100  	if len(itemsMap) == 0 {
   101  		return it.Cap(addCapacity)
   102  	}
   103  
   104  	length := len(itemsMap)
   105  
   106  	if isMakeClone {
   107  		hashMap := it.Cap(length + addCapacity)
   108  
   109  		return hashMap.AddOrUpdateMap(itemsMap)
   110  	}
   111  
   112  	// no clone
   113  	return &Hashmap{
   114  		items:      itemsMap,
   115  		length:     length,
   116  		isEmptySet: length == constants.Zero,
   117  	}
   118  }
   119  
   120  // MapWithCap always returns the clone of the items.
   121  func (it *newHashmapCreator) MapWithCap(
   122  	addCapacity int,
   123  	itemsMap map[string]string,
   124  ) *Hashmap {
   125  	if len(itemsMap) == 0 {
   126  		return it.Cap(addCapacity)
   127  	}
   128  
   129  	if addCapacity == 0 {
   130  		return it.UsingMap(itemsMap)
   131  	}
   132  
   133  	length := len(itemsMap)
   134  	hashMap := it.Cap(length + addCapacity)
   135  
   136  	return hashMap.AddOrUpdateMap(itemsMap)
   137  }