-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbtree_functions.h
102 lines (99 loc) · 2.61 KB
/
btree_functions.h
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
#ifndef BTREE_FUNCTIONS_H
#define BTREE_FUNCTIONS_H
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
template<typename T>
bool is_there(T* data, const size_t s, const T& target){
for(size_t i = 0; i < s; i++)
if(data[i]==target)
return true;
return false;
}
template<typename T>
size_t index_of(T* data, const size_t s, const T& target){
//Returns the index of the found target, else returns s
for(size_t i = 0; i < s; i++)
if(data[i] == target)
return i;
return s;
}
template<typename T>
size_t first_ge(T* data, const size_t s, const T& target){
//Return the first index that data[index] is >= target
for(size_t i = 0; i < s; i++)
if(target <= data[i])
return i;
return s;
}
template<typename T>
bool insert_sorted(T* data, size_t& s, const T& target, bool dupes=false){
if(!dupes && is_there(data, s, target))
return false;
data[s] = target;
for(size_t i = 0; s - i > 0; i++){ //insertion sort it
if(data[s-i] < data[s-i-1])
swap(data[s-i], data[s-i-1]);
else
break;
}
s++;
return true;
}
template<typename T>
void move_to_end(T* data, size_t& s, size_t i){
for(size_t j = i; j < s; j++){
swap(data[j], data[j+1]);
}
s--;
}
template<typename T>
void copy_array(T a_new[], const T a_copy[], const size_t& s){
for(size_t i = 0; i < s; i++)
a_new[i] = a_copy[i];
}
template<typename T>
void print_array(T arr[], const T& s){
for(size_t i = 0; int(i) < s; i++){
cout << "[" << setw(4) << setfill('0') << arr[i] << "] ";
}
cout << endl;
}
template<typename T>
void delete_item(T data[], int& i, int& s, T& entry){
swap(entry, data[i]);
s--;
for(int ii = i; i < s; i++)
swap(data[ii], data[ii+1]);
}
template<typename T>
ostream& operator <<(ostream& outs, const vector<T>& data){
outs << "{";
for(size_t i = 0; i < data.size(); i++){
outs << data[i];
if(i < data.size()-1)
outs << ", ";
}
outs << "}";
return outs;
}
template<typename T>
vector<T>& operator +=(vector<T>& left, const vector<T>& right){
for(size_t i = 0; i < right.size(); i++){
left.push_back(right[i]);
}
return left;
}
template<typename T>
vector<T>& operator +=(vector<T>& left, const T& right){
left.push_back(right);
return left;
}
template<typename T>
vector<T>& operator -=(vector<T>& left, const T& right){
left.erase(std::remove(left.begin(), left.end(), right), left.end());
return left;
}
#endif // BTREE_FUNCTIONS_H