Medial Code Documentation
Loading...
Searching...
No Matches
MedGenUtils.h
1//
2// MedGenUtils - general needed utilities (time conversions, vector ops, randomizing, etc)
3//
4
5#ifndef __MED_GEN_UTILS_H__
6#define __MED_GEN_UTILS_H__
7
8#include <stdlib.h>
9#include <vector>
10#include <map>
11#include <MedUtils/MedUtils/MedGlobalRNG.h>
12
13
14#include <boost/algorithm/string/classification.hpp>
15#include <boost/algorithm/string/split.hpp>
17
18using namespace std;
19
20//================================================================
21// Time / Date related
22//================================================================
23// days from 01011900 - function is in h file since it is inline.
24//inline
25inline int date_to_days(int date);
26inline int year_to_days(int year) {return date_to_days(year*10000+0701);}
27inline float get_age(int date, int byear) {return ((float)(date_to_days(date) - year_to_days(byear))/365.0f);}
28
29//================================================================
30// vector/array operations
31//================================================================
32
33#define VEC_DATA(v) ((v).size()>0 ? &((v)[0]) : NULL)
34//#define VEC_DATA(v) ((v).size()>0 ? (v).data() : 0)
35
36// get the a vector of all indexes such that v[i] == 0
37template <class T> void get_zero_inds(T *v, int len, vector<int> &inds);
38// get the a vector of all indexes such that v[i] != 0
39template <class T> void get_nonzero_inds(T *v, int len, vector<int> &inds);
40// get the a vector of all indexes such that v[i] == 0 , c++ ver
41template <class T> void get_zero_inds(vector<T> &v, vector<int> &inds) {get_zero_inds(&v[0],(int)v.size(),inds);}
42// get the a vector of all indexes such that v[i] != 0 , c++ ver
43template <class T> void get_nonzero_inds(vector<T> &v, vector<int> &inds) {get_nonzero_inds(&v[0],(int)v.size(),inds);}
44// gets all elements in a vector<vector<T>> to a single vector<T>
45template <class T> void get_vec_from_vecvec(vector<vector<T>> &v_in, vector<T> &v_out);
46// gets number of different values in a vector
47template <class T> int get_vec_ndiff_vals(vector<T> &v);
48
49// generate an arithmetic sequence start:step:finish. if step <= 0 returns -1 otherwise 0,
50// and fills seq with the sequence
51// isForward - go from start to finish or backward from finish
52template<typename T> int sequence(T start, T finish, T step, vector<T>& seq, bool isForward = true);
53
54// gets a (sorted) partition on the values in a vector and gets the matching categorized vec
55void categorize_vec(vector<float> &in, vector<float> &bounds, vector<float> &out);
56
57void get_probs_vec(vector<float> &v);
58
59//================================================================
60// Random Numbers
61//================================================================
62
63// wrappers for globalRNG::rand()
64//....................
65
66// set the random seed, if called with -1 will take seed from current time
67void set_rand_seed(int seed);
68
69void get_rand_splits(vector<int> &split, int nsplits, int size);
70
71// gets first size elements of a permutaion of 0..N-1
72void get_rand_vector_no_repetitions(vector<int> &v, int N, int size);
73
74// gets size random values in range 0...N-1
75void get_rand_vector_with_repetitions(vector<int> &v, int N, int size);
76
77
78//==========================================================
79// Inline functions
80//==========================================================
81// conversions
82// days from 01011900 - function is in h file since it is inline.
83inline int date_to_days(int date)
84{
85 int y,d,m;
86
87 y = date/10000;
88 m = (date - y*10000)/100;
89 d = date - y*10000 - m*100;
90
91 return (y*365 + (m-1)*30 + d);
92
93}
94
95int get_day(int val) ;
96int get_day_approximate(int val);
97int get_date(int days);
98
99//================================================================
100// Initialization Utilities
101//================================================================
102// OLD CODE
103//int initialization_text_to_map(const string& text, map<string, string>& init_map);
104
105//===========================
106// Opertaing System Utilities
107//===========================
108
109//is the OS Windows
110bool is_windows_os(void);
111
112// templated functions in _imp file
113#include "MedGenUtils_imp.h"
114
115#endif
Logger.h - allowing logs with more control.
Definition StdDeque.h:58