gitlab.com/evatix-go/core@v1.3.55/coredata/corestr/LinkedListNode.go (about) 1 package corestr 2 3 import ( 4 "fmt" 5 "strings" 6 7 "gitlab.com/evatix-go/core/constants" 8 ) 9 10 type LinkedListNode struct { 11 Element string 12 next *LinkedListNode 13 } 14 15 func (linkedListNode *LinkedListNode) HasNext() bool { 16 return linkedListNode.next != nil 17 } 18 19 func (linkedListNode *LinkedListNode) Next() *LinkedListNode { 20 return linkedListNode.next 21 } 22 23 func (linkedListNode *LinkedListNode) EndOfChain() ( 24 endOfChain *LinkedListNode, 25 length int, 26 ) { 27 node := linkedListNode 28 length++ 29 30 for node.HasNext() { 31 node = node.Next() 32 length++ 33 } 34 35 return node, length 36 } 37 38 func (linkedListNode *LinkedListNode) LoopEndOfChain( 39 processor LinkedListSimpleProcessor, 40 ) (endOfLoop *LinkedListNode, length int) { 41 node := linkedListNode 42 arg := &LinkedListProcessorParameter{ 43 Index: 0, 44 CurrentNode: node, 45 PrevNode: nil, 46 IsFirstIndex: true, 47 IsEndingIndex: false, 48 } 49 50 isBreak := processor(arg) 51 length++ 52 53 if isBreak { 54 return node, length 55 } 56 57 i := 1 58 for node.HasNext() { 59 prev := node 60 node = node.Next() 61 isEndingIndex := !node.HasNext() 62 63 arg2 := &LinkedListProcessorParameter{ 64 Index: i, 65 CurrentNode: node, 66 PrevNode: prev, 67 IsFirstIndex: false, 68 IsEndingIndex: isEndingIndex, 69 } 70 71 isBreak = processor(arg2) 72 length++ 73 i++ 74 75 if isBreak { 76 return node, length 77 } 78 } 79 80 return node, length 81 } 82 83 func (linkedListNode *LinkedListNode) Clone() *LinkedListNode { 84 return &LinkedListNode{ 85 Element: linkedListNode.Element, 86 next: nil, 87 } 88 } 89 90 func (linkedListNode *LinkedListNode) AddNext( 91 linkedListForIncrement *LinkedList, 92 item string, 93 ) *LinkedListNode { 94 newNode := &LinkedListNode{ 95 Element: item, 96 next: linkedListNode.Next(), 97 } 98 99 linkedListNode.next = newNode 100 linkedListForIncrement.incrementLength() 101 102 return newNode 103 } 104 105 func (linkedListNode *LinkedListNode) AddStringsPtrToNode( 106 linkedListForIncrement *LinkedList, 107 isSkipOnNull bool, 108 items *[]string, 109 ) *LinkedList { 110 return linkedListForIncrement.AddStringsPtrToNode( 111 isSkipOnNull, 112 linkedListNode, 113 items) 114 } 115 116 func (linkedListNode *LinkedListNode) AddCollectionToNode( 117 linkedListForIncrement *LinkedList, 118 isSkipOnNull bool, 119 collection *Collection, 120 ) *LinkedList { 121 return linkedListForIncrement.AddStringsPtrToNode( 122 isSkipOnNull, 123 linkedListNode, 124 collection.ListPtr()) 125 } 126 127 func (linkedListNode *LinkedListNode) AddNextNode( 128 linkedListForIncrement *LinkedList, 129 nextNode *LinkedListNode, 130 ) *LinkedListNode { 131 nextNode.next = linkedListNode.Next() 132 linkedListNode.next = nextNode 133 linkedListForIncrement.incrementLength() 134 135 return nextNode 136 } 137 138 func (linkedListNode *LinkedListNode) IsEqual(another *LinkedListNode) bool { 139 if linkedListNode == nil && nil == another { 140 return true 141 } 142 143 if linkedListNode == nil || nil == another { 144 return false 145 } 146 147 if linkedListNode == another { 148 return true 149 } 150 151 //goland:noinspection GoNilness 152 if linkedListNode.Element == another.Element { 153 return linkedListNode.isNextEqual( 154 another, 155 true) 156 } 157 158 return false 159 } 160 161 func (linkedListNode *LinkedListNode) IsChainEqual( 162 another *LinkedListNode, 163 isCaseSensitive bool, 164 ) bool { 165 if linkedListNode == nil && nil == another { 166 return true 167 } 168 169 if linkedListNode == nil || nil == another { 170 return false 171 } 172 173 if linkedListNode == another { 174 return true 175 } 176 177 elem1 := linkedListNode.Element 178 elem2 := another.Element 179 180 //goland:noinspection GoNilness 181 isElementSame := (isCaseSensitive && elem1 == elem2) || 182 (!isCaseSensitive && strings.EqualFold(elem1, elem2)) 183 184 return isElementSame && 185 linkedListNode.isNextChainEqual( 186 another, isCaseSensitive) 187 } 188 189 func (linkedListNode *LinkedListNode) IsEqualSensitive( 190 another *LinkedListNode, 191 isCaseSensitive bool, 192 ) bool { 193 if linkedListNode == another { 194 return true 195 } 196 197 if another == nil && linkedListNode == nil { 198 return true 199 } 200 201 if another == nil || linkedListNode == nil { 202 return false 203 } 204 205 isSame := linkedListNode.IsEqualValueSensitive(another.Element, isCaseSensitive) 206 207 return isSame && 208 linkedListNode.isNextEqual(another, isCaseSensitive) 209 } 210 211 func (linkedListNode *LinkedListNode) isNextEqual( 212 another *LinkedListNode, 213 isCaseSensitive bool, 214 ) bool { 215 next1 := linkedListNode.Next() 216 next2 := another.Next() 217 218 if next1 == nil && nil == next2 { 219 return true 220 } 221 222 if next1 == nil || nil == next2 { 223 return false 224 } 225 226 if isCaseSensitive { 227 return next1.Element == next2.Element 228 } 229 230 return strings.EqualFold(next1.Element, next2.Element) 231 } 232 233 func (linkedListNode *LinkedListNode) isNextChainEqual( 234 another *LinkedListNode, 235 isCaseSensitive bool, 236 ) bool { 237 next1 := linkedListNode.Next() 238 next2 := another.Next() 239 240 if next1 == nil && nil == next2 { 241 return true 242 } 243 244 if next1 == nil || nil == next2 { 245 return false 246 } 247 248 return next1. 249 IsChainEqual(next2, isCaseSensitive) 250 } 251 252 func (linkedListNode *LinkedListNode) CreateLinkedList() *LinkedList { 253 return Empty.LinkedList(). 254 AppendChainOfNodes(linkedListNode) 255 } 256 257 func (linkedListNode *LinkedListNode) IsEqualValue(value string) bool { 258 return linkedListNode.Element == value 259 } 260 261 func (linkedListNode *LinkedListNode) IsEqualValueSensitive(value string, isCaseSensitive bool) bool { 262 if isCaseSensitive { 263 return value == linkedListNode.Element 264 } 265 266 return strings.EqualFold(linkedListNode.Element, value) 267 } 268 269 func (linkedListNode *LinkedListNode) String() string { 270 return linkedListNode.Element 271 } 272 273 func (linkedListNode *LinkedListNode) ListPtr() *[]string { 274 list := make([]string, 0, constants.ArbitraryCapacity100) 275 276 node := linkedListNode 277 list = append(list, node.Element) 278 279 for node.HasNext() { 280 node = node.Next() 281 282 list = append(list, node.Element) 283 } 284 285 return &list 286 } 287 288 func (linkedListNode *LinkedListNode) Join(separator string) *string { 289 list := linkedListNode.ListPtr() 290 toString := strings.Join(*list, separator) 291 292 return &toString 293 } 294 295 func (linkedListNode *LinkedListNode) StringListPtr(header string) *string { 296 finalString := header + 297 *linkedListNode.Join(commonJoiner) 298 299 return &finalString 300 } 301 302 func (linkedListNode *LinkedListNode) Print(header string) { 303 finalString := linkedListNode.StringListPtr(header) 304 fmt.Println(finalString) 305 }