hongji.li
2023-11-03 aefafd2142478d4fb07d6b8b45c3047e247389e0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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;
        }
      }
    }
  *]
}