Coder Social home page Coder Social logo

cpplibext's Introduction

Introduction

This is a small library of C++11 extensions.

Current features:

  • multi_array (multi dimensional array, similar to std::array)
  • bit_mask (bit mask/ flags/ options wrapper class)
  • range_iterator (iterator which knowns its range)
  • packed_vector ()
  • flexible_stack (stack with flexible element sizes, similar to std::stack; )

Examples

/* --- multi_array --- */

// Classic C array:
int classicArray[4][10][2];
classicArray[2][5][0] = 4;

// boost::multi_array
boost::multi_array<int, 3> boostArray(boost::extents[4][10][2]);
boostArray[2][5][0] = 4;

// multi_array:
ext::multi_array<int, 4, 10, 2> multiArray;
multiArray[2] = 3; // Set a value for an entire 'slice' (i.e. multiArray[2][x][y] = 3 for all 0 <= x < 10 and 0 <= y < 2)
multiArray[2][5][0] = 4; // No overhead here in memory and speed compared to 'classicArray'


/* --- packed_vector --- */

/*
"std::vector" allows only a single type for all its elements.
"packed_vector" has a base type and allows inserting elements of any derived class.
All elements are still stored inside a single coherent memory block instead of distributed memory chunks.
This can be a good performance boost when storing lots of small different classes inside a single container.
*/

struct A
{
	virtual ~A()
	{
	}
	int x = 1;
};
struct B : public A
{
	int y = 2;
};
struct C
{
	int z = 0;
};

// This vector packs everything into a byte-aligned std::vector for optimal cache use.
// It allows different element types (or classes) if they have the same base type (here 'A').
ext::packed_vector<A> list;

list.push_back(A());
list.push_back(B());
list.push_back(B());

auto a = list.get<A>(0);
auto b = list.get<B>(1);
//auto c = list.get<C>(2); // 2 errors: 3rd element is of type 'B' and 'C' is not a derived type of 'A'.


/* --- flexible_stack --- */

enum TypeIds
{
	ID_CHAR,
	ID_INT,
	ID_FLOAT,
	ID_DOUBLE
};

flexible_stack<TypeIds> stack;

stack.push('x', ID_CHAR);
stack.push(1, ID_INT);
stack.push(2.3f, ID_FLOAT);
stack.push(4.5, ID_DOUBLE);

while (!stack.empty())
{
	switch (stack.top_id())
	{
		case ID_CHAR:
			char c = stack.top<char>();
			break;
		case ID_INT:
			int i = stack.top<int>();
			break;
		case ID_FLOAT:
			float f = stack.top<float>();
			break;
		case ID_DOUBLE:
			double d = stack.top<double>();
			break;
	}
	stack.pop();
}

cpplibext's People

Contributors

lukasbanana avatar

Watchers

James Cloos avatar chenjie.ma avatar

Forkers

beyondcy

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.