| 245 | } |
| 246 | |
| 247 | private touch(item: Item<K, V>, touch: Touch): void { |
| 248 | if (!this._head || !this._tail) { |
| 249 | throw new Error('Invalid list'); |
| 250 | } |
| 251 | if ((touch !== Touch.First && touch !== Touch.Last)) { |
| 252 | return; |
| 253 | } |
| 254 | |
| 255 | if (touch === Touch.First) { |
| 256 | if (item === this._head) { |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | const next = item.next; |
| 261 | const previous = item.previous; |
| 262 | |
| 263 | // Unlink the item |
| 264 | if (item === this._tail) { |
| 265 | // previous must be defined since item was not head but is tail |
| 266 | // So there are more than on item in the map |
| 267 | previous!.next = undefined; |
| 268 | this._tail = previous; |
| 269 | } |
| 270 | else { |
| 271 | // Both next and previous are not undefined since item was neither head nor tail. |
| 272 | next!.previous = previous; |
| 273 | previous!.next = next; |
| 274 | } |
| 275 | |
| 276 | // Insert the node at head |
| 277 | item.previous = undefined; |
| 278 | item.next = this._head; |
| 279 | this._head.previous = item; |
| 280 | this._head = item; |
| 281 | } else if (touch === Touch.Last) { |
| 282 | if (item === this._tail) { |
| 283 | return; |
| 284 | } |
| 285 | |
| 286 | const next = item.next; |
| 287 | const previous = item.previous; |
| 288 | |
| 289 | // Unlink the item. |
| 290 | if (item === this._head) { |
| 291 | // next must be defined since item was not tail but is head |
| 292 | // So there are more than on item in the map |
| 293 | next!.previous = undefined; |
| 294 | this._head = next; |
| 295 | } else { |
| 296 | // Both next and previous are not undefined since item was neither head nor tail. |
| 297 | next!.previous = previous; |
| 298 | previous!.next = next; |
| 299 | } |
| 300 | item.next = undefined; |
| 301 | item.previous = this._tail; |
| 302 | this._tail.next = item; |
| 303 | this._tail = item; |
| 304 | } |