00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef OMC_COMBINATION_H
00020 #define OMC_COMBINATION_H
00021
00022 #include "Core.h"
00023 #include <algorithm>
00024 #include <iostream>
00025 #include <string>
00026 #include <vector>
00027
00028 namespace omc
00029 {
00030 class Combination
00031 {
00032 friend std::ostream & operator<< (std::ostream &, const Combination &);
00033
00034 public:
00035 Combination (size_t);
00036 Combination (const std::string &);
00037
00038
00039
00040 size_t size () const;
00041
00042 bool next ();
00043
00044 bool has (index_t) const;
00045
00046 bool operator== (const Combination &) const;
00047
00048 void add (index_t);
00049
00050 protected:
00051 size_t m_numElements;
00052
00053 unsigned long m_Bit;
00054 };
00055
00056
00057 std::ostream & operator<< (std::ostream &, const Combination &);
00058
00059
00061 inline Combination::Combination (size_t n)
00062 : m_numElements (n), m_Bit (0)
00063 {}
00064
00067 inline Combination::Combination (const std::string & s)
00068 : m_numElements (s.size()), m_Bit (0)
00069 {
00070 for (int i=0; i<m_numElements; i++)
00071 if (s[i] == '1')
00072 m_Bit += (1<<i);
00073 }
00074
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00088 inline size_t Combination::size () const {
00089 return m_numElements;
00090 }
00091
00093 inline bool Combination::next () {
00094 if (m_Bit == (unsigned long) (1 << m_numElements) - 1)
00095 return false;
00096 else {
00097 m_Bit++;
00098 return true;
00099 }
00100 }
00101
00103 inline bool Combination::has (index_t i) const {
00104 return (m_Bit & (1<<i));
00105 }
00106
00108 inline bool Combination::operator== (const Combination & comb) const {
00109 return m_numElements == comb.m_numElements && m_Bit == comb.m_Bit;
00110 }
00111
00113 inline void Combination::add (index_t i) {
00114 m_Bit |= (1<<i);
00115 }
00116
00117
00119 inline std::ostream & operator<< (std::ostream & os,
00120 const Combination & comb)
00121 {
00122 bool first = true;
00123
00124 os << "{";
00125 for (index_t i=0; i<comb.m_numElements; i++)
00126 if (comb.has (i)) {
00127 if (!first) os << ",";
00128 else first = false;
00129 os << i;
00130 }
00131 os << "}";
00132 return os;
00133 }
00134
00135 }
00136
00137 #endif // OMC_COMBINATION_H