Guest User

Untitled

a guest
Jul 21st, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. local_procedure_body Initialize ()
  2. {
  3. self[inserting_rep] = true;
  4. }
  5.  
  6. local_procedure_body Partition (
  7. consumes Queue_Of_Item& q,
  8. preserves Item& p,
  9. produces Queue_Of_Item& q1,
  10. produces Queue_Of_Item& q2
  11. )
  12. /*!
  13. ensures
  14. q1 * q2 is permutation of #q and
  15. for all x: integer where (x is in elements (q1))
  16. (ARE_IN_ORDER(x, p)) and
  17. for all x: integer where (x is in elements (q2))
  18. (not ARE_IN_ORDER(x, p))
  19. !*/
  20. {
  21. q1.Clear();
  22. q2.Clear();
  23. while (q.Length > 0)
  24. {
  25. object Item temp;
  26. q.Dequeue (temp);
  27. if (Item_Are_In_Order::Are_In_Order (p, temp))
  28. {
  29. q2.Enqueue(temp);
  30. }
  31. else
  32. {
  33. q1.Enqueue(temp);
  34. }
  35. }
  36. }
  37.  
  38. local_procedure_body Combine (
  39. produces Queue_Of_Item& q,
  40. consumes Item& p,
  41. consumes Queue_Of_Item& q1,
  42. consumes Queue_Of_Item& q2
  43. )
  44. /*!
  45. ensures
  46. q = #q1 * <#p> * #q2
  47. !*/
  48. {
  49. object Integer temp; // used in the loop
  50. q &= q1;
  51. q1.Clear();
  52. q.Enqueue(p);
  53. while (q2.Length() > 0)
  54. {
  55. q2.Dequeue(temp);
  56. q.Enqueue(temp);
  57. }
  58. }
  59.  
  60. local_procedure_body Sort (
  61. alters Queue_Of_Item& q
  62. )
  63. /*!
  64. ensures
  65. q is permutation of #q and
  66. IS_ORDERED (q)
  67. decreases
  68. |q|
  69. !*/
  70. {
  71. if (q.Length() > 1)
  72. {
  73. object Item p;
  74. q.Dequeue (p);
  75. object Queue_Of_Item q1, q2;
  76. Partition (q, p, q1, q2);
  77. Sort (q1);
  78. Sort (q2);
  79. Combine (q, p, q1, q2);
  80. }
  81. }
  82.  
  83.  
  84.  
  85. public:
  86.  
  87. standard_concrete_operations (Sorting_Machine_Kernel_3);
  88.  
  89. procedure_body Insert (
  90. consumes Item& x
  91. )
  92. {
  93. self[contents_rep].Enqueue (x);
  94. }
  95.  
  96. procedure_body Remove_First (
  97. produces Item& x
  98. )
  99. {
  100. self[contents_rep].Dequeue (x);
  101. }
  102.  
  103. procedure_body Remove_Any (
  104. produces Item& x
  105. )
  106. {
  107. self[contents_rep].Dequeue (x);
  108. }
  109.  
  110. procedure_body Change_To_Extraction_Phase ()
  111. {
  112. self[inserting_rep] = false;
  113. Sort(self[contents_rep]);
  114. }
  115.  
  116. function_body Boolean Is_In_Extraction_Phase ()
  117. {
  118. return not (self[inserting_rep]);
  119. }
  120.  
  121. function_body Integer Size ()
  122. {
  123. return self[contents_rep].Length();
  124. }
  125.  
  126. };
Add Comment
Please, Sign In to add comment