Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.65 KB | None | 0 0
  1. public static IEnumerable<CampaignScheduleTaskInfo> GetGroupedPlayListItemsInRange(TimeLineResolution resolution, List<PlaylistItem> campaignPlaylistItems, DateTime startPosition, DateTime endPosition, uint maxInstances)
  2.         {
  3.             // Perform a sweep algorithm to find all playlistitems from all campaigns, where items from campaigns with lower index cover the ones with higher index
  4.            
  5.             // This array contains pointers for each campaign to the next playlist item after the current date time
  6.             var data = new IEnumerator<CampaignScheduleTaskInfo>[campaignPlaylistItems.Count];
  7.             int i = 0;
  8.             foreach (var campaignPlaylistItem in from c in campaignPlaylistItems orderby c.Priority ascending select c)
  9.             {
  10.                 data[i++] = GetGroupedPlayListItemsInRange(resolution, campaignPlaylistItem, startPosition, endPosition, maxInstances).GetEnumerator();
  11.             }
  12.  
  13.             // Create initial sweep jobs
  14.             i = 0;
  15.             var sweepJobs = new List<SweepStatus>();
  16.             foreach (var campaign in data)
  17.             {
  18.                 CreateNewSweepJob(campaign, i++, ref sweepJobs);
  19.             }
  20.  
  21.             // Do the sweep algorithm: one job at a time, from left to right
  22.             SweepStatus status = null;
  23.             while (sweepJobs.Count != 0)
  24.             {
  25.                 var currentJob = (from x in sweepJobs orderby x.Position select x).FirstOrDefault();
  26.                 sweepJobs.Remove(currentJob);
  27.  
  28.                 if (status == null)
  29.                 {
  30.                     status = currentJob;
  31.                     continue;
  32.                 }
  33.  
  34.                 if (currentJob.StartPoint) // Job starts
  35.                 {
  36.                     if (currentJob.Index < status.Index) // Higher priority job starts, emit result
  37.                     {
  38.                         status.CampaignInfo.EndTime = currentJob.CampaignInfo.StartTime;
  39.                         yield return status.CampaignInfo;
  40.                         status = currentJob;
  41.                     }
  42.                     else // Lower priority job starts, ignore
  43.                     {
  44.  
  45.                     }
  46.                 }
  47.                 else // Job ends
  48.                 {
  49.                     if (currentJob.Index > status.Index) // Lower priority job ends, increment enumerator and create new sweep jobs
  50.                     {
  51.                         CreateNewSweepJob(data[currentJob.Index], currentJob.Index, ref sweepJobs);
  52.                     }
  53.                     else // Higher priority job ends
  54.                     {
  55.                         yield return status.CampaignInfo;
  56.  
  57.                         i = 0;
  58.                         bool found = false;
  59.                         foreach (var campaign in data)
  60.                         {
  61.                             if (campaign.Current.StartTime <= currentJob.Position && campaign.Current.EndTime >= currentJob.Position)
  62.                             {
  63.                                 status = new SweepStatus()
  64.                                 {
  65.                                     Position = currentJob.Position,
  66.                                     StartPoint = true,
  67.                                     Index = i,
  68.                                     CampaignInfo = campaign.Current
  69.                                 };
  70.                                 found = true;
  71.                                 break;
  72.                             }
  73.                             ++i;
  74.                         }
  75.                         if (!found)
  76.                             status = null;
  77.                     }
  78.                 }
  79.             }
  80.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement