Skip to content

Commit

Permalink
feat: Update hash set method
Browse files Browse the repository at this point in the history
  • Loading branch information
FallenDeity committed May 1, 2023
1 parent 1eb7168 commit 386c222
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 60 deletions.
7 changes: 6 additions & 1 deletion src/internals/hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ def __call__(self, interpreter: "Interpreter", arguments: list[t.Any], /) -> t.A
elif str(arguments[0]) in self.parent.fields:
self.parent.fields[str(arguments[0])] = arguments[1]
return arguments[1]
raise PyLoxAttributeError(f"Undefined property '{arguments[0]}'.")
else:
try:
self.parent.fields[arguments[0]] = arguments[1]
return arguments[1]
except TypeError:
raise PyLoxAttributeError(f"Undefined property '{arguments[0]}'.")


class LoxHash(LoxContainer):
Expand Down
74 changes: 15 additions & 59 deletions test.lox
Original file line number Diff line number Diff line change
@@ -1,62 +1,18 @@
class Node {
init (value) {
this.value = value;
this.next = nil;
}
}


class List {
init() {
this.head = nil;
this.tail = nil;
}

add(value) {
var node = Node(value);
if (!this.head) {
this.head = node;
this.tail = node;
} else {
this.tail.next = node;
this.tail = node;
}
}
var cache = hash();

remove(value) {
var node = this.head;
var prev = nil;
while (node) {
if (node.value == value) {
if (!prev) {
this.head = node.next;
} else {
prev.next = node.next;
}
return;
}
prev = node;
node = node.next;
}
}

write() {
var node = this.head;
while (node) {
print node.value;
node = node.next;
}
}
fun fib(n) {
if (n < 2) {
return n;
}
if (cache.get(n)) {
return cache.get(n);
}
var res = fib(n - 1) + fib(n - 2);
cache.set(n, res);
return res;
}


var list = List();
for (var i = 0; i < 10; i = i + 1) {
list.add(i);
}
for (var i = 0; i < 10; i = i + 1) {
if (i % 2 == 0) {
list.remove(i);
}
}
list.write();
var before = clock();
print fib(30);
var after = clock();
print after - before;

0 comments on commit 386c222

Please sign in to comment.