-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_structures.dart
65 lines (50 loc) · 1.1 KB
/
data_structures.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import 'dart:collection';
class Tuple<T, U> {
T x;
U y;
Tuple(this.x, this.y);
@override
String toString() => 'x: $x, y: $y';
@override
bool operator ==(Object other) =>
other is Tuple &&
other.runtimeType == runtimeType &&
other.x == x &&
other.y == y;
@override
int get hashCode => Object.hash(x, y);
}
class Vector2 extends Tuple<int, int> {
factory Vector2.fromNormalisedDir(String direction) => ordinals[direction]!;
Vector2(super.x, super.y);
static final ordinals = {
"U": Vector2(0, 1),
"D": Vector2(0, -1),
"L": Vector2(-1, 0),
"R": Vector2(1, 0),
};
}
class Stack<T> extends ListQueue<T> {
void pushAll(Iterable<T> v) {
addAll(v);
}
List<T> popCount(int count) {
var out = <T>[];
for (var i = 0; i < count; i++) {
if (isNotEmpty) {
out.insert(0, removeLast());
}
}
return out;
}
void push(T v) => add(v);
T pop() => removeLast();
}
class TreeNode<T> {
T value;
TreeNode(this.value);
List<TreeNode<T>> children = [];
void add(TreeNode<T> child) {
children.add(child);
}
}