Acoustic FDTD Solver
UniqueCoefficientContainer.hpp
Go to the documentation of this file.
1 #ifndef UNIQUE_HPP
2 #define UNIQUE_HPP
3 
4 #include <cmath>
5 #include <iostream>
6 #include <cstdlib>
7 #include <cassert>
8 
9 class Node {
10  Node *left;
11  Node *right;
12  double x;
13  unsigned short idx;
14 public:
15  Node () {
16  left = NULL;
17  right = NULL;
18  x = 0;
19  idx = 0;
20  }
21  Node(double x, unsigned short idx) {
22  this->x = x;
23  this->idx=idx;
24  left = NULL;
25  right = NULL;
26  }
27  Node(const Node &n) {
28  x = n.x;
29  idx = n.idx;
30  left = n.left;
31  right= n.right;
32  }
33  Node &operator=(const Node &n) {
34  x = n.x;
35  idx=n.idx;
36  left = n.left;
37  right = n.right;
38  return *this;
39  }
40  bool operator<(const Node &n) const {
41  return x < n.x;
42  }
43  bool operator>(const Node &n) const {
44  return x > n.x;
45  }
46  bool operator==(const Node &n) const {
47  return x == n.x;
48  }
49  unsigned short getIndex() const {
50  return idx;
51  }
52  double getValue() const {
53  return x;
54  }
55 
60  unsigned short setLeft(const Node &node) {
61  if (left == NULL) {
62  left = new Node(node);
63  return node.getIndex();
64  }
65 
66  if (node==*left) {
67  return left->getIndex();
68  } else if (node < *left) {
69  return left->setLeft(node);
70  } else { // (*left < node)
71  return left->setRight(node);
72  }
73 
74  }//setLeft()
75 
80  unsigned short setRight(const Node &node) {
81  if (right == NULL) {
82  right = new Node(node);
83  return node.getIndex();
84  }
85  if (node == *right) {
86  return right->getIndex();
87  } else if (node < *right) {
88  return right->setLeft(node);
89  } else { // (*right < node)
90  return right->setRight(node);
91  }
92  }//setRight()
93 
94  ~Node() {
95  if (left != NULL) {
96  delete left;
97  left = NULL;
98  }
99  if (right!= NULL) {
100  delete right;
101  right= NULL;
102  }
103  }
104 };
105 
106 
108  double *coeff;
109  unsigned short size;
111 
112  unsigned short insert(const Node &node) {
113  if (node.getIndex() == 0) {
114  // set the root node
115  root_node = node;
116  coeff[0] = node.getValue();
117  size = 1;
118  return 0;
119  }
120 
121  unsigned short idx=0;
122  if (node == root_node) {
123  return 0;
124  }
125 
126  if (node < root_node) {
127  idx = root_node.setLeft(node);
128  if (idx == size) { // a new node is added to the tree
129  coeff[size] = node.getValue();
130  size = size + 1;
131  }
132  }
133 
134  if (root_node < node) {
135  idx = root_node.setRight(node);
136  if (idx == size) { // a new node is added to the tree
137  coeff[size] = node.getValue();
138  size = size + 1;
139  }
140  }
141 
142  return idx;
143  }//insert
144 public:
146  int n = std::pow(2,16);
147  coeff = new double[n];
148  size = 0;
149  }
150 
159  unsigned short insert(double x) {
160  assert(size <= 65536);
161  Node node(x, size);
162  unsigned short idx = insert(node);
163  return idx;
164  }
165 
169  unsigned short insert_deprecated(double x) {
170  assert(size <= 65536);
171 
172  unsigned short idx;
173  for (idx = 0; idx < size; idx++) {
174  if (coeff[idx] == x)
175  return idx;
176  }
177  coeff[size] = x;
178  size++;
179  return size;
180  }
181 
185  const double * getHandle() {
186  return coeff;
187  }
188 
189  void print() const {
190  std::cout << "Coefficient container : " << std::endl;
191  for (unsigned i=0; i<size; i++)
192  std::cout << coeff[i] << std::endl;
193  }
194 
196  delete [] coeff;
197  size = 0;
198  }
199 };
200 
201 #endif