Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static IEnumerable<CampaignScheduleTaskInfo> GetGroupedPlayListItemsInRange(TimeLineResolution resolution, List<PlaylistItem> campaignPlaylistItems, DateTime startPosition, DateTime endPosition, uint maxInstances)
- {
- // Perform a sweep algorithm to find all playlistitems from all campaigns, where items from campaigns with lower index cover the ones with higher index
- // This array contains pointers for each campaign to the next playlist item after the current date time
- var data = new IEnumerator<CampaignScheduleTaskInfo>[campaignPlaylistItems.Count];
- int i = 0;
- foreach (var campaignPlaylistItem in from c in campaignPlaylistItems orderby c.Priority ascending select c)
- {
- data[i++] = GetGroupedPlayListItemsInRange(resolution, campaignPlaylistItem, startPosition, endPosition, maxInstances).GetEnumerator();
- }
- // Create initial sweep jobs
- i = 0;
- var sweepJobs = new List<SweepStatus>();
- foreach (var campaign in data)
- {
- CreateNewSweepJob(campaign, i++, ref sweepJobs);
- }
- // Do the sweep algorithm: one job at a time, from left to right
- SweepStatus status = null;
- while (sweepJobs.Count != 0)
- {
- var currentJob = (from x in sweepJobs orderby x.Position select x).FirstOrDefault();
- sweepJobs.Remove(currentJob);
- if (status == null)
- {
- status = currentJob;
- continue;
- }
- if (currentJob.StartPoint) // Job starts
- {
- if (currentJob.Index < status.Index) // Higher priority job starts, emit result
- {
- status.CampaignInfo.EndTime = currentJob.CampaignInfo.StartTime;
- yield return status.CampaignInfo;
- status = currentJob;
- }
- else // Lower priority job starts, ignore
- {
- }
- }
- else // Job ends
- {
- if (currentJob.Index > status.Index) // Lower priority job ends, increment enumerator and create new sweep jobs
- {
- CreateNewSweepJob(data[currentJob.Index], currentJob.Index, ref sweepJobs);
- }
- else // Higher priority job ends
- {
- yield return status.CampaignInfo;
- i = 0;
- bool found = false;
- foreach (var campaign in data)
- {
- if (campaign.Current.StartTime <= currentJob.Position && campaign.Current.EndTime >= currentJob.Position)
- {
- status = new SweepStatus()
- {
- Position = currentJob.Position,
- StartPoint = true,
- Index = i,
- CampaignInfo = campaign.Current
- };
- found = true;
- break;
- }
- ++i;
- }
- if (!found)
- status = null;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement