PPL  0.12.1
Pending_List.templates.hh
Go to the documentation of this file.
00001 /* Pending_List class implementation.
00002    Copyright (C) 2001-2010 Roberto Bagnara <bagnara@cs.unipr.it>
00003    Copyright (C) 2010-2012 BUGSENG srl (http://bugseng.com)
00004 
00005 This file is part of the Parma Polyhedra Library (PPL).
00006 
00007 The PPL is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 The PPL is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with this program; if not, write to the Free Software Foundation,
00019 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307, USA.
00020 
00021 For the most up-to-date information see the Parma Polyhedra Library
00022 site: http://bugseng.com/products/ppl/ . */
00023 
00024 #ifndef PPL_Pending_List_templates_hh
00025 #define PPL_Pending_List_templates_hh 1
00026 
00027 #include <iostream>
00028 
00029 namespace Parma_Polyhedra_Library {
00030 
00031 namespace Implementation {
00032 
00033 namespace Watchdog {
00034 
00035 template <typename Traits>
00036 typename Pending_List<Traits>::Iterator
00037 Pending_List<Traits>::insert(const typename Traits::Threshold& deadline,
00038                              const Handler& handler,
00039                              bool& expired_flag) {
00040   Iterator position = active_list.begin();
00041   for (Iterator active_list_end = active_list.end();
00042        position != active_list_end
00043          && Traits::less_than(position->deadline(), deadline);
00044        ++position)
00045     ;
00046   Iterator pending_element_p;
00047   // Only allocate a new element if the free list is empty.
00048   if (free_list.empty())
00049     pending_element_p
00050       = new Pending_Element<typename Traits::Threshold>(deadline,
00051                                                         handler,
00052                                                         expired_flag);
00053   else {
00054     pending_element_p = free_list.begin();
00055     free_list.erase(pending_element_p);
00056     pending_element_p->assign(deadline, handler, expired_flag);
00057   }
00058   Iterator r = active_list.insert(position, *pending_element_p);
00059   assert(OK());
00060   return r;
00061 }
00062 
00063 template <typename Traits>
00064 bool
00065 Pending_List<Traits>::OK() const {
00066   if (!active_list.OK())
00067     return false;
00068 
00069   if (!free_list.OK())
00070     return false;
00071 
00072   const typename Traits::Threshold* old;
00073   Const_Iterator i = active_list.begin();
00074   old = &i->deadline();
00075   ++i;
00076   for (Const_Iterator active_list_end = active_list.end(); i != active_list_end; ++i) {
00077     const typename Traits::Threshold& t = i->deadline();
00078     if (Traits::less_than(t, *old)) {
00079 #ifndef NDEBUG
00080       std::cerr << "The active list is not sorted!"
00081                 << std::endl;
00082 #endif
00083       return false;
00084     }
00085     old = &t;
00086   }
00087   return true;
00088 }
00089 
00090 } // namespace Watchdog
00091 
00092 } // namespace Implementation
00093 
00094 } // namespace Parma_Polyhedra_Library
00095 
00096 #endif // !defined(PPL_Pending_List_templates_hh)