I want to create a graph based on the combined value of several overlapping events. I want one data point for each change in combined value, so for a set of events like this:
11111111111
22222
55555555555555
3333333
^ ^ ^ ^ ^ ^ ^ ^ ^
(The number indicates the value so for 3333333 the value is 3, the x axis represent time, the ^ markers represents the changes timestamp result are to be generated for)
I want an output like this:
timestamp0: 0,timestamp1: 2,timestamp2: (2+1),timestamp3:1,
timestamp4:(5+1),timestamp5:5,timestamp6:0,timestamp7:3,timestamp8:0
Is it a common way to do this, or a data structure or algorithm typically used to solve these types of problems?
I can manage to do this, but I wonder if there already exists a better way. New events will be added dynamically, and performance of result generation is important.
addition after the first answers arrived:
My current implementation is to loop through a list of events and adding the current one to a set of active events and check if any active events has become invalid since last iteration (these can be generated timestamps for and removed from the active list). The current implementation has a lot of looping and some special case handling (like if two events stop at the same time), so I wonder if you know of a smarter way?
I would keep an associative array (Object) where you store which events are active. Whenever an event is triggered or finalized (which are the only two situations which produce the changes you want to track) you add/remove an element to the object and the print its contents with a loop like the following: