forked from wreardan/cart
/
util.hpp
128 lines (111 loc) · 3.45 KB
/
util.hpp
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
/*
This file contains utility functions that are similar
to their Python versions, but missing from C++.
*/
#pragma once
#include <vector>
#include <string>
#include <sstream>
//http://stackoverflow.com/questions/10051679/c-tokenize-string
std::vector<std::string> inline split_string(const std::string &source, const char *delimiter = " ", bool keepEmpty = false)
{
std::vector<std::string> results;
size_t prev = 0;
size_t next = 0;
while ((next = source.find_first_of(delimiter, prev)) != std::string::npos)
{
if (keepEmpty || (next - prev != 0))
{
results.push_back(source.substr(prev, next - prev));
}
prev = next + 1;
}
if (prev < source.size())
{
results.push_back(source.substr(prev));
}
return results;
}
//does somethind similar to Python's ' '.join(list)
template<typename T>
std::string inline join(std::vector<T> & list, const char delimiter) {
std::stringstream ss;
for(int i = 0; i < list.size(); i++) {
ss << list[i];
if(i < list.size() - 1) {
ss << delimiter;
}
}
return ss.str();
}
//i.e. double<int> = merge(range(0, 10), range(15, 20));
//immutable
vector<int> inline merge(vector<int> a, vector<int> b) {
//add second array
for(int i = 0; i < b.size(); i++) {
a.push_back(b[i]);
}
return a;
}
//Similar to Python's range() function
//Returns a list of integers representing the range
vector<int> inline range(int start, int stop=-1, int step=1) {
vector<int> result;
if(stop == -1) {
stop = start;
start = 0;
}
for(int i = start; i < stop; i += step) {
result.push_back(i);
}
return result;
}
//Convert Python style negative indexes into regular indices
//i.e. (index as int input, list as vector<>)
//index = negative_index_convert(list, index);
template<typename T>
int inline negative_index_convert(std::vector<T> list, int index) {
//TODO: Bounds checking?
if(index >= 0) {
return index;
}
else {
return index + list.size();
}
}
//Slice an array, immutable, similar to Python's [::] operator
template<typename T>
std::vector<T> inline slice(std::vector<T> list, int start=0, int end=-1, int step=1) {
vector<T> result;
start = negative_index_convert(list, start);
end = negative_index_convert(list, end);
for(int i = start; i < end; i += step) {
T element = list[i];
result.push_back(element);
}
return result;
}
//http://www.thebrokendesk.com/post/zip-two-arrays-togeather-in-c/
//Python-like zip functionality
#include <algorithm> // std::sort
#include <utility> // std::pair
/* generate an array of tuples [(A0,B0), (A1,B1), ...] */
template<typename T>
void zip(std::vector<T> &A, std::vector<T> &B, std::vector<std::pair<T,T> > &zipped){
for(unsigned int i=0; i<A.size(); i++){
zipped.push_back(std::make_pair(A[i], B[i]));
}
}
/* take the values from the sorted tuple array and assign them to the initial arrays */
template<typename T>
void unzip(std::vector<std::pair<T, T> > &zipped, std::vector<T> &A, std::vector<T> &B){
for(unsigned int i=0; i<A.size(); i++){
A[i] = zipped[i].first;
B[i] = zipped[i].second;
}
}
/* conditional to sort by the first element of the tuple */
template<typename T>
bool pairCompare(const std::pair<T, T>& firstElem, const std::pair<T, T>& secondElem) {
return firstElem.first < secondElem.first;
}