Coder Social home page Coder Social logo

inlined_vector's Introduction

Note

This code has been merged into utils. Please go there for the latest updates.

Inlined Vector

A c++11 vector-like data structure that stores elements internally. It can grow beyond its capacity, in which case it becomes a wrapper around std::vector.

For a production-quality inlined vector see e.g., abseil or similar.

Usage

Drop the header into your project. Provide the following defines before including the header if you wish to get exceptions or error messages.

#define BSP_INLINED_VECTOR_THROWS
#define BSP_INLINED_VECTOR_LOG_ERROR(message) std::cerr << message << "\n"
#include "inlined_vector.h"

The vector is templated on type, initial capacity, and whether it is allowed to expand or not. For example, v1 is a non-expanding vector of 16 integers and v2 is an expanding vector of 8 integers.

bsp::inlined_vector<int, 16, false> v1;
bsp::inlined_vector<int, 8, true> v2;

API is similar to std::vector:

bsp::inlined_vector<int, 16, true> v { 1, 2, 3, 4, 5 };
assert(v.size() == 5);
v.push_back(13);
assert(v.back() == 13);
v.pop_back();

A basic iterator interface is available.

#include <iterator>
...
auto it = v.insert(std::next(v.begin(), 3), 42);
assert(*it == 42);
v.erase(it);

Use expanded() to check if the inlined_vector has grown into a dynamically-allocated vector.

bsp::inlined_vector<int, 16, true> v { 1, 2, 3, 4, 5 };
for (int i=0; i<32; ++i) v.push_back(i);
assert(v.expanded());

Running the tests

Basic Catch2 tests are provided in tests/.

License

This project is licensed under the MIT License - see the LICENSE.md file for details

inlined_vector's People

Contributors

eigenbom avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 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.