1 /* Pending_List class implementation.
2    Copyright (C) 2001-2010 Roberto Bagnara <bagnara@cs.unipr.it>
3    Copyright (C) 2010-2016 BUGSENG srl (http://bugseng.com)
4 
5 This file is part of the Parma Polyhedra Library (PPL).
6 
7 The PPL is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 The PPL is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307, USA.
20 
21 For the most up-to-date information see the Parma Polyhedra Library
22 site: http://bugseng.com/products/ppl/ . */
23 
24 #ifndef PPL_Pending_List_templates_hh
25 #define PPL_Pending_List_templates_hh 1
26 
27 #include <iostream>
28 
29 namespace Parma_Polyhedra_Library {
30 
31 namespace Implementation {
32 
33 namespace Watchdog {
34 
35 template <typename Traits>
36 typename Pending_List<Traits>::iterator
insert(const typename Traits::Threshold & deadline,const Handler & handler,bool & expired_flag)37 Pending_List<Traits>::insert(const typename Traits::Threshold& deadline,
38                              const Handler& handler,
39                              bool& expired_flag) {
40   iterator position = active_list.begin();
41   for (iterator active_list_end = active_list.end();
42        position != active_list_end
43          && Traits::less_than(position->deadline(), deadline);
44        ++position) {
45   }
46   iterator pending_element_p;
47   // Only allocate a new element if the free list is empty.
48   if (free_list.empty()) {
49     pending_element_p
50       = new Pending_Element<typename Traits::Threshold>(deadline,
51                                                         handler,
52                                                         expired_flag);
53   }
54   else {
55     pending_element_p = free_list.begin();
56     free_list.erase(pending_element_p);
57     pending_element_p->assign(deadline, handler, expired_flag);
58   }
59   iterator r = active_list.insert(position, *pending_element_p);
60   assert(OK());
61   return r;
62 }
63 
64 template <typename Traits>
65 bool
OK() const66 Pending_List<Traits>::OK() const {
67   if (!active_list.OK()) {
68     return false;
69   }
70   if (!free_list.OK()) {
71     return false;
72   }
73 
74   const typename Traits::Threshold* old;
75   const_iterator i = active_list.begin();
76   old = &i->deadline();
77   ++i;
78   for (const_iterator active_list_end = active_list.end(); i != active_list_end; ++i) {
79     const typename Traits::Threshold& t = i->deadline();
80     if (Traits::less_than(t, *old)) {
81 #ifndef NDEBUG
82       std::cerr << "The active list is not sorted!"
83                 << std::endl;
84 #endif
85       return false;
86     }
87     old = &t;
88   }
89   return true;
90 }
91 
92 } // namespace Watchdog
93 
94 } // namespace Implementation
95 
96 } // namespace Parma_Polyhedra_Library
97 
98 #endif // !defined(PPL_Pending_List_templates_hh)
99