Coder Social home page Coder Social logo

cmaughan / gapbuffer Goto Github PK

View Code? Open in Web Editor NEW
11.0 4.0 0.0 16 KB

A gapbuffer implementation in C++ 11, implemented as an STL container

License: Other

CMake 9.53% Batchfile 0.32% C++ 87.31% Shell 2.85%
gapbuffer gap-buffer stl stl-container stl-containers vector buffer

gapbuffer's Introduction

Gap Buffer

Build Status Coverage Status GitHub license

If you arrived here by accident, and want to know what a gap buffer is: https://en.wikipedia.org/wiki/Gap_buffer

This is my attempt at an STL-friendly gap buffer. It helped me understand STL containers a little more. This type of data structure is usually used to manipulate text efficiently, when edits occur in the same locality. Since this implementation is a templated container, you could store anything in it - not just chars. Most of the standard STL operations are included.

From the outside this buffer looks like a std::vector. The difference is that under the covers, a 'Gap' is maintained which makes insertion and removal of elements in the same region inexpensive. Iterators behave as you expect them to, but they are effectively skipping over the gap when you increment/decrement them - thus the container can be used like a contiguous vector, even though it is not.

An internal find_first_of, find_first_not_of is provided for a little extra speed - since it can make use of knowledge of the gap area to split the search into 2 efficient loops.

Unit tests are provided to check on the basic behavior. This project just builds a test executable which runs the tests. Just include gap_buffer.h in your project to use it.

Example

GapBuffer<char> myBuffer(0, 4); // Empty buffer, with 4 character gap
myBuffer.push_back('A');
myBuffer.push_back('B');

assert(myBuffer[1] == B);

// Buffer now contains 'AB', and has a 2 character invisible Gap:
// We can use the string method to visualize the gap
assert(myBuffer.string(true) == "AB|2|");

// Remove the first character
myBuffer.erase(myBuffer.begin(), myBuffer.begin() + 1);

// The gap has moved to the beginning of the buffer, and is now 3 chars
assert(myBuffer.string(true) == "|3|B");

// Indexing the buffer or using iterators 'hides' the gap:
assert(myBuffer[0] == 'B');
assert(*myBuffer.begin() == 'B');

See the unit tests for more examples.

Building

On windows there is a simple config.bat file for Visual Studio 2017. On other systems, you should just be able to use CMake in the usual way. Example of an out-of-source build setup:

mkdir build
cd build
cmake -G "Unix Makefiles" ..
cmake --build .
ctest -V

To do

  • There are a few missing container functions at the bottom of gap_buffer.h. Fill them in and send me a PR ;)
  • Performance testing.
  • EnsureGapPosAndSize could probably be more efficient if the gap is both moving and being resized.
  • More unit tests.

gapbuffer's People

Contributors

cmaughan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.