[675716e] | 1 | //
|
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2019 University of Waterloo
|
---|
| 3 | //
|
---|
| 4 | // The contents of this file are covered under the licence agreement in the
|
---|
| 5 | // file "LICENCE" distributed with Cforall.
|
---|
| 6 | //
|
---|
| 7 | // Heap.h --
|
---|
| 8 | //
|
---|
| 9 | // Author : Thierry Delisle
|
---|
| 10 | // Created On : Fri Mar 03 14:53:53 2019
|
---|
| 11 | // Last Modified By :
|
---|
| 12 | // Last Modified On :
|
---|
| 13 | // Update Count :
|
---|
| 14 | //
|
---|
| 15 |
|
---|
[8e70823] | 16 | #pragma once
|
---|
| 17 |
|
---|
| 18 | namespace Stats {
|
---|
| 19 | namespace Base {
|
---|
[1cb7fab2] | 20 | class TreeImpl;
|
---|
| 21 |
|
---|
| 22 | struct TreeTop {
|
---|
| 23 | TreeImpl * head = nullptr;
|
---|
| 24 | TreeImpl * tail = nullptr;
|
---|
| 25 |
|
---|
| 26 | inline void append(TreeImpl * node);
|
---|
| 27 | };
|
---|
[8e70823] | 28 |
|
---|
[1cb7fab2] | 29 | template<typename func_t>
|
---|
| 30 | void ForAll(TreeTop & range, size_t level, func_t func, bool destroy = false);
|
---|
[8e70823] | 31 |
|
---|
[1cb7fab2] | 32 | class TreeImpl {
|
---|
| 33 | public:
|
---|
[8e70823] | 34 | virtual void print(std::ostream &) = 0;
|
---|
| 35 |
|
---|
[1cb7fab2] | 36 | const char * const name;
|
---|
| 37 | TreeImpl(const char * const name) : name(name) {}
|
---|
[8e70823] | 38 |
|
---|
| 39 | protected:
|
---|
| 40 | virtual ~TreeImpl() = default;
|
---|
| 41 |
|
---|
| 42 | TreeImpl * next = nullptr;
|
---|
[1cb7fab2] | 43 | TreeTop children;
|
---|
| 44 |
|
---|
| 45 | friend struct TreeTop;
|
---|
[8e70823] | 46 |
|
---|
| 47 | template<typename func_t>
|
---|
[1cb7fab2] | 48 | friend void ForAll(TreeTop & range, size_t level, func_t func, bool destroy);
|
---|
[8e70823] | 49 | };
|
---|
| 50 |
|
---|
[1cb7fab2] | 51 | void TreeTop::append(TreeImpl * node) {
|
---|
| 52 | if(!head) { head = node; }
|
---|
| 53 | else { tail->next = node;}
|
---|
| 54 | tail = node;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
[8e70823] | 57 | template<typename func_t>
|
---|
[1cb7fab2] | 58 | inline void ForAll(TreeTop & range, size_t level, func_t func, bool destroy) {
|
---|
[8e70823] | 59 | auto it = range.head;
|
---|
| 60 | while(it) {
|
---|
| 61 | auto next = it->next;
|
---|
| 62 | func(it, level);
|
---|
| 63 | ForAll(it->children, level + 1, func);
|
---|
| 64 | if(destroy) delete it;
|
---|
| 65 | it = next;
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[1cb7fab2] | 69 | template<TreeTop & top>
|
---|
[8e70823] | 70 | class Tree : public TreeImpl {
|
---|
| 71 | public:
|
---|
[1cb7fab2] | 72 | Tree(const char * const name) : TreeImpl{name} {
|
---|
[8e70823] | 73 | top.append(this);
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[1cb7fab2] | 76 | Tree(const char * const name, Tree * parent) : TreeImpl{name} {
|
---|
[8e70823] | 77 | parent->children.append(this);
|
---|
| 78 | }
|
---|
| 79 | protected:
|
---|
| 80 | virtual ~Tree() = default;
|
---|
| 81 | };
|
---|
| 82 | }
|
---|
| 83 | }
|
---|