| Quintiq file version 2.0 | 
| #parent: #root | 
| Method CleanUpUnitAvailability | 
| { | 
|   Description: 'Find sets of equivalent unit availabilities in consecutive periods and remove all but the first one' | 
|   TextBody: | 
|   [* | 
|     // Clean up unit availability | 
|     // Get a sorted set of all unit availabilities of this unit | 
|     sorteduas := selectsortedset( this, UnitAvailability, ua,  | 
|                                   true,  | 
|                                   ua.Start() ); | 
|      | 
|     head := null( UnitAvailability ); | 
|      | 
|     // Traverse the sorted set of unit availabilities | 
|     traverse( sorteduas, Elements, ua ) | 
|     { | 
|       // Check if there's any unit availability is selected, if there's none, set the current unit availability as selected | 
|       if( isnull( head ) ) | 
|       { | 
|         head := ua; | 
|       } | 
|      | 
|       // Get the index of the selected unit availability in the sorted set | 
|       headpos := sorteduas.Find( head ); | 
|       // Get the next unit availability after the selected unit availability in the sorted set | 
|       nextua := guard( sorteduas.Element( headpos + 1 ), null( UnitAvailability ) ); | 
|      | 
|       // Check if there's any next unit availability | 
|       if( not isnull( nextua ) ) | 
|       { | 
|         // Check if the next unit availability has the same data as the selected one | 
|         // If the data is the same, remove the next unit availability from the sorted set and dataset | 
|         if( head.HasSameData( nextua ) ) | 
|         { | 
|           sorteduas.Remove( nextua ); | 
|           nextua.Delete(); | 
|         } | 
|         // Else, set the next unit availability as the selected one | 
|         else | 
|         { | 
|           head := nextua; | 
|         } | 
|       } | 
|     } | 
|   *] | 
| } |