-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree.h
More file actions
714 lines (620 loc) · 16 KB
/
Tree.h
File metadata and controls
714 lines (620 loc) · 16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
#pragma once
#include "nlohmann/json.hpp"
#include <algorithm>
#include <stdexcept>
#include <vector>
#include <exception>
#include <memory>
using json = nlohmann::json;
/**
@file Tree.h
@brief Tree structure
@note
Root item must be set during initalisation.
Item should define the "<", ">=", "==" and "!=" operators.
*/
namespace henn
{
#pragma region TreeModel
template <class Item>
struct TreeModel
{
public:
struct TreeItem
{
TreeItem() = default;
~TreeItem() = default;
TreeItem(const TreeItem& other)
: m_Item(other.m_Item ? std::make_unique<Item>(*other.m_Item) : nullptr)
{
m_Children.reserve(other.m_Children.size());
for (const auto& child : other.m_Children)
{
m_Children.push_back(std::make_unique<TreeItem>(*child));
}
};
TreeItem& operator=(const TreeItem& other)
{
if (this != &other)
{
TreeItem temp(other);
std::swap(m_Item, temp.m_Item);
std::swap(m_Children, temp.m_Children);
}
return *this;
}
std::unique_ptr<Item> m_Item;
std::vector<std::unique_ptr<TreeItem>> m_Children;
bool IsLeaveNode()
{
return m_Children.size() == 0;
};
bool operator<(const TreeItem& other) const
{
return (*this->m_Item < *other.m_Item);
};
bool operator==(const TreeItem& other) const
{
// First, compare the items themselves
if (!(*m_Item == *other.m_Item))
return false;
// Then compare the number of children
if (m_Children.size() != other.m_Children.size())
return false;
// Finally, compare each child
for (size_t i = 0; i < m_Children.size(); ++i)
{
if (!(*m_Children[i] == *other.m_Children[i]))
return false;
}
// Items and all children are equal
return true;
};
bool operator!=(const TreeItem& other) const
{
return !(*this == other);
}
};
public:
TreeModel()
{
m_nullItemPtr = std::make_unique<Item>();
};
~TreeModel() = default;
TreeModel(const TreeModel& other);
TreeModel(const json& j);
TreeModel(Item _item);
TreeModel(std::unique_ptr<TreeItem>&& treeItem) noexcept
: m_treeItem(std::move(treeItem))
{
m_nullItemPtr = std::make_unique<Item>();
};
TreeModel& operator=(const TreeModel& other);
bool operator==(const TreeModel& other)
{
return *m_treeItem == *other.m_treeItem;
};
bool operator!=(const TreeModel& other)
{
return !(*this == other);
};
bool Append(Item _item, Item _parentItem, const bool after = true);
Item Get(const size_t);
bool GetCopy(const Item& _item, std::unique_ptr<TreeItem>& ret)
{
return getRecursive(m_treeItem, _item, ret);
};
bool getRecursive(std::unique_ptr<TreeItem>& _treeItem, const Item& _item, std::unique_ptr<TreeItem>& ret)
{
if (*_treeItem->m_Item == _item)
{
ret = std::make_unique<TreeItem>(*_treeItem);
return true;
}
for (auto& treeItem : _treeItem->m_Children)
{
if (getRecursive(treeItem, _item, ret))
{
return true;
}
}
return false;
};
std::vector<Item> GetPath(const size_t index);
std::unique_ptr<Item>& Get(std::vector<Item> structure);
std::unique_ptr<TreeItem>& GetTreeItem()
{
return m_treeItem;
};
size_t Size() const;
size_t SizeLeaveNodes() const;
void SortSecondColumn();
void SortAllColumns();
void Clear()
{
m_treeItem.reset();
};
bool IsNull() const;
json GetJson() const;
template <typename UpdateFunction>
void UpdatePropertyForAllItems(UpdateFunction updateFunction)
{
if (!m_treeItem)
return;
updatePropertyRecursive(m_treeItem, updateFunction);
}
template <typename FindFunction>
std::vector<std::unique_ptr<Item>> FindItems(FindFunction findFunction)
{
if (!m_treeItem)
return std::vector<std::unique_ptr<Item>>{};
return findItemsRecursive(m_treeItem, findFunction);
}
private:
bool addRecursive(std::unique_ptr<TreeItem>&, Item, Item, const bool after);
bool getRecursive(std::unique_ptr<TreeItem>&, Item&, const size_t, size_t&);
bool getRecursivePath(std::unique_ptr<TreeItem>& currentItem, const size_t targetIndex, size_t& currentIndex, std::vector<Item>& path);
size_t sizeRecursive(const std::unique_ptr<TreeItem>&, bool) const;
void sortColumnsRecursive(std::unique_ptr<TreeItem>&);
void copyRecursive(std::unique_ptr<TreeItem>& dest, const std::unique_ptr<TreeItem>& src);
void from_json_recursive(std::unique_ptr<TreeItem>& treeItem, const json& j);
template <typename UpdateFunction>
void updatePropertyRecursive(std::unique_ptr<TreeItem>& currentItem, UpdateFunction updateFunction)
{
updateFunction(currentItem->m_Item);
for (auto& child : currentItem->m_Children)
{
updatePropertyRecursive(child, updateFunction);
}
}
template <typename FindFunction>
std::vector<std::unique_ptr<Item>> findItemsRecursive(std::unique_ptr<TreeItem>& currentItem, FindFunction findFunction)
{
std::vector<std::unique_ptr<Item>> result{};
if (findFunction(currentItem->m_Item))
result.emplace_back(std::move(std::make_unique<Item>(*currentItem->m_Item)));
for (auto& child : currentItem->m_Children)
{
auto ret = findItemsRecursive(child, findFunction);
for (auto& e : ret)
{
result.emplace_back(std::move(std::make_unique<Item>(*e)));
}
}
return result;
}
private:
std::unique_ptr<TreeItem> m_treeItem;
std::unique_ptr<Item> m_nullItemPtr;
};
template<typename Item>
TreeModel<Item>::TreeModel(const TreeModel& other)
{
m_nullItemPtr = std::make_unique<Item>();
// Perform a deep copy of the tree
if (other.m_treeItem)
{
m_treeItem = std::make_unique<TreeItem>();
copyRecursive(m_treeItem, other.m_treeItem);
}
}
template<typename Item>
TreeModel<Item>::TreeModel(const json& j)
{
m_nullItemPtr = std::make_unique<Item>();
if (!j.is_null() && j.find("item") != j.end())
{
m_treeItem = std::make_unique<TreeItem>();
from_json_recursive(m_treeItem, j);
}
};
template<typename Item>
TreeModel<Item>::TreeModel(Item _item)
{
m_nullItemPtr = std::make_unique<Item>();
m_treeItem = std::make_unique<TreeItem>();
m_treeItem->m_Item = std::make_unique<Item>(_item);
}
template<typename Item>
typename TreeModel<Item>::TreeModel& TreeModel<Item>::operator=(const TreeModel& other)
{
if (this != &other)
{
// Release existing resources
m_treeItem.reset();
m_nullItemPtr = std::make_unique<Item>();
// Perform a deep copy of the tree
if (other.m_treeItem)
{
m_treeItem = std::make_unique<TreeItem>();
copyRecursive(m_treeItem, other.m_treeItem);
}
}
return *this;
}
template<typename Item>
typename bool TreeModel<Item>::IsNull() const
{
if (!m_treeItem || !m_treeItem->m_Item)
return true;
return false;
}
template<typename Item>
void TreeModel<Item>::copyRecursive(std::unique_ptr<TreeItem>& dest, const std::unique_ptr<TreeItem>& src)
{
if (src)
{
dest->m_Item = std::make_unique<Item>(*src->m_Item);
for (const auto& child : src->m_Children)
{
auto newChild = std::make_unique<TreeItem>();
copyRecursive(newChild, child);
dest->m_Children.push_back(std::move(newChild));
}
}
}
template<typename Item>
void TreeModel<Item>::from_json_recursive(std::unique_ptr<TreeItem>& treeItem, const json& j)
{
if (!j.is_null() && j.find("item") != j.end())
{
// Deserialize the item
Item item = j["item"].get<Item>();
treeItem->m_Item = std::make_unique<Item>(std::move(item));
// Deserialize the children if they exist
if (j.find("children") != j.end())
{
for (const auto& child_json : j["children"])
{
auto childTreeItem = std::make_unique<TreeItem>();
from_json_recursive(childTreeItem, child_json);
treeItem->m_Children.push_back(std::move(childTreeItem));
}
}
}
}
/**
@brief Appends a new item to the tree.
This comment is to long, because I want to test doxygen!
@param _item The item to be added.
@param _parentItem The parent item under which the new item will be added.
@return
- @c true The item was successfully added to the tree.
- @c false The item could not be added, possibly because the parent item was not found in the tree structure.
@details
If the parent item is found in the tree structure, the new item will be added as a child
of the parent item. If the parent item is not found, the method will attempt to find the
parent item recursively in the children of all existing items.
@note
doxygen text
@code if (m_treeItem.m_Item == _parentItem) @endcode
@warning
doxygen text
@see
doxygen text
doxygen text
@todo
doxygen text
doxygen text
@throws
doxygen text
*/
template<typename Item>
bool TreeModel<Item>::Append(Item _item, Item _parentItem, const bool after)
{
bool bAdded = false;
if (*m_treeItem->m_Item == _parentItem)
{
auto treeItem = std::make_unique<TreeItem>();
treeItem->m_Item = std::make_unique<Item>(_item);
if (after)
m_treeItem->m_Children.emplace_back(std::move(treeItem));
else
m_treeItem->m_Children.insert(m_treeItem->m_Children.begin(), std::move(treeItem));
bAdded = true;
}
else
{
for (auto& item : m_treeItem->m_Children)
{
bAdded = addRecursive(item, _item, _parentItem, after);
if (bAdded)
break;
}
}
return bAdded;
}
/**
@note Start is 0
*/
template<typename Item>
Item TreeModel<Item>::Get(const size_t _index)
{
Item result;
size_t _nCounter = -1;
if (!getRecursive(m_treeItem, result, _index, _nCounter))
throw new std::exception("Index not found!");
return result;
}
/**
Sort second column from small to big.
*/
template<class Item>
void TreeModel<Item>::SortSecondColumn()
{
if (m_treeItem->m_Children.size() < 1)
return;
auto& children = m_treeItem->m_Children;
std::sort(children.begin(), children.end(), [](const std::unique_ptr<TreeItem>& a, const std::unique_ptr< TreeItem>& b)
{
return *a < *b;
});
}
/**
Sort second column from small to big.
*/
template<class Item>
void TreeModel<Item>::SortAllColumns()
{
sortColumnsRecursive(m_treeItem);
}
template<class Item>
void TreeModel<Item>::sortColumnsRecursive(std::unique_ptr<TreeItem>& treeItem)
{
if (treeItem->m_Children.size() < 1)
return;
auto& children = treeItem->m_Children;
std::sort(children.begin(), children.end(), [](const std::unique_ptr<TreeItem>& a, const std::unique_ptr< TreeItem>& b)
{
return *a < *b;
});
std::for_each(children.begin(), children.end(), [this](std::unique_ptr<TreeItem>& ti)
{
sortColumnsRecursive(ti);
});
}
template<class Item>
size_t TreeModel<Item>::Size() const
{
if (IsNull())
return 0;
size_t nResult = 1;
for (auto& treeItem : m_treeItem->m_Children)
{
nResult += sizeRecursive(treeItem, false);
}
return nResult;
}
template<class Item>
size_t TreeModel<Item>::SizeLeaveNodes() const
{
if (IsNull())
return 0;
size_t nResult = 0;
for (auto& treeItem : m_treeItem->m_Children)
{
nResult += sizeRecursive(treeItem, true);
}
return nResult;
}
template<typename Item>
bool TreeModel<Item>::addRecursive(std::unique_ptr<TreeItem>& _treeItem, Item _item, Item _parentItem, bool after)
{
if (*_treeItem->m_Item == _parentItem)
{
auto elems = std::make_unique<TreeItem>();
elems->m_Item = std::make_unique<Item>(_item);
if (after)
_treeItem->m_Children.emplace_back(std::move(elems));
else
_treeItem->m_Children.insert(_treeItem->m_Children.begin(), std::move(elems));
return true;
}
for (auto& layerChilds : _treeItem->m_Children)
{
if (addRecursive(layerChilds, _item, _parentItem, after))
return true;
}
return false;
}
template<typename Item>
size_t TreeModel<Item>::sizeRecursive(const std::unique_ptr<TreeItem>& _treeItem, bool _countJustLeaveNodes) const
{
size_t _nCounter = 0;
if (!_countJustLeaveNodes || (_countJustLeaveNodes && _treeItem->IsLeaveNode()))
_nCounter = 1;
for (auto& treeItem : _treeItem->m_Children)
{
_nCounter += sizeRecursive(treeItem, _countJustLeaveNodes);
}
return _nCounter;
};
template<typename Item>
bool TreeModel<Item>::getRecursive(std::unique_ptr<TreeItem>& _treeItem, Item& _refItem, const size_t _index, size_t& _nCounter)
{
_nCounter++;
if (_index == _nCounter)
{
_refItem = *_treeItem->m_Item;
return true;
}
for (auto& item : _treeItem->m_Children)
{
if (getRecursive(item, _refItem, _index, _nCounter))
{
return true;
}
}
return false;
};
/**
@note Start is 0
*/
template <class Item>
std::vector<Item> TreeModel<Item>::GetPath(const size_t index)
{
std::vector<Item> path;
if (!m_treeItem || index >= Size())
{
// Return an empty path if the tree is empty or the index is out of bounds
return path;
}
size_t currentIndex = 0;
getRecursivePath(m_treeItem, index, currentIndex, path);
return path;
}
template <class Item>
bool TreeModel<Item>::getRecursivePath(std::unique_ptr<TreeItem>& currentItem, const size_t targetIndex, size_t& currentIndex, std::vector<Item>& path)
{
path.push_back(*(currentItem->m_Item));
if (currentIndex == targetIndex)
{
// Found the target item, path is complete
return true;
}
for (auto& child : currentItem->m_Children)
{
currentIndex++;
// Recursively explore the child's subtree
if (getRecursivePath(child, targetIndex, currentIndex, path))
return true;
}
// If we reach here, the current item is not on the path to the target item
// Remove it from the path
path.pop_back();
return false;
}
template<typename Item>
json TreeModel<Item>::GetJson() const
{
// Helper lambda function to recursively convert TreeItems to JSON
std::function<json(const std::unique_ptr<TreeItem>&)> treeItemToJson = [&](const std::unique_ptr<TreeItem>& treeItem) -> json
{
if (!treeItem || !treeItem->m_Item)
{
// If the TreeItem or its Item is null, return a null JSON object
return json{};
}
json itemJson;
itemJson["item"] = *treeItem->m_Item;// Assuming that the Item type can be serialized to JSON
if (!treeItem->IsLeaveNode())
{
for (const auto& child : treeItem->m_Children)
{
itemJson["children"].push_back(treeItemToJson(child));
}
}
return itemJson;
};
// Assuming that the tree model itself is not null, serialize the root item and its descendants
if (!IsNull())
{
return treeItemToJson(m_treeItem);
}
// If the tree model is null, return an empty JSON object
return json{};
}
template<typename Item>
std::unique_ptr<Item>& TreeModel<Item>::Get(std::vector<Item> structure)
{
if (!m_treeItem || !m_treeItem->m_Item || structure.size() < 1 || *m_treeItem->m_Item != structure[0])
{
return m_nullItemPtr;
}
auto currentNode = m_treeItem.get();
// Traverse the tree based on the structure vector
for (size_t i = 1; i < structure.size(); ++i)
{
// Find the child with the current structure item
auto it = std::find_if(currentNode->m_Children.begin(), currentNode->m_Children.end(),
[&structure, i](const auto& child)
{
return child->m_Item && *(child->m_Item) == structure[i];
});
if (it == currentNode->m_Children.end())
{
return m_nullItemPtr;
}
// Move to the next level in the tree
currentNode = it->get();
}
// Return the found item
return currentNode->m_Item;
};
#pragma endregion
#pragma region Iterators
/**
Iterator base class.
@see Literature
Design Patterns: Elements of Reusable Object-Oriented Software
Chapter: BehaviourPatterns / Iterator
*/
template <class Item>
class Iterator
{
public:
virtual void First() = 0;
virtual void Next() = 0;
virtual bool IsDone() = 0;
virtual Item CurrentItem() = 0;
protected:
Iterator();
};
template<class Item>
inline Iterator<Item>::Iterator()
{
}
/**
Iterates through all elements from top to bottom.
*/
template <class Item>
class TopDownIterator : public Iterator<Item>
{
protected:
struct IteratorOutOfBounds : std::exception
{
using std::exception::exception;
};
public:
TopDownIterator(TreeModel<Item>);
virtual void First();
virtual void Next();
virtual bool IsDone();
virtual Item CurrentItem();
protected:
size_t m_lCurrent = 0;
TreeModel<Item> m_tree;
};
/**
Iterates through tree structure from to to bottom.
*/
template<typename Item>
TopDownIterator<Item>::TopDownIterator(TreeModel<Item> tree) : m_tree(tree)
{
}
template<typename Item>
void TopDownIterator<Item>::First()
{
m_lCurrent = 0;
}
template<typename Item>
void TopDownIterator<Item>::Next()
{
m_lCurrent++;
}
template<typename Item>
bool TopDownIterator<Item>::IsDone()
{
return m_lCurrent >= m_tree.Size();
}
template<typename Item>
Item TopDownIterator<Item>::CurrentItem()
{
//if (IsDone())
//{
// throw IteratorOutOfBounds("No valid state!");
//}
return m_tree.Get(m_lCurrent);
}
#pragma endregion
}