00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifndef PERMUTATION_H_
00034 #define PERMUTATION_H_
00035
00036 #include <permlib/common.h>
00037
00038
00039 #include <string>
00040 #include <iostream>
00041 #include <boost/tokenizer.hpp>
00042 #include <sstream>
00043 #include <set>
00044
00045 #include <boost/shared_ptr.hpp>
00046
00047 namespace permlib {
00048
00050 class Permutation {
00051 public:
00053 typedef std::vector<unsigned long> perm;
00054
00056 explicit Permutation(unsigned int n);
00058 Permutation(unsigned int n, const std::string &cycles);
00060 explicit Permutation(const perm &p);
00062 Permutation(const Permutation &p) : m_perm(p.m_perm), m_isIdentity(p.m_isIdentity) {};
00063
00065 Permutation operator*(const Permutation &p) const;
00067
00070 Permutation& operator*=(const Permutation &p);
00072
00075 Permutation& operator^=(const Permutation &p);
00077 Permutation operator~() const;
00079 Permutation& invertInplace();
00081 bool operator==(const Permutation &p2) const { return m_perm == p2.m_perm; };
00082
00084 inline ulong operator/(ulong val) const { return at(val); }
00086 inline ulong at(ulong val) const { return m_perm[val]; }
00087
00089 ulong operator%(ulong val) const;
00090
00092 friend std::ostream &operator<< (std::ostream &out, const Permutation &p);
00093
00095
00098 bool isIdentity() const;
00100 inline void flush() {};
00102 inline uint size() const { return m_perm.size(); }
00103
00105 void setTransposition(uint pos, uint val);
00106 protected:
00108 perm m_perm;
00109
00111 bool m_isIdentity;
00112
00114 Permutation(unsigned int n, bool) : m_perm(n), m_isIdentity(false) {}
00115 };
00116
00117
00118
00119
00120
00121
00122 inline Permutation::Permutation(unsigned int n)
00123 : m_perm(n), m_isIdentity(true)
00124 {
00125 for (unsigned int i=0; i<n; ++i)
00126 m_perm[i] = i;
00127 }
00128
00129 inline Permutation::Permutation(unsigned int n, const std::string & cycles)
00130 : m_perm(n), m_isIdentity(false)
00131 {
00132 typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
00133 boost::char_separator<char> sepCycles(",");
00134 tokenizer tokens(cycles, sepCycles);
00135
00136 for (unsigned int i=0; i<n; ++i)
00137 m_perm[i] = i;
00138
00139 for (tokenizer::iterator tok_iter = tokens.begin(); tok_iter != tokens.end(); ++tok_iter) {
00140 std::stringstream ss(*tok_iter);
00141
00142 ulong first, last, temp;
00143 ss >> first;
00144 last = first;
00145
00146 while (!ss.eof()) {
00147 ss >> temp;
00148 m_perm[last-1] = temp-1;
00149 last = temp;
00150 }
00151 m_perm[last-1] = first-1;
00152 }
00153 }
00154
00155
00156 inline Permutation::Permutation(const perm& p)
00157 : m_perm(p), m_isIdentity(false)
00158 { }
00159
00160 inline Permutation Permutation::operator*(const Permutation &p) const {
00161 BOOST_ASSERT(p.m_perm.size() == m_perm.size());
00162
00163 Permutation res(m_perm.size(), true);
00164 for (unsigned int i=0; i<m_perm.size(); ++i) {
00165 res.m_perm[i] = p.m_perm[m_perm[i]];
00166 }
00167 return res;
00168 }
00169
00170 inline Permutation& Permutation::operator*=(const Permutation &p) {
00171 BOOST_ASSERT(p.m_perm.size() == m_perm.size());
00172 m_isIdentity = false;
00173
00174 for (unsigned int i=0; i<m_perm.size(); ++i) {
00175 m_perm[i] = p.m_perm[m_perm[i]];
00176 }
00177 return *this;
00178 }
00179
00180 inline Permutation& Permutation::operator^=(const Permutation &p) {
00181 BOOST_ASSERT(p.m_perm.size() == m_perm.size());
00182 m_isIdentity = false;
00183 perm tmp(m_perm);
00184
00185 for (unsigned int i=0; i<m_perm.size(); ++i) {
00186 m_perm[i] = tmp[p.m_perm[i]];
00187 }
00188 return *this;
00189 }
00190
00191 inline Permutation Permutation::operator~() const {
00192 Permutation res(m_perm.size(), true);
00193 for (unsigned int i=0; i<m_perm.size(); ++i) {
00194 res.m_perm[m_perm[i]] = i;
00195 }
00196 return res;
00197 }
00198
00199 inline Permutation& Permutation::invertInplace() {
00200 perm tmp(m_perm);
00201 for (unsigned int i=0; i<m_perm.size(); ++i) {
00202 m_perm[tmp[i]] = i;
00203 }
00204 return *this;
00205 }
00206
00207 inline ulong Permutation::operator%(ulong val) const {
00208 for (uint i = 0; i < m_perm.size(); ++i) {
00209 if (m_perm[i] == val)
00210 return i;
00211 }
00212
00213 BOOST_ASSERT(false);
00214 return -1;
00215 }
00216
00217 inline bool Permutation::isIdentity() const {
00218 if (m_isIdentity)
00219 return true;
00220 for (unsigned int i=0; i<m_perm.size(); ++i)
00221 if (at(i) != i)
00222 return false;
00223 return true;
00224 }
00225
00226 inline void Permutation::setTransposition(uint pos, uint val) {
00227 BOOST_ASSERT(pos < m_perm.size());
00228 BOOST_ASSERT(val < m_perm.size());
00229
00230 m_perm[pos] = val;
00231 m_perm[val] = pos;
00232 }
00233
00234 inline std::ostream& operator<<(std::ostream& out, const Permutation& p) {
00235 std::set<ulong> worked;
00236 bool output = false;
00237 for (ulong x=0; x<p.m_perm.size(); ++x) {
00238 unsigned long px, startX;
00239 startX = x;
00240 px = p.m_perm[x];
00241 if (worked.count(x) || x == px) {
00242 continue;
00243 }
00244
00245 worked.insert(x);
00246 out << "(" << (x+1) << ",";
00247 while (p.m_perm[px] != startX) {
00248 out << (px+1) << ",";
00249 worked.insert(px);
00250 px = p.m_perm[px];
00251 }
00252 worked.insert(px);
00253 out << (px+1);
00254 out << ")";
00255 output = true;
00256 }
00257 if (!output)
00258 out << "()";
00259 return out;
00260 }
00261
00262 }
00263
00264 #endif // -- PERMUTATION_H_