[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 | |
---|
[4f97937] | 18 | #include <cstdint> |
---|
| 19 | #include <iostream> |
---|
| 20 | |
---|
[8e70823] | 21 | namespace Stats { |
---|
| 22 | namespace Base { |
---|
[1cb7fab2] | 23 | class TreeImpl; |
---|
| 24 | |
---|
| 25 | struct TreeTop { |
---|
| 26 | TreeImpl * head = nullptr; |
---|
| 27 | TreeImpl * tail = nullptr; |
---|
| 28 | |
---|
| 29 | inline void append(TreeImpl * node); |
---|
| 30 | }; |
---|
[8e70823] | 31 | |
---|
[1cb7fab2] | 32 | template<typename func_t> |
---|
[4f97937] | 33 | void ForAll(TreeTop & range, std::size_t level, func_t func, bool destroy = false); |
---|
[8e70823] | 34 | |
---|
[1cb7fab2] | 35 | class TreeImpl { |
---|
| 36 | public: |
---|
[8e70823] | 37 | virtual void print(std::ostream &) = 0; |
---|
| 38 | |
---|
[1cb7fab2] | 39 | const char * const name; |
---|
| 40 | TreeImpl(const char * const name) : name(name) {} |
---|
[8e70823] | 41 | |
---|
| 42 | protected: |
---|
| 43 | virtual ~TreeImpl() = default; |
---|
| 44 | |
---|
| 45 | TreeImpl * next = nullptr; |
---|
[1cb7fab2] | 46 | TreeTop children; |
---|
| 47 | |
---|
| 48 | friend struct TreeTop; |
---|
[8e70823] | 49 | |
---|
| 50 | template<typename func_t> |
---|
[4f97937] | 51 | friend void ForAll(TreeTop & range, std::size_t level, func_t func, bool destroy); |
---|
[8e70823] | 52 | }; |
---|
| 53 | |
---|
[1cb7fab2] | 54 | void TreeTop::append(TreeImpl * node) { |
---|
| 55 | if(!head) { head = node; } |
---|
| 56 | else { tail->next = node;} |
---|
| 57 | tail = node; |
---|
| 58 | } |
---|
| 59 | |
---|
[8e70823] | 60 | template<typename func_t> |
---|
[4f97937] | 61 | inline void ForAll(TreeTop & range, std::size_t level, func_t func, bool destroy) { |
---|
[8e70823] | 62 | auto it = range.head; |
---|
| 63 | while(it) { |
---|
| 64 | auto next = it->next; |
---|
| 65 | func(it, level); |
---|
| 66 | ForAll(it->children, level + 1, func); |
---|
| 67 | if(destroy) delete it; |
---|
| 68 | it = next; |
---|
| 69 | } |
---|
| 70 | } |
---|
| 71 | |
---|
[1cb7fab2] | 72 | template<TreeTop & top> |
---|
[8e70823] | 73 | class Tree : public TreeImpl { |
---|
| 74 | public: |
---|
[1cb7fab2] | 75 | Tree(const char * const name) : TreeImpl{name} { |
---|
[8e70823] | 76 | top.append(this); |
---|
| 77 | } |
---|
| 78 | |
---|
[1cb7fab2] | 79 | Tree(const char * const name, Tree * parent) : TreeImpl{name} { |
---|
[8e70823] | 80 | parent->children.append(this); |
---|
| 81 | } |
---|
| 82 | protected: |
---|
| 83 | virtual ~Tree() = default; |
---|
| 84 | }; |
---|
| 85 | } |
---|
| 86 | } |
---|