c++ - What's a good data structure for a 24hr minute-by-minute boolean record -


i tasked create data structure holds boolean every minute of last 24hrs. (did event x occur?) need keep last 24hrs time. (that is, data added, old data popped off.) data persisted flash drive. on embedded platform, memory isn't limited (i have 128mb available), fragmentation might become problem, though. realtime system, since record per minute, there's little runtime constraints.

the interface this:

class x_record {   public:     // record whether or not x occurred minute     void record_entry(bool x_occured);      // how many minutes has x occured in last 24hrs?      unsigned int x_occurance_minutes() const;    private:     // here dragons  }; 

what data structure store actual data in? current favorites std::deque<bool> , array of 24 long long, 60 of 64 bits each used 60mins of hour. latter current favorite persistence.

i think have pretty idea pros , cons of both ideas, hope of provide additional insides , maybe more ideas.

p.s.: strictly c++03 + tr1 + boost 1.52, no c++11/14 available.

to elaborate on vector<bool> version, think it's quite idea, since always store same amount of data (that's @ least understood):

class x_record {    vector<bool> data;    unsigned int currentminute;  public:    x_record(): data(24*60, false), currentminute(0){}     // record whether or not x occurred minute    void record_entry(bool x_occured){       data[currentminute] = x_occured;       currentminute = (currentminute+1)%data.size();    }  }; 

this way, vector size constant shouldn't fragmented (since it's allocated @ same time). can keep track of current minute currentminute variable. when fill fields, set 0 %(24*60) , overwrite old data, since don't need it.

you use normal array instead of vector<bool>, require either more space (since c++ stores bool values same way char), or bit manipulation - in opinion - reinventing wheel, when got vector<bool> specialization.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -