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 🧑💻.
To use the array, just include the header file. Thats it.
#include "farray1.hpp"
// 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
.
This project has a Website! containing more information:
- Short Despription about the algorithm
- Advanced Features - iterator, direct-functions, smaller-blocks, templates
- Much More!