I'm implementing DS in C++ that support regular get()
and set()
plus set_all()
in O(1).
For that I saved for each element a time strap of when it was updated the last time and compared it to when I lastly updated the set_all variable.
The problem is that I used std::time
to measure time which wasn't good enough as hundreds of operations could occur in same time.
How can I solve this?
class evector {
vector<int> vec;
vector<time_t> t_straps;
int g_val = 0;
time_t g_strap = 0;
public:
explicit evector(int size, int init_val = 0) : vec(size, init_val), t_straps(size, time(0)) {
}
void set(int val, int index) {
vec[index] = val;
t_straps[index] = time(0);
}
int get(int index) {
if (t_straps[index] > g_strap)
return vec[index];
return g_val;
}
void set_all(int val) {
g_val = val;
g_strap = time(0);
}
};