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