Main   Namespaces   Classes   Hierarchy   Annotated   Files   Compound   Global   Pages  

SortedSequence.hpp

Go to the documentation of this file.
00001 //----------------------------------------------------------------------------
00002 /** @file
00003  */
00004 //----------------------------------------------------------------------------
00005 
00006 #ifndef SORTEDSEQUENCE_H
00007 #define SORTEDSEQUENCE_H
00008 
00009 #include <vector>
00010 #include <iostream>
00011 #include "Benzene.hpp"
00012 
00013 _BEGIN_BENZENE_NAMESPACE_
00014 
00015 //----------------------------------------------------------------------------
00016 
00017 /** 
00018     
00019 */
00020 class SortedSequence
00021 {
00022 public:
00023 
00024     /** Creates an empty sorted sequence. */
00025     SortedSequence();
00026 
00027     /** Creates a seq with max values max, and num indices. */
00028     SortedSequence(int max, int num);
00029 
00030     /** Creates a sequence with max value max and current indices seq; 
00031         seq must be strictly increasing. */
00032     SortedSequence(int max, const std::vector<int>& seq);
00033 
00034     /** Returns true if no more valid sequences. */
00035     bool finished() const;
00036 
00037     /** Returns a reference to the nth index. 
00038         If these value is changed to make the sequence not strictly
00039         increasing, further output is undefined. */
00040     int& operator[](int n);
00041 
00042     /** Updates indices to next valid sorted sequence. */
00043     void operator++();
00044 
00045     /** Returns the indices as a vector of integers. */
00046     std::vector<int>& indices();
00047 
00048 private:
00049     int m_max;
00050     std::vector<int> m_seq;
00051 };
00052 
00053 
00054 inline SortedSequence::SortedSequence()
00055     : m_max(0),
00056       m_seq(1, 1)
00057 { }
00058 
00059 inline SortedSequence::SortedSequence(int max, int num)
00060     : m_max(max),
00061       m_seq(num, 0)
00062 {
00063     // handle the case where num == 0 explicitly
00064     if (num == 0) {
00065         m_max = 1;
00066         m_seq = std::vector<int>(1, 0);
00067     }
00068     
00069     for (int i=0; i<(int)m_seq.size(); ++i) 
00070         m_seq[i] = i;
00071 }
00072 
00073 inline SortedSequence::SortedSequence(int max, const std::vector<int>& seq)
00074     : m_max(max),
00075       m_seq(seq)
00076 { }
00077 
00078 inline bool SortedSequence::finished() const
00079 {
00080     return (m_seq[0] > m_max-(int)m_seq.size());
00081 }
00082 
00083 inline int& SortedSequence::operator[](int n)
00084 {
00085     assert(0 <= n && n < (int)m_seq.size());
00086     return m_seq[n];
00087 }
00088 
00089 inline std::vector<int>& SortedSequence::indices()
00090 {
00091     return m_seq;
00092 }
00093 
00094 inline void SortedSequence::operator++()
00095 {
00096     unsigned i = m_seq.size()-1;
00097     int off = 0;
00098     while (true) {
00099         m_seq[i]++;
00100 
00101         if (m_seq[i] < m_max-off)
00102             break;
00103 
00104         if (i == 0) 
00105             break;
00106 
00107         i--;
00108         off++;
00109     }
00110 
00111     for (; i<m_seq.size()-1; ++i)
00112         m_seq[i+1] = m_seq[i]+1;
00113 }
00114 
00115 //----------------------------------------------------------------------------
00116 
00117 _END_BENZENE_NAMESPACE_
00118 
00119 #endif // SORTEDSEQUENCE_H


6 Jan 2011 Doxygen 1.6.3