gitlab.com/evatix-go/core@v1.3.55/coredata/corestr/newHashsetCreator.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 newHashsetCreator struct{}
     9  
    10  func (it *newHashsetCreator) Empty() *Hashset {
    11  	return it.Cap(constants.Zero)
    12  }
    13  
    14  func (it *newHashsetCreator) Cap(length int) *Hashset {
    15  	hashset := make(map[string]bool, length)
    16  
    17  	return &Hashset{
    18  		items:         hashset,
    19  		hasMapUpdated: false,
    20  		length:        length,
    21  		isEmptySet:    true,
    22  	}
    23  }
    24  
    25  // StringsOption addCapacity will not work if it is not a clone.
    26  //goland:noinspection ALL
    27  func (it *newHashsetCreator) StringsOption(
    28  	addCapacity int,
    29  	isMakeClone bool,
    30  	items ...string,
    31  ) *Hashset {
    32  	if items == nil && addCapacity == 0 {
    33  		return it.Empty()
    34  	}
    35  
    36  	if items == nil && addCapacity > 0 {
    37  		return it.Cap(addCapacity)
    38  	}
    39  
    40  	return it.Strings(
    41  		items)
    42  }
    43  
    44  func (it *newHashsetCreator) PointerStrings(
    45  	inputArray []*string,
    46  ) *Hashset {
    47  	if len(inputArray) == 0 {
    48  		return it.Empty()
    49  	}
    50  
    51  	maps := converters.StringsPointersToStringBoolMap(&inputArray)
    52  
    53  	return it.UsingMap(
    54  		*maps,
    55  	)
    56  }
    57  
    58  // PointerStringsPtrOption addCapacity will not work if it is not a clone.
    59  func (it *newHashsetCreator) PointerStringsPtrOption(
    60  	addCapacity int,
    61  	isMakeClone bool,
    62  	inputArray *[]*string,
    63  ) *Hashset {
    64  	if inputArray == nil || *inputArray == nil {
    65  		return it.Cap(addCapacity)
    66  	}
    67  
    68  	maps := converters.StringsPointersToStringBoolMap(inputArray)
    69  
    70  	return it.UsingMapOption(
    71  		addCapacity,
    72  		isMakeClone,
    73  		*maps,
    74  	)
    75  }
    76  
    77  // UsingCollection addCapacity will not work if it is not a clone.
    78  func (it *newHashsetCreator) UsingCollection(
    79  	collection *Collection,
    80  ) *Hashset {
    81  	if collection == nil || collection.IsEmpty() {
    82  		return it.Empty()
    83  	}
    84  
    85  	return it.Strings(
    86  		collection.items)
    87  }
    88  
    89  func (it *newHashsetCreator) Strings(
    90  	inputArray []string,
    91  ) *Hashset {
    92  	if len(inputArray) == 0 {
    93  		return it.Empty()
    94  	}
    95  
    96  	maps := converters.StringsTo.Hashset(inputArray)
    97  
    98  	return it.UsingMap(
    99  		maps)
   100  }
   101  
   102  func (it *newHashsetCreator) SimpleSlice(
   103  	simpleSlice *SimpleSlice,
   104  ) *Hashset {
   105  	if simpleSlice.IsEmpty() {
   106  		return it.Empty()
   107  	}
   108  
   109  	maps := converters.StringsTo.Hashset(simpleSlice.Items)
   110  
   111  	return it.UsingMap(
   112  		maps)
   113  }
   114  
   115  func (it *newHashsetCreator) StringsSpreadItems(
   116  	inputArray ...string,
   117  ) *Hashset {
   118  	if len(inputArray) == 0 {
   119  		return it.Empty()
   120  	}
   121  
   122  	maps := converters.StringsTo.Hashset(inputArray)
   123  
   124  	return it.UsingMapOption(
   125  		constants.Zero,
   126  		false,
   127  		maps)
   128  }
   129  
   130  // StringsPtr addCapacity will not work if it is not a clone.
   131  func (it *newHashsetCreator) StringsPtr(
   132  	inputArray *[]string,
   133  ) *Hashset {
   134  	if inputArray == nil || *inputArray == nil {
   135  		return it.Empty()
   136  	}
   137  
   138  	maps := converters.StringsTo.Hashset(*inputArray)
   139  
   140  	return it.UsingMapOption(
   141  		constants.Zero,
   142  		false,
   143  		maps,
   144  	)
   145  }
   146  
   147  // UsingMapOption addCapacity will not work if it is not a clone.
   148  func (it *newHashsetCreator) UsingMapOption(
   149  	addCapacity int,
   150  	isMakeClone bool,
   151  	itemsMap map[string]bool,
   152  ) *Hashset {
   153  	if len(itemsMap) == 0 {
   154  		return it.Cap(addCapacity)
   155  	}
   156  
   157  	length := len(itemsMap)
   158  
   159  	if isMakeClone {
   160  		hashset := it.Cap(length + addCapacity)
   161  
   162  		return hashset.AddItemsMap(itemsMap)
   163  	}
   164  
   165  	return &Hashset{
   166  		items:      itemsMap,
   167  		length:     length,
   168  		isEmptySet: length == constants.Zero,
   169  	}
   170  }
   171  
   172  func (it *newHashsetCreator) UsingMap(
   173  	itemsMap map[string]bool,
   174  ) *Hashset {
   175  	if len(itemsMap) == 0 {
   176  		return it.Cap(0)
   177  	}
   178  
   179  	length := len(itemsMap)
   180  	hashset := it.Cap(length)
   181  
   182  	return hashset.AddItemsMap(itemsMap)
   183  }