Skip to content

Instantly share code, notes, and snippets.

@niner
Created November 14, 2021 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niner/23eedda15d16c703546e17a20fc7c19c to your computer and use it in GitHub Desktop.
Save niner/23eedda15d16c703546e17a20fc7c19c to your computer and use it in GitHub Desktop.
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