Open
Description
Users sometime simulate arrays using maps, so our implementation should be more gas efficient than just maps.
One of the use cases is small byte arrays, for instance, a 32-byte array (which is 256 bits): this fits into one 257-bit integer but the user has to do a bunch of bit arithmetic to operate on it. For those small arrays with statically known length we could provide a Tact primitive simplifying this use case by essentially having a bunch of storage vars of the integer type.
Here is an implementation in Tact of those small byte arrays (32-byte array in this case, emulated with a single 257-bit Integer):
import "@stdlib/deploy";
extends mutates fun set(self: Int, i: Int, v: Int) {
self &= ~ (0xFF << i * 8);
self |= (v << i * 8)
}
extends mutates fun clear(self: Int) { self = 0 }
extends fun get(self: Int, i: Int): Int { return (self >> i * 8) & 0xFF }
contract Test with Deployable {
array: Int = 0;
init() {
self.array.set(3, 0xFF);
self.array.set(3, 42);
self.array.set(0, 54);
self.array.set(31, 1);
}
}