rockorager / GapBuffer.zig

a general-purpose gap buffer implementation in Zig

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

GapBuffer.zig

A gap buffer is like a std.ArrayList, except that rather than having one contiguous block of items, there are two, with a moveable “gap” between them. Although moving the gap requires a copy, insertions and deletions at either side of the gap become O(1) operations.

This repository implements managed, unmanaged and optionally aligned versions of GapBuffer(T). The API is directly inspired by std.ArrayList. The main differences are “Before” and “After” versions of operations that operate or affect the gap—“Before” operations will add or remove elements before the gap. There are also convenience functions for translating a “logical” index into an offset, an element, or a pointer from the buffer, allowing the user to be largely agnostic about the location of the gap.

Also implemented is a “struct of arrays” transform of GapBuffer(T) where T is a struct or tagged union type, MultiGapBuffer(T).

To add this package to your project, pick a commit hash <hash> and run this:

$ zig fetch --save https://github.com/ryleelyman/GapBuffer.zig/archive/<hash>.tar.gz

Then in your build.zig you can add this:

const gap_buffer = b.dependency("gap_buffer", .{
    .target = target,
    .optimize = optimize,
});
const gb = gap_buffer.module("gap_buffer");

// For whatever you're building; in this case let's assume it's called exe.
exe.root_module.addImport("gap_buffer", gb);

and in your source code:

// import
const gb = @import("gap_buffer");

// and use it something like this...
var buffer = gb.GapBuffer(u8).init(allocator);

About

a general-purpose gap buffer implementation in Zig

License:MIT License


Languages

Language:Zig 100.0%