dumblob / farray

Initialize / Fill C++ array fast - O(1) time with only 1 extra bit of memory.

Repository from Github https://github.comdumblob/farrayRepository from Github https://github.comdumblob/farray

Farray - Fillable Array

C++ Header-only Implementation of the In-Place Initializable Arrays paper.

Templated array with constant-time fill(v), read(i), write(i,v) operations, all with 1 bit of extra memory.

The paper is based on the simpler Initializing an array in constant time - which uses 2n extra memory words.

I wrote a Medium article about array initialization and this project. Read it and come back 🧑‍💻.

Basic Use:

To use the array, just include the header file. Thats it.

#include "farray1.hpp"

Using the Farray1 class:

// initialization (all to 1s)
Farray1<int> A(n, 1);   // Farray1 allocated an array be itself. 
                        // It can also take an already allocated array.

// read & write
A[3] = 5;
int x = A[12] + A[19] + A[3];   // reading (1+1+5)

cout << "This must be seven: " << x << endl;

// simple initialization - all values are now 2020
A = 2020;     

for (int i = 5; i <= 10; i++)
    A[i] = i*i;
    
for (int i = 3; i <= 12; i++)
    cout << A[i] << " ";

The output will be:

This must be seven: 7
2020 2020 25 36 49 64 81 100 2020 2020 

You can also use the A.fill(v), A.read(i), A.write(i,v) syntax,
instread of A=v, A[i], A[i]=v.

Farray Website!

This project has a Website! containing more information:

About

Initialize / Fill C++ array fast - O(1) time with only 1 extra bit of memory.

License:BSD 2-Clause "Simplified" License


Languages

Language:C++ 99.3%Language:CMake 0.7%