man_power.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*-----------------------------------------------------------------------------+
  2. Interval Container Library
  3. Author: Joachim Faulhaber
  4. Copyright (c) 2007-2009: Joachim Faulhaber
  5. Copyright (c) 1999-2006: Cortex Software GmbH, Kantstrasse 57, Berlin
  6. +------------------------------------------------------------------------------+
  7. Distributed under the Boost Software License, Version 1.0.
  8. (See accompanying file LICENCE.txt or copy at
  9. http://www.boost.org/LICENSE_1_0.txt)
  10. +-----------------------------------------------------------------------------*/
  11. /** Example man_power.cpp \file man_power.cpp
  12. \brief Using set style operators to compute with interval sets and maps.
  13. Interval sets and maps can be filled and manipulated using
  14. set style operation like union (+=), difference (-=) and intersection
  15. (&=).
  16. In this example 'man_power' a number of those operations are
  17. demonstrated in the process of calculation the available working
  18. times (man-power) of a company's employees accounting for weekends,
  19. holidays, sickness times and vacations.
  20. \include man_power_/man_power.cpp
  21. */
  22. //[example_man_power
  23. // The next line includes <boost/gregorian/date.hpp>
  24. // and a few lines of adapter code.
  25. #include <boost/icl/gregorian.hpp>
  26. #include <iostream>
  27. #include <boost/icl/discrete_interval.hpp>
  28. #include <boost/icl/interval_map.hpp>
  29. using namespace std;
  30. using namespace boost::gregorian;
  31. using namespace boost::icl;
  32. // Function weekends returns the interval_set of weekends that are contained in
  33. // the date interval 'scope'
  34. interval_set<date> weekends(const discrete_interval<date>& scope)
  35. {
  36. interval_set<date> weekends;
  37. date cur_weekend_sat
  38. = first(scope)
  39. + days(days_until_weekday(first(scope), greg_weekday(Saturday)))
  40. - weeks(1);
  41. week_iterator week_iter(cur_weekend_sat);
  42. for(; week_iter <= last(scope); ++week_iter)
  43. weekends += discrete_interval<date>::right_open(*week_iter, *week_iter + days(2));
  44. weekends &= scope; // cut off the surplus
  45. return weekends;
  46. }
  47. // The available working time for the employees of a company is calculated
  48. // for a period of 3 months accounting for weekends and holidays.
  49. // The available daily working time for the employees is calculated
  50. // using interval_sets and interval_maps demonstrating a number of
  51. // addition, subtraction and intersection operations.
  52. void man_power()
  53. {
  54. date someday = from_string("2008-08-01");
  55. date thenday = someday + months(3);
  56. discrete_interval<date> scope = discrete_interval<date>::right_open(someday, thenday);
  57. // ------------------------------------------------------------------------
  58. // (1) In a first step, the regular working times are computed for the
  59. // company within the given scope. From all available days, the weekends
  60. // and holidays have to be subtracted:
  61. interval_set<date> worktime(scope);
  62. // Subtract the weekends
  63. worktime -= weekends(scope);
  64. // Subtract holidays
  65. worktime -= from_string("2008-10-03"); //German reunification ;)
  66. // company holidays (fictitious ;)
  67. worktime -= discrete_interval<date>::closed(from_string("2008-08-18"),
  68. from_string("2008-08-22"));
  69. //-------------------------------------------------------------------------
  70. // (2) Now we calculate the individual work times for some employees
  71. //-------------------------------------------------------------------------
  72. // In the company works Claudia.
  73. // This is the map of her regular working times:
  74. interval_map<date,int> claudias_working_hours;
  75. // Claudia is working 8 hours a day. So the next statement says
  76. // that every day in the whole scope is mapped to 8 hours worktime.
  77. claudias_working_hours += make_pair(scope, 8);
  78. // But Claudia only works 8 hours on regular working days so we do
  79. // an intersection of the interval_map with the interval_set worktime:
  80. claudias_working_hours &= worktime;
  81. // Yet, in addition Claudia has her own absence times like
  82. discrete_interval<date> claudias_seminar (from_string("2008-09-16"),
  83. from_string("2008-09-24"),
  84. interval_bounds::closed());
  85. discrete_interval<date> claudias_vacation(from_string("2008-08-01"),
  86. from_string("2008-08-14"),
  87. interval_bounds::closed());
  88. interval_set<date> claudias_absence_times(claudias_seminar);
  89. claudias_absence_times += claudias_vacation;
  90. // All the absence times have to subtracted from the map of her working times
  91. claudias_working_hours -= claudias_absence_times;
  92. //-------------------------------------------------------------------------
  93. // Claudia's boss is Bodo. He only works part time.
  94. // This is the map of his regular working times:
  95. interval_map<date,int> bodos_working_hours;
  96. // Bodo is working 4 hours a day.
  97. bodos_working_hours += make_pair(scope, 4);
  98. // Bodo works only on regular working days
  99. bodos_working_hours &= worktime;
  100. // Bodos additional absence times
  101. discrete_interval<date> bodos_flu(from_string("2008-09-19"), from_string("2008-09-29"),
  102. interval_bounds::closed());
  103. discrete_interval<date> bodos_vacation(from_string("2008-08-15"), from_string("2008-09-03"),
  104. interval_bounds::closed());
  105. interval_set<date> bodos_absence_times(bodos_flu);
  106. bodos_absence_times += bodos_vacation;
  107. // All the absence times have to be subtracted from the map of his working times
  108. bodos_working_hours -= bodos_absence_times;
  109. //-------------------------------------------------------------------------
  110. // (3) Finally we want to calculate the available manpower of the company
  111. // for the selected time scope: This is done by adding up the employees
  112. // working time maps:
  113. interval_map<date,int> manpower;
  114. manpower += claudias_working_hours;
  115. manpower += bodos_working_hours;
  116. cout << first(scope) << " - " << last(scope)
  117. << " available man-power:" << endl;
  118. cout << "---------------------------------------------------------------\n";
  119. for(interval_map<date,int>::iterator it = manpower.begin();
  120. it != manpower.end(); it++)
  121. {
  122. cout << first(it->first) << " - " << last(it->first)
  123. << " -> " << it->second << endl;
  124. }
  125. }
  126. int main()
  127. {
  128. cout << ">>Interval Container Library: Sample man_power.cpp <<\n";
  129. cout << "---------------------------------------------------------------\n";
  130. man_power();
  131. return 0;
  132. }
  133. // Program output:
  134. /*
  135. >>Interval Container Library: Sample man_power.cpp <<
  136. ---------------------------------------------------------------
  137. 2008-Aug-01 - 2008-Oct-31 available man-power:
  138. ---------------------------------------------------------------
  139. 2008-Aug-01 - 2008-Aug-01 -> 4
  140. 2008-Aug-04 - 2008-Aug-08 -> 4
  141. 2008-Aug-11 - 2008-Aug-14 -> 4
  142. 2008-Aug-15 - 2008-Aug-15 -> 8
  143. 2008-Aug-25 - 2008-Aug-29 -> 8
  144. 2008-Sep-01 - 2008-Sep-03 -> 8
  145. 2008-Sep-04 - 2008-Sep-05 -> 12
  146. 2008-Sep-08 - 2008-Sep-12 -> 12
  147. 2008-Sep-15 - 2008-Sep-15 -> 12
  148. 2008-Sep-16 - 2008-Sep-18 -> 4
  149. 2008-Sep-25 - 2008-Sep-26 -> 8
  150. 2008-Sep-29 - 2008-Sep-29 -> 8
  151. 2008-Sep-30 - 2008-Oct-02 -> 12
  152. 2008-Oct-06 - 2008-Oct-10 -> 12
  153. 2008-Oct-13 - 2008-Oct-17 -> 12
  154. 2008-Oct-20 - 2008-Oct-24 -> 12
  155. 2008-Oct-27 - 2008-Oct-31 -> 12
  156. */
  157. //]