Medial Code Documentation
Loading...
Searching...
No Matches
MedGenUtils_imp.h
1//
2#ifndef __MED_GEN_UTILS_IMP_H__
3#define __MED_GEN_UTILS_IMP_H__
4
5//................................................................................................
6template <class T> void get_zero_inds(T *v, int len, vector<int> &inds)
7{
8 inds.clear();
9 for (int i=0; i<len; i++)
10 if (v[i] == (T)0)
11 inds.push_back(i);
12}
13
14//................................................................................................
15template <class T> void get_nonzero_inds(T *v, int len, vector<int> &inds)
16{
17 inds.clear();
18 for (int i=0; i<len; i++)
19 if (v[i] != (T)0)
20 inds.push_back(i);
21}
22
23//................................................................................................
24template <class T> void get_vec_from_vecvec(vector<vector<T>> &v_in, vector<T> &v_out)
25{
26 v_out.clear();
27
28 for (int i=0; i<v_in.size(); i++)
29 for (int j=0; j<v_in[i].size(); j++)
30 v_out.push_back(v_in[i][j]);
31}
32
33//................................................................................................
34// gets number of different values in a vector
35template <class T> int get_vec_ndiff_vals(vector<T> &v)
36{
37
38 if (v.size() == 0) return 0;
39
40 map<T, int> m;
41
42 for (int i=0; i<v.size(); i++) {
43 if (m.find(v[i]) == m.end())
44 m[v[i]] = 1;
45 }
46
47 return ((int)m.size());
48}
49
50
51
52// given two sorted vectors, where <in> has unique entries, find where the values of <search> would fit in <in>
53// if a searched entry is found exactily in <in> it will be considered to fit after the equal entry in <in>
54template <typename T> int find_sorted_vec_in_sorted_vec(const vector<T> &search, const vector<T> &in, vector<size_t>& indices) {
55 indices.clear();
56
57 if (in.empty())
58 return -1;
59
60 if (search.empty())
61 return 0;
62
63 indices.resize(search.size());
64
65 //at this point search.size() >= 1
66
67 //search_start, search_end - range of indices in <search> to handle
68 int search_start = (int)(search.size());
69 int search_end = 0;
70
71 //handle first search entries that fit at the beginning of <in>
72 for (int j = 0; j < search.size(); ++j) {
73 if (search[j] < in[0]) {
74 indices[j] = 0;
75 }
76 else {
77 search_start = j;
78 break;
79 }
80 }
81
82 for (int j = (int)(search.size()) - 1; j >= search_start; --j) {
83 if (search[j] >= in.back()) {
84 indices[j] = in.size();
85 }
86 else {
87 search_end = j;
88 break;
89 }
90 }
91
92 //note that for search_start =< j <= search_end we have search[j] in [in[0],in.back())
93
94 if (search_start > search_end)
95 return 0;
96
97 int start = 0;
98
99 for (int j = search_start; j <= search_end; ++j) {
100 T e = search[j];
101
102 int end = (int)(in.size()) - 1;
103
104 //we will always have an invariant: e is in [in[start],in[end])
105
106 while (1) {
107 if (start >= end - 1) {
108 //cout << indices.size() << " " << j << " " << start << " " << end << " " << search_start << " " << search_end << endl;
109 indices[j] = (size_t)end;
110 break;
111 }
112
113 int mid = (start + end) / 2;
114
115 if (e >= in[mid]) {
116 start = mid;
117 }
118 else {
119 end = mid;
120 }
121 }
122 }
123
124 return 0;
125}
126
127
128//................................................................................................
129// generates an arithmetic sequence
130template<typename T> int sequence(T start, T finish, T step, vector<T>& seq, bool isForward) {
131
132 if (step <= 0)
133 return -1;
134
135 seq.clear();
136 seq.reserve(1 + (size_t)((finish - start) / step));
137
138 if (isForward) {
139 T cur = start;
140
141 while (cur <= finish) {
142 seq.push_back(cur);
143 cur += step;
144 }
145 }
146 else {//backward
147 T cur = finish;
148
149 while (cur >= start) {
150 seq.push_back(cur);
151 cur -= step;
152 }
153
154 reverse(seq.begin(), seq.end());
155 }
156
157 seq.shrink_to_fit();
158 return 0;
159}
160
161//................................................................................................
162//comparators for pairs - by first and by second elements
163
164template<typename S, typename T> struct ComparePairBySecond {
165 bool operator()(const pair<S, T>& left, const pair<S, T>& right) {
166 return (left.second < right.second);
167 };
168};
169
170template<typename S, typename T> struct ComparePairByFirst {
171 bool operator()(const pair<S, T>& left, const pair<S, T>& right) {
172 return (left.first < right.first);
173 };
174};
175
176#endif
Definition MedGenUtils_imp.h:170
Definition MedGenUtils_imp.h:164