CrackerCat / oak-1

:deciduous_tree: A simple and lightweight tree container (C++03)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

oak

  • Oak is a lightweight tree container. Written in C++03.
  • Oak is simple. Loosely modeled after std::map<k,v> interface with a few extras.
  • Oak is tiny. Header-only.
  • Oak is cross-platform.
  • Oak is stand-alone. No dependencies.
  • Oak is zlib/libpng licensed.

API

  • Oak is loosely modeled after std::map<k,v> interface with a few extras.
  • Use [k] to read/create subtrees on demand (standard std::map behavior).
  • Use (k) to read subtrees. Does not create subtrees on demand.
  • Use .empty(k) to check subtree existence.
  • Use .get(v)/.set(v) to get/set leaf values.
  • Use .setdown()/.setup() to set children/parent values.

Sample

#include <iostream>
#include <string>
#include "oak.hpp"

int main() {
    oak::tree<std::string, int> t;

    // read-write access [] operator (creates trees on demand)
    t["fruits"]["oranges"];
    t["fruits"]["pineapples"] = 6;
    t["fruits"]["lemons"] = 12;
    t["animals"]["pigs"]["pink"] = 0;
    t["animals"]["pigs"]["guinea"] = 0;

    assert( 2 == t.size() );
    assert( 3 == t["fruits"].size() );

    // read-only access () operator (does not create tree on demand)
    t("fruits")("operator () should never create on demand");
    assert( 3 == t("fruits").size() );

    // print some subtrees
    for( auto &k : t("animals") ) {
        std::cout << ".key=" << k.first << "; .tree {\n" << k.second << "}" << std::endl;
    }

    // more printing
    std::cout << t << std::endl;
    std::cout << t.as_csv() << std::endl;

    std::cout << "All ok." << std::endl;
}

Possible output

.key=pigs; .tree {
[2] guinea (0)
[2] pink (0)
}
[2] animals (0)
        [1] pigs (0)
                [2] guinea (0)
                [2] pink (0)
[2] fruits (0)
        [3] lemons (12)
        [3] oranges (0)
        [3] pineapples (6)

/animals,0
/animals/pigs,0
/animals/pigs/guinea,0
/animals/pigs/pink,0
/fruits,0
/fruits/lemons,12
/fruits/oranges,0
/fruits/pineapples,6

All ok.

@todoc

  • rest of API, including walk()

Changelog

  • v1.0.0 (2015/10/25): Semantic versioning adherence
  • v1.0.0 (2015/10/25): Fix csv() method when using non-string keys in map

About

:deciduous_tree: A simple and lightweight tree container (C++03)

License:zlib License


Languages

Language:C++ 100.0%