Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am tasked to create a data structure that holds a boolean for every minute of the last 24hrs. (Did event X occur?) I need to keep the last 24hrs all the time. (That is, data will constantly be added, old data popped off.) The data is to be persisted to a flash drive. We are on an embedded platform, but memory isn't that limited (I have 128MB available), fragmentation might become a problem, though. This is a realtime system, but since the record is per minute, there's little runtime constraints.

The interface could look something like this:

class x_record {
  public:
    // record whether or not x occurred this minute
    void record_entry(bool x_occured);

    // how many minutes has x occured in the last 24hrs? 
    unsigned int x_occurance_minutes() const;

  private:
    // HERE BE DRAGONS 
};

What would be a good data structure to store the actual data in? My current favorites are std::deque<bool> and an array of 24 long long, with 60 of their 64 bits each used for the 60mins of an hour. The latter is the current favorite for persistence.

I think I have a pretty good idea about the pros and cons of both ideas, but would hope some of you could provide additional insides and maybe even more ideas.

P.S.: This is strictly C++03 + TR1 + Boost 1.52, no C++11/14 available.

like image 482
sbi Avatar asked Nov 28 '22 07:11

sbi


1 Answers

To elaborate on the vector<bool> version, I think it's quite a good idea, since you always store the same amount of data (that's at least what I 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 this minute
   void record_entry(bool x_occured){
      data[currentMinute] = x_occured;
      currentMinute = (currentMinute+1)%data.size();
   }

};

This way, the vector size is constant so it shouldn't be fragmented (since it's allocated all at the same time). You can keep track of the current minute with the currentMinute variable. When you fill all the fields, you just set it to 0 with %(24*60) and overwrite the old data, since you don't need it.

You could also use a normal array instead of a vector<bool>, but that would require either more space (since normally C++ stores bool values the same way as a char), or some bit manipulation which is - in my opinion - reinventing the wheel, when we got the vector<bool> specialization.

like image 129
Paweł Stawarz Avatar answered Dec 10 '22 11:12

Paweł Stawarz