-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrie_sort.cpp
More file actions
97 lines (71 loc) · 1.87 KB
/
trie_sort.cpp
File metadata and controls
97 lines (71 loc) · 1.87 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
//
// trie_sort.cpp
// TrieSort
//
// Created by Cameron Monks on 12/20/18.
// Copyright © 2018 Cameron Monks. All rights reserved.
//
#include "trie_sort.hpp"
template <class T>
TrieSortNode<T>::~TrieSortNode() {
for (unsigned i = 0; i < children.size(); i++) {
if (children[i] != NULL) {
delete children[i];
}
}
}
template <class T>
void TrieSortNode<T>::insert(const T &value) {
insert(value, 0);
}
template <class T>
void TrieSortNode<T>::insert(const T &value, unsigned index) {
PRETAZALS++;
if (index > value.maxIndex()) {
count++;
return;
}
unsigned i = value.hash(index);
if (children[i] == NULL) {
children[i] = new TrieSortNode();
}
children[i]->insert(value, index + 1);
}
template <class T>
std::vector<T> TrieSortNode<T>::sort(T value) const {
std::vector<T> arr;
sort(arr, value);
return arr;
}
template <class T>
void TrieSortNode<T>::sort(std::vector<T> &arr, T &value) const {
PRETAZALS+=count + 1;
for (unsigned i = 0; i < count; i++) {
arr.push_back(value);
}
for (unsigned i = 0; i < children.size(); i++) {
if (children[i] != NULL) {
T v = value;
v.add(i);
children[i]->sort(arr, v);
}
}
}
template <class T, class K>
std::vector<K> convertTo(const std::vector<T> &arr) {
std::vector<K> rArr;
for (unsigned i = 0; i < arr.size(); i++) {
rArr.push_back(arr[i]);
}
return rArr;
}
template <class T, class K>
std::vector<T> sort(const std::vector<T> &arr) {
std::vector<K> specialArray = convertTo<T, K>(arr);
TrieSortNode<K> head;
for (unsigned i = 0; i < arr.size(); i++) {
head.insert(specialArray[i]);
}
specialArray = head;
return convertTo<K, T>(specialArray);
}