From cb78047876df64da0e4d72b5e37f9ea91189c674 Mon Sep 17 00:00:00 2001 From: Tim Kurvers Date: Tue, 9 Apr 2024 23:40:10 +0200 Subject: [PATCH] feat: add LinkedList find / toString convenience methods --- src/utils/datastructures/LinkedList.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/utils/datastructures/LinkedList.ts b/src/utils/datastructures/LinkedList.ts index 46a2fb3..37c193e 100644 --- a/src/utils/datastructures/LinkedList.ts +++ b/src/utils/datastructures/LinkedList.ts @@ -48,6 +48,15 @@ class LinkedList { this.linkToTail(entity); } + find(predicate: (entity: T) => boolean): T | null { + for (const entity of this) { + if (predicate(entity)) { + return entity; + } + } + return null; + } + isLinked(entity: T) { return this.linkFor(entity).isLinked; } @@ -98,6 +107,14 @@ class LinkedList { } } + toString(callback: (entity: T) => string) { + const result = []; + for (const entity of this) { + result.push(callback(entity)); + } + return `[${result.join(', ')}]`; + } + unlink(entity: T) { return this.linkFor(entity).unlink(); }