Created
May 13, 2014 12:20
-
-
Save Wizmann/407c32a6c463c0f8304d to your computer and use it in GitHub Desktop.
DiscreteMap.cc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
#include <iostream> | |
#include <ctime> | |
#include <cassert> | |
#include <cstdlib> | |
#include <cstring> | |
#include <vector> | |
#include <map> | |
#include <algorithm> | |
using namespace std; | |
#define print(x) cout << x << endl | |
#define input(x) cin >> x | |
template<int map_size> | |
struct DiscreteMap { | |
DiscreteMap(){} | |
void init(int* i_array, int i_size) | |
{ | |
int idx = 1; | |
memset(m2o, -1, sizeof(m2o)); | |
o2m.clear(); | |
sort(i_array, i_array + i_size); | |
for (int i = 0; i < i_size; i++) { | |
m2o[idx] = i_array[i]; | |
o2m[i_array[i]] = idx; | |
idx++; | |
} | |
} | |
int disc(int big) | |
{ | |
if (o2m.find(big) == o2m.end()) { | |
return -1; | |
} | |
return o2m[big]; | |
} | |
int undisc(int small) | |
{ | |
if (small >= map_size) { | |
return -1; | |
} | |
return m2o[small]; | |
} | |
int m2o[map_size]; | |
map<int, int> o2m; | |
}; | |
int main() | |
{ | |
DiscreteMap<1000> dmap; | |
int tarr[] = {1, 10, 100, 1000, 10000}; | |
dmap.init(tarr, 5); | |
assert(dmap.disc(1) == 1); | |
assert(dmap.disc(10) == 2); | |
assert(dmap.disc(100) == 3); | |
assert(dmap.disc(1000) == 4); | |
assert(dmap.disc(10000) == 5); | |
assert(dmap.undisc(1) == 1); | |
assert(dmap.undisc(2) == 10); | |
assert(dmap.undisc(3) == 100); | |
assert(dmap.undisc(4) == 1000); | |
assert(dmap.undisc(5) == 10000); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment