source: XIOS/dev/dev_olga/src/extern/remap/src/misc.hpp @ 1022

Last change on this file since 1022 was 1022, checked in by mhnguyen, 7 years ago
File size: 1.1 KB
Line 
1#ifndef  __MISC_H__
2#define  __MISC_H__
3
4#include <stdlib.h> // srand
5#include <algorithm> // swap
6#include <cmath> // pow
7#include <vector> // randomizeArray
8
9namespace sphereRemap {
10
11// y = b^x
12// always round down to integer
13
14// b unknown
15static int iroot(int x, int y)
16{
17        // FIXME: is this save to do? will it work correctly for integer cases like iroot(3, 8)?
18        // (what if roundoff errors produce pow(3., 1./8.) = 1.99999999999995? )
19        return pow(y, 1./x); 
20}
21
22// y unknown
23static int ipow(int basis, int exponent)
24{
25        int res = 1;
26        while (exponent--) res *= basis;
27        return res;
28}
29
30// x unknown
31// integer logarithm, rounds down
32static int ilog(int base, int arg)
33{
34        int x = 0;
35        for (int y = base; y <= arg; y *= base)
36                x++;
37        return x;
38}
39
40
41/* provide an array of indices that are in random order but every index appears exatly 1 time */
42static void randomizeArray(std::vector<int>& array)
43{
44        srand (3);
45
46        for (int i = 0; i < array.size(); i++)
47                array[i] = i;
48
49        for (int i = 0; i < 3 * array.size(); i++)
50        {
51                int ind1 = rand() % array.size();
52                int ind2 = rand() % array.size();
53                std::swap(array[ind1], array[ind2]);
54        }
55}
56
57}
58#endif
Note: See TracBrowser for help on using the repository browser.