8000 Integer arrays · Issue #163 · tact-lang/tact · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content 8000
< A557 div class="prc-PageHeader-PageHeader-sT1Hp HeaderViewer-module__headerContainer--p0Eo1 ">
Integer arrays #163
Open
Open
@anton-trunov

Description

@anton-trunov

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);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0