Giter Club home page Giter Club logo

intrusive_list's Introduction

intrusive_list

Header-only, composition-based, allocation-free double-linked list for C++11.

Getting started

Add list_node as a member to whatever struct you want to keep a list of. Then, you can insert references to that struct into a list.

#include <avakar/intrusive/list.h>
using namespace avakar::intrusive;

struct X {
    // A node maintains a membership in at most one list.
    list_node load_order;

    // If your object is kept in multiple lists, you need a node for each list.
    list_node memory_order;
};

int main()
{
    // Lists are ranges of references. You must specify the node to be used
    // by each list.
    list<X, &X::load_order> load_order_list;

    load_order_list.push_back(x);

    // You can check if a node is attached to a list.
    assert(x.load_order.attached());
    assert(!x.memory_order.attached());

    // You can enumerate elements the way you would any other range.
    for (X & elem: load_order_list) {
        // ...
    }

    list<X, &X::load_order> another_load_order_list;

    // Inserting to another list that uses the same node will detach that
    // node from any list it is currently attached in.
    another_load_order_list.push_back(x);
    assert(load_order_list.empty());

    // You can erase an element with `erase`, but if you have the associated
    // node available, you can use `detach` on the node.
    x.load_order.detach();
    assert(another_load_order_list.empty());
}

Nodes are movable and will carry the list membership with them. Node objects that were moved from will end up detached.

Clearing or destroying a list will detach all its nodes.

Thread-safety

There is none.

CMake integration

Copy this repo into yours, add it as a submodule, or FetchContent it. Either way, make sure this repo is added via add_subdirectory or FetchContent_MakeAvailable, then link against avakar::intrusive_list.

intrusive_list's People

Contributors

avakar avatar

Watchers

 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.