Module elembuf

Optimized containers alike to arrays

<

Example

Whole library can be used in 5 lines. No new methods to remember while beating the efficiency of arrays and queues.


// Import
import elembuf;

// Instantiate
auto buf = buffer("Hello ");

// Ensure new data fits
assert(buf.max >= "world!".length + buf.length); 

// Fill
buf ~= "world!";

// Read
assert(buf == "Hello world!");

Example

IO - Fast library integration

No outdated push/pop methods. IO libraries that require pointers work out of the box. Just use a lambda.

// Construct
auto buf = buffer([1,2,3]);

auto src = (int[] arr)
{ 
	arr[0] = 4;
	return 1;
};

// Fill
buf ~= src;
assert(buf == [1,2,3,4]);

// Reuse
buf.length = 0;
buf ~= src;
buf ~= 5;
assert(buf == [4,5]);

Example

Concurrency - Built in for your convenience

Simple solution for single consumer-producer synchronization that works efficiently in the background without mutexes or slow synchronization keywords.

alias T = size_t; 

// Producer thread created
auto buf = tbuffer((T[]).init); 

size_t counter;

size_t delegate(T[]) source = (T[] arr)
{ 
		foreach(ref i; arr)
		{
			i = counter;
			counter++;
		}

	return arr.length;
};

// Give instructions to producer
buf ~= source; 


for(int i; i < buf.max * 5; )
{
	while(buf.length == 0)
	{
		// Aquire data
		buf ~= buf.source; 
	}

	i += buf.length;
	buf = buf[$..$];
}

// Unallocate all data &
// destroy the thread.
// Can be used for all buffers.
buf.deinit;

Example

Mirroring - For Compression & Decryption

New item orders can easily be established without copying using a mirror provided by the operating system.

Memory can be visualized as blocks of two O's, each having a size of max/2. The buffer only sees memory marked with X's. The mirror border is marked with |, right side of which is the mirrored memory.

/+ Current view is OO|OO +/
auto buf = buffer("");

// aO|aO
buf.ptr[0..buf.max/2] = 'a';

// ab|ab
buf.ptr[buf.max/2 .. buf.max] = 'b';

/+ Expand view from OO|OO +/

// OX|XO
buf = buf.ptr[ buf.max / 2 .. buf.max + buf.max / 2 ];

// ab|ab
assert(buf[0] == 'b');
assert(buf[$-1] == 'a');

/+ Order: ab -> ba +/

Functions

NameDescription
buffer(arg)

Dynamic circular array.

tbuffer(arg)

Threaded dynamic circular array.