From cc4f48c0ce1d67be3b937eaf4449d6ca0ce57e84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Luiz=20Alvares?= Date: Mon, 16 Jan 2023 12:15:44 -0300 Subject: [PATCH] `storage.push` now accepts an optional value This is useful when the entry it's post-initialized after the push. --- rotor/storage.nelua | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/rotor/storage.nelua b/rotor/storage.nelua index f8d76e9..1e1db6c 100755 --- a/rotor/storage.nelua +++ b/rotor/storage.nelua @@ -63,8 +63,10 @@ local GenIdx = require 'rotor.gen_idx' Otherwise, the entry will be inserted on the found free slot and this function will return `true`, the generational index of the used slot and a reference to the inserted entry. + + You can optionally pass a value to initialize this entry. ]] - function storage:push(value: T): (boolean, GenIdx, *T) + function storage:push(value: facultative(T)): (boolean, GenIdx, *T) local available_found, available_idx = find_next_available(self, self.next_idx) if not available_found then @@ -72,8 +74,13 @@ local GenIdx = require 'rotor.gen_idx' return false end + ## if value.type.is_niltype then + self.entries[available_idx] = {} + ## else + self.entries[available_idx] = value + ## end + self.next_idx = (available_idx + 1) % SIZE - self.entries[available_idx] = value self.unavailables[available_idx] = true self.generations[available_idx] = self.generations[available_idx] + 1 @@ -152,10 +159,7 @@ local GenIdx = require 'rotor.gen_idx' end ## end - self.entries = {} - self.unavailables = {} - self.generations = {} - self.next_idx = 0 + $self = {} end ## return storage