-
-
Save niner/23eedda15d16c703546e17a20fc7c19c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/lib/Inline/Language/ObjectKeeper.pm6 b/lib/Inline/Language/ObjectKeeper.pm6 | |
index 27fc538..5ea758c 100644 | |
--- a/lib/Inline/Language/ObjectKeeper.pm6 | |
+++ b/lib/Inline/Language/ObjectKeeper.pm6 | |
@@ -1,27 +1,38 @@ | |
class Inline::Language::ObjectKeeper { | |
- has @!objects; | |
- has $!last_free = -1; | |
+ has IterationBuffer $!objects; | |
+ has $!last_free; | |
- method keep(Any:D $value) returns Int { | |
- my $index = $!last_free; | |
- if $index != -1 { | |
- $!last_free = @!objects.AT-POS($index); | |
- @!objects.ASSIGN-POS($index, $value); | |
- $index | |
+ submethod BUILD() { | |
+ $!objects := IterationBuffer.new; | |
+ $!last_free := -1; | |
+ } | |
+ | |
+ method push($value is raw) { | |
+ my $objects := $!objects; | |
+ $objects.push($value); | |
+ $objects.elems - 1 | |
+ } | |
+ | |
+ method keep(Any:D $value is raw) returns Int { | |
+ my $index := $!last_free; | |
+ if $index == -1 { | |
+ self.push($value) | |
} | |
else { | |
- @!objects.push($value); | |
- @!objects.end | |
+ my $objects := $!objects; | |
+ $!last_free := $objects.AT-POS($index); | |
+ $objects.BIND-POS($index, $value); | |
+ $index | |
} | |
} | |
method get(Int $index) returns Any:D { | |
- @!objects[$index]; | |
+ $!objects.AT-POS($index); | |
} | |
method free(Int $index --> Nil) { | |
- @!objects.ASSIGN-POS($index, $!last_free); | |
- $!last_free = $index; | |
+ $!objects.BIND-POS($index, $!last_free); | |
+ $!last_free := $index; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment