I am stuck on my code without any ideas.
I have a :
QList<QPair<QTime,QTime>> data;
Where my QPair represents a Initial Time, End time that is basically a time frame where something is scheduled.
I have this Qlist in order to know what schedules I have for a specific day.
I need to know what are the free times and I am run out of ideas about how I can do that. I start with making a Qlist to put everything on the same place in terms of schedules and ordered that Qlist.
First of all, the
QPair
is not a very descriptive type. It makes sense to have your own structure:Since all of the operations are simplest to perform on individual events, rather than blocks that are pairs of events, let’s also have a representation of a single event. The event indicates the beginning or an end of a chunk of time that may be available, or busy. The availability, or busyness, are considered separately. The
available
member indicates that a block of availability begins (1) or ends (-1). Thebusy
member indicates similarly that a period of busyness begins (1) or ends (-1).Then you'll want to sort the blocks according to their start time, join the overlapping ones, then merge them according to the desired operation. You want to subtract busy times from available times, so the sought output is “AvailableNotBusy”: the period of time must be both originally available, and not overlapping with busy period.
For example, to get free schedule you want all blocks of time that were available and also weren't busy:
The conversion between blocks and events is rather mechanical:
Events are sorted by time:
Now, to merge the events, we iterate through them in the order of time, while keeping track of whether at any point we’re within an available period, and/or within a busy period. This is indicated by non-zero value of the running sum
available
andbusy
, respectively. The values of these sums indicate how many blocks of a given type are overlapping at any given time. E.g.busy==3
means that we’re within 3 overlapping busy blocks. The operation that determines what output we get takes the current value of the running sums. The outcome is dumped as a merged event any time the result of the operation changes upon passing a point in time. Overlapping event times are handled by only looking for changes inoperation
’s outcome after leaving a time point. TherecessiveKind
of an event is the default event type we start with. The first change of operation’s result away from that event type will cause the first event to be emitted.Note: there may be a bug in this code :)
There are a few common operations you may wish to perform:
MergeOp::Available
: extract only events related to availability, ignoring busyness.MergeOp::Busy
: extract only events related to busyness, ignoring availability.MergeOp::AvailableNotBusy
: extract events when the(available && !busy)
status changes.MergeOp::BusyNotAvailable
: extract events when the(busy && !available)
status changes.