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

     1  package corestr
     2  
     3  type NonChainedLinkedListNodes struct {
     4  	items             *[]*LinkedListNode
     5  	isChainingApplied bool
     6  }
     7  
     8  func (receiver *NonChainedLinkedListNodes) IsChainingApplied() bool {
     9  	return receiver.isChainingApplied
    10  }
    11  
    12  func (receiver *NonChainedLinkedListNodes) Items() *[]*LinkedListNode {
    13  	return receiver.items
    14  }
    15  
    16  func NewNonChainedLinkedListNodes(
    17  	capacity int,
    18  ) *NonChainedLinkedListNodes {
    19  	items := make([]*LinkedListNode, 0, capacity)
    20  
    21  	return &NonChainedLinkedListNodes{
    22  		items: &items,
    23  	}
    24  }
    25  
    26  func (receiver *NonChainedLinkedListNodes) Length() int {
    27  	if receiver.items == nil {
    28  		return 0
    29  	}
    30  
    31  	return len(*receiver.items)
    32  }
    33  
    34  func (receiver *NonChainedLinkedListNodes) IsEmpty() bool {
    35  	return receiver.items == nil || len(*receiver.items) == 0
    36  }
    37  
    38  func (receiver *NonChainedLinkedListNodes) Adds(
    39  	nodes ...*LinkedListNode,
    40  ) *NonChainedLinkedListNodes {
    41  	if nodes == nil {
    42  		return receiver
    43  	}
    44  
    45  	for i := range nodes {
    46  		*receiver.items = append(
    47  			*receiver.items,
    48  			nodes[i])
    49  	}
    50  
    51  	return receiver
    52  }
    53  
    54  func (receiver *NonChainedLinkedListNodes) HasItems() bool {
    55  	return !receiver.IsEmpty()
    56  }
    57  
    58  func (receiver *NonChainedLinkedListNodes) First() *LinkedListNode {
    59  	return (*receiver.items)[0]
    60  }
    61  
    62  func (receiver *NonChainedLinkedListNodes) FirstOrDefault() *LinkedListNode {
    63  	if receiver.IsEmpty() {
    64  		return nil
    65  	}
    66  
    67  	return (*receiver.items)[0]
    68  }
    69  
    70  func (receiver *NonChainedLinkedListNodes) Last() *LinkedListNode {
    71  	return (*receiver.items)[receiver.Length()-1]
    72  }
    73  
    74  func (receiver *NonChainedLinkedListNodes) LastOrDefault() *LinkedListNode {
    75  	if receiver.IsEmpty() {
    76  		return nil
    77  	}
    78  
    79  	return (*receiver.items)[receiver.Length()-1]
    80  }
    81  
    82  // ApplyChaining Warning Mutates data inside.
    83  func (receiver *NonChainedLinkedListNodes) ApplyChaining() *NonChainedLinkedListNodes {
    84  	length := receiver.Length()
    85  	if length == 0 {
    86  		return receiver
    87  	}
    88  
    89  	receiver.isChainingApplied = true
    90  	for i, node := range *receiver.items {
    91  		if i+1 >= length {
    92  			break
    93  		}
    94  
    95  		nextNode := (*receiver.items)[i+1]
    96  		node.next = nextNode
    97  	}
    98  
    99  	if receiver.HasItems() {
   100  		receiver.Last().next = nil
   101  	}
   102  
   103  	return receiver
   104  }
   105  
   106  func (receiver *NonChainedLinkedListNodes) ToChainedNodes() *[]*LinkedListNode {
   107  	length := receiver.Length()
   108  	list := make([]*LinkedListNode, length)
   109  
   110  	if length == 0 {
   111  		return &list
   112  	}
   113  
   114  	for i, node := range *receiver.items {
   115  		if i+1 >= length {
   116  			break
   117  		}
   118  
   119  		curNode := node.Clone()
   120  		list = append(list, curNode)
   121  		nextNode := (*receiver.items)[i+1]
   122  		nextNodeClone := nextNode.Clone()
   123  		curNode.next = nextNodeClone
   124  		list = append(list, nextNodeClone)
   125  	}
   126  
   127  	return &list
   128  }