Sequencer.cc

00001 // Copyright 2008 Michael Marsh, University of Maryland.
00002 //
00003 // This file is part of pydtn.
00004 //
00005 // pydtn is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // pydtn is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with pydtn.  If not, see <http://www.gnu.org/licenses/>.
00017 //
00018 // The views and conclusions contained in the software and documentation
00019 // are those of the authors and should not be interpreted as representing
00020 // official policies, either expressed or implied, of the University
00021 // of Maryland.
00022 //
00023 // pydtn extends and embeds the Python interpreter, which is
00024 // Copyright 2001-2006 Python Software Foundation, All Rights Reserved,
00025 // and is released under the PSF License Agreement.
00026 //
00027 // RANLUX random number generation uses the Boost library,
00028 // Copyright 1994-2006 by various authors (details in individual files),
00029 // which is released under the Boost Software License, Version 1.0.
00030 
00031 #include "Sequencer.h"
00032 
00033 Sequencer::Sequencer() :
00034    m_seenAny( false ),
00035    m_stragglers( 0 )
00036 {
00037 }
00038 
00039 Sequencer::Sequencer( const Sequencer& seq ) :
00040    m_seenAny( seq.m_seenAny ),
00041    m_first( seq.m_first ),
00042    m_through( seq.m_through ),
00043    m_stragglers( seq.m_stragglers )
00044 {
00045 }
00046 
00047 Sequencer::~Sequencer()
00048 {
00049    if ( 0 != m_stragglers )
00050    {
00051       delete m_stragglers;
00052    }
00053 }
00054 
00055 bool
00056 Sequencer::add( uint32_t s )
00057 {
00058    if ( ! m_seenAny )
00059    {
00060       m_seenAny = true;
00061       m_first = s;
00062       m_through = s;
00063       return true;
00064    }
00065    if ( s < m_first )
00066    {
00067       if ( s == (m_first - 1) )
00068       {
00069          m_first = s;
00070          return true;
00071       }
00072       Sequencer* temp = new Sequencer( *this );
00073       m_first = s;
00074       m_through = s;
00075       m_stragglers = temp;
00076       return true;
00077    }
00078    if ( s <= m_through )
00079    {
00080       return false;
00081    }
00082    if ( s == (m_through + 1) )
00083    {
00084       m_through = s;
00085 
00086       // grab the front of stragglers if appropriate
00087       if ( 0 != m_stragglers )
00088       {
00089          if ( m_stragglers->m_first == (m_through+1) )
00090          {
00091             m_through = m_stragglers->m_through;
00092             Sequencer* temp = m_stragglers;
00093             m_stragglers = temp->m_stragglers;
00094             temp->m_stragglers = 0;
00095             delete temp;
00096          }
00097       }
00098 
00099       return true;
00100    }
00101    if ( 0 == m_stragglers )
00102    {
00103       m_stragglers = new Sequencer();
00104    }
00105    return m_stragglers->add(s);
00106 }

Generated on Mon Mar 24 11:15:46 2008 for Pydtn Simulator by  doxygen 1.5.4