title: Time periods merge by priority algorithm
body: <p>I'm stuck trying to develop an algorithm for resolving time periods with different priority in classic ASP.</p>
  
  <p>Is there any way to resolve time period overlapped by higher priority period (without resolving priority in every day)?</p>
  
  <p>--</p>
  
  <p>example input:</p>
  
  <pre><code>periodId(0) = 0: startDate(0) = dateValue("15-01-01"): endDate(0) = dateValue("15-03-31")    
  periodId(1) = 1: startDate(1) = dateValue("15-01-18"): endDate(1) = dateValue("15-02-08")    
  periodId(2) = 2: startDate(2) = dateValue("15-02-02"): endDate(2) = dateValue("15-02-16")    
  periodId(3) = 3: startDate(3) = dateValue("15-01-29"): endDate(3) = dateValue("15-02-05")    
  periodId(4) = 4: startDate(4) = dateValue("15-03-01"): endDate(4) = dateValue("15-03-08")
  </code></pre>
  
  <p>--</p>
  
  <p>example output:</p>
  
  <pre><code>resolvedId(0) = 0: resolvedStart(0) = "15-01-01": resolvedEnd(0) = "15-01-17"    
  resolvedId(1) = 1: resolvedStart(1) = "15-01-18": resolvedEnd(1) = "15-01-28"    
  resolvedId(2) = 3: resolvedStart(2) = "15-01-29": resolvedEnd(2) = "15-02-05"    
  resolvedId(3) = 2: resolvedStart(3) = "15-02-06": resolvedEnd(3) = "15-02-16"    
  resolvedId(4) = 0: resolvedStart(4) = "15-02-17": resolvedEnd(4) = "15-02-28"    
  resolvedId(5) = 4: resolvedStart(5) = "15-03-01": resolvedEnd(5) = "15-03-08"    
  resolvedId(6) = 0: resolvedStart(6) = "15-03-09": resolvedEnd(6) = "15-03-31"
  </code></pre>
  
  <p>example in graphic:</p>
  
  <p><img src="https://i.stack.imgur.com/PV0oL.png" alt="Example Output"></p>
  
  <p>...any ideas?</p>
  
  <p>Thank you in advance!</p>
  
  <p>----- EDIT 15-03-13 -----</p>
  
  <p>I need array as output - image was only for better understanding. I need arrays with dates as output...</p>
  
  <p>I developed algorithm:</p>
  
  <pre><code>&lt;%' ....
  i = 0
  while not objRS.Eof
      redim preserve periodId(i)  'integer
      redim preserve startDate(i)  'date
      redim preserve endDate(i)    'date
  
      'DB parse
      periodId(i) = objRS("idc")
      periodStartDateIn(i) = DateValue(objRS("tempDatumStart"))
      periodEndDateIn(i)   = DateValue(objRS("tempDatumKonec"))
  
      'date limit detection
      if periodStartDateIn(i) &lt; resolvedMinDate then resolvedMinDate = periodStartDateIn(i)
      if periodEndDateIn(i)   &gt; resolvedMaxDate then resolvedMaxDate = periodEndDateIn(i)
  
      i = i + 1
      objRs.moveNext
  wend
  
  lastIndex = -1
  redim preserve resolvedIndex(0)
  redim preserve resolvedStartDate(0)
  redim preserve resolvedEndDate(0)
  for dateActual = resolvedMinDate to resolvedMaxDate
      for index = lbound(periodId) to ubound(periodId)
          if periodStartDateIn(index) &lt;= dateActual and dateActual &lt;= periodEndDateIn(index) then
              IndexActual = index
              exit for
          end if
      next
  
      if IndexActual &lt;&gt; lastIndex then
          if lastIndex &lt;&gt; -1 then 'not first pass
              redim preserve resolvedIndex(ubound(resolvedIndex) + 1)
              redim preserve resolvedStartDate(ubound(resolvedStartDate) + 1)
              redim preserve resolvedEndDate(ubound(resolvedEndDate) + 1)
              resolvedEndDate(ubound(resolvedEndDate)-1) = dateActual - 1 'close prev period
          end if
          resolvedIndex(ubound(resolvedIndex)) = IndexActual
          resolvedStartDate(ubound(resolvedStartDate)) = dateActual
      end if
      lastIndex = IndexActual
  next
  resolvedEndDate(ubound(resolvedEndDate)) = dateActual - 1
  
  '...
  %&gt;
  </code></pre>
  
  <p>...but it checks every date in interval. This should take quite long time to resolve in case of many periods and few year-decades between startDate and endDate...</p>
  
  <p>...is there any algorith to solve it analytic way?</p>