Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

WindowPosition.cpp

Go to the documentation of this file.
00001 #include "headers.h"
00011 
00015 WindowPosition::WindowPosition()
00016 {
00017     m_seqNum = WindowPosition::DONT_CARE;
00018     m_windowSize = WINDOW_SIZE;
00019     m_seqNumSpace = 1;
00020 } // ctor
00021 
00022 
00026 WindowPosition::WindowPosition(int size, int start)
00027 {
00028     if (size < 0) {
00029         debug(DEBUG_WP, "Size less than zero");
00030         m_seqNumSpace = 1;
00031     }
00032     m_seqNumSpace = size;
00033 
00034         if (start < -1) {
00035         debug(DEBUG_WP, "start position out of range");
00036         m_seqNum = WindowPosition::DONT_CARE;
00037         }
00038         else {
00039                 m_seqNum = start;
00040         }
00041     m_windowSize = WINDOW_SIZE;
00042 } // ctor
00043 
00044 
00048 WindowPosition::WindowPosition(const WindowPosition& other) {
00049     m_seqNum = other.m_seqNum;
00050     m_seqNumSpace = other.m_seqNumSpace;
00051     m_windowSize = other.m_windowSize;
00052 } // copy ctor
00053 
00054 
00058 WindowPosition 
00059 WindowPosition::operator=(const WindowPosition& other) {
00060     m_seqNum = other.m_seqNum;
00061     m_seqNumSpace = other.m_seqNumSpace;
00062     m_windowSize = other.m_windowSize;
00063     return *this;
00064 } // fn operator=
00065 
00066 
00070 int
00071 WindowPosition::incrementSeqNum() {
00072     if (m_seqNum == WindowPosition::DONT_CARE) {
00073         return m_seqNum;
00074     }
00075         return m_seqNum = (m_seqNum + 1) % m_seqNumSpace;
00076 } // fn incrementSeqNum
00077 
00078 
00082 int
00083 WindowPosition::decrementSeqNum() {
00084     if (m_seqNum == WindowPosition::DONT_CARE) {
00085         return m_seqNum;
00086     }
00087         return m_seqNum = (m_seqNum + m_seqNumSpace-1) % m_seqNumSpace;
00088 } // fn decrementSeqNum
00089 
00090 
00094 WindowPosition 
00095 WindowPosition::operator+(int value)
00096 {
00097     if (m_seqNum == WindowPosition::DONT_CARE) {
00098         WindowPosition w(m_seqNumSpace, m_seqNum);
00099         return w;
00100     }
00101     WindowPosition w(m_seqNumSpace, (m_seqNum + value)%m_seqNumSpace);
00102     return w;
00103 } // fn operator+
00104 
00105 
00109 WindowPosition 
00110 WindowPosition::operator-(int value)
00111 {
00112     if (m_seqNum == WindowPosition::DONT_CARE) {
00113         WindowPosition w(m_seqNumSpace, m_seqNum);
00114         return w;
00115     }
00116     
00117     // (value % m_seqNumSpace) --> this takes care of looping the value around the array
00118     // pos + (m_seqNumSpace - (...)) to "subtract" the value
00119     // (...)%m_seqNumSpace to take care of when value == zero
00120     WindowPosition w(m_seqNumSpace, (m_seqNum+(m_seqNumSpace - (value % m_seqNumSpace)))%m_seqNumSpace);
00121     return w;
00122 } // fn operator-
00123 
00124 
00128 bool
00129 WindowPosition::operator!=(WindowPosition value) {
00130     if (m_seqNumSpace == value.m_seqNumSpace) {
00131         if (m_seqNum != value.m_seqNum) {
00132             return true;
00133         }
00134         else {
00135             return false;
00136         }
00137     }
00138     else {
00139         debug(DEBUG_WP, "Comparing WindowPositions of different size");
00140         return false;
00141     }
00142 } // fn operator!=
00143 
00144 
00148 bool
00149 WindowPosition::operator==(WindowPosition value) {
00150     return (!(*this != value));
00151 } // fn operator==
00152 
00153 
00162 bool
00163 WindowPosition::withinRange(WindowPosition start, WindowPosition end) {
00164     // if a window position has not been given a value yet, alway return true
00165     // because it doesnt matter
00166     if ((start.m_seqNum == WindowPosition::DONT_CARE) || (end.m_seqNum == WindowPosition::DONT_CARE)) {
00167         debug(DEBUG_WP, "Calling withinRange() with uninitialized value!!!");
00168         return true;
00169     }
00170 
00171     if (start.m_seqNum > end.m_seqNum) {
00172         // we are wrapping around
00173         if ((m_seqNum >= start.m_seqNum) || (m_seqNum <= end.m_seqNum)) {
00174             return true;
00175         }
00176     }
00177     else if (start.m_seqNum < end.m_seqNum) {
00178         // standard comparison
00179         if ((m_seqNum >= start.m_seqNum) && (m_seqNum <= end.m_seqNum)) {
00180             return true;
00181         }
00182     }
00183     else if (start.m_seqNum == end.m_seqNum) {
00184         // if start and end are the same
00185         if (m_seqNum == start.m_seqNum) {
00186             return true;
00187         }
00188     }
00189     return false;
00190 }
00191 
00192 
00196 int
00197 WindowPosition::distanceTo(WindowPosition end) {
00198     if ( (m_seqNum == WindowPosition::DONT_CARE) || (end.m_seqNum == WindowPosition::DONT_CARE) ) {
00199         debug(DEBUG_WP, "Calling distanceTo() with uninitialized value!!!");
00200         return m_seqNumSpace;
00201     }
00202     
00203     if (m_seqNum > end.m_seqNum) {
00204         return (m_seqNumSpace - m_seqNum) + end.m_seqNum;
00205     }
00206     else {
00207         return (end.m_seqNum - m_seqNum);
00208     }
00209 } // fn distanceTo
00210 
00211 
00215 int
00216 WindowPosition::getPosition() {
00217     return m_seqNum % m_windowSize;
00218 } // fn getPosition
00219 
00220 
00224 int
00225 WindowPosition::getSeqNum() {
00226     return m_seqNum;
00227 } // fn getSeqNum
00228 
00229 
00233 void 
00234 WindowPosition::toStream(std::ostream& s) {
00235     s << "(" <<  m_seqNumSpace << ", " << m_seqNum << ")";
00236 } // fn toStream
00237 

Generated at Thu Jul 11 13:31:53 2002 for Peekabooty by doxygen1.2.9 written by Dimitri van Heesch, © 1997-2001