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 "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
00104 void setTransposition(uint pos, uint val);
00105 protected:
00107 perm m_perm;
00108
00110 bool m_isIdentity;
00111
00113 Permutation(unsigned int n, bool) : m_perm(n), m_isIdentity(false) {}
00114 };
00115
00116
00117
00118
00119
00120
00121 inline Permutation::Permutation(unsigned int n)
00122 : m_perm(n), m_isIdentity(true)
00123 {
00124 for (unsigned int i=0; i<n; ++i)
00125 m_perm[i] = i;
00126 }
00127
00128 inline Permutation::Permutation(unsigned int n, const std::string & cycles)
00129 : m_perm(n), m_isIdentity(false)
00130 {
00131 typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
00132 boost::char_separator<char> sepCycles(",");
00133 tokenizer tokens(cycles, sepCycles);
00134
00135 for (unsigned int i=0; i<n; ++i)
00136 m_perm[i] = i;
00137
00138 for (tokenizer::iterator tok_iter = tokens.begin(); tok_iter != tokens.end(); ++tok_iter) {
00139 std::stringstream ss(*tok_iter);
00140
00141 ulong first, last, temp;
00142 ss >> first;
00143 last = first;
00144
00145 while (!ss.eof()) {
00146 ss >> temp;
00147 m_perm[last-1] = temp-1;
00148 last = temp;
00149 }
00150 m_perm[last-1] = first-1;
00151 }
00152 }
00153
00154
00155 inline Permutation::Permutation(const perm& p)
00156 : m_perm(p), m_isIdentity(false)
00157 { }
00158
00159 inline Permutation Permutation::operator*(const Permutation &p) const {
00160 BOOST_ASSERT(p.m_perm.size() == m_perm.size());
00161
00162 Permutation res(m_perm.size(), true);
00163 for (unsigned int i=0; i<m_perm.size(); ++i) {
00164 res.m_perm[i] = p.m_perm[m_perm[i]];
00165 }
00166 return res;
00167 }
00168
00169 inline Permutation& Permutation::operator*=(const Permutation &p) {
00170 BOOST_ASSERT(p.m_perm.size() == m_perm.size());
00171 m_isIdentity = false;
00172
00173 for (unsigned int i=0; i<m_perm.size(); ++i) {
00174 m_perm[i] = p.m_perm[m_perm[i]];
00175 }
00176 return *this;
00177 }
00178
00179 inline Permutation& Permutation::operator^=(const Permutation &p) {
00180 BOOST_ASSERT(p.m_perm.size() == m_perm.size());
00181 m_isIdentity = false;
00182 perm tmp(m_perm);
00183
00184 for (unsigned int i=0; i<m_perm.size(); ++i) {
00185 m_perm[i] = tmp[p.m_perm[i]];
00186 }
00187 return *this;
00188 }
00189
00190 inline Permutation Permutation::operator~() const {
00191 Permutation res(m_perm.size(), true);
00192 for (unsigned int i=0; i<m_perm.size(); ++i) {
00193 res.m_perm[m_perm[i]] = i;
00194 }
00195 return res;
00196 }
00197
00198 inline Permutation& Permutation::invertInplace() {
00199 perm tmp(m_perm);
00200 for (unsigned int i=0; i<m_perm.size(); ++i) {
00201 m_perm[tmp[i]] = i;
00202 }
00203 return *this;
00204 }
00205
00206 inline ulong Permutation::operator%(ulong val) const {
00207 for (uint i = 0; i < m_perm.size(); ++i) {
00208 if (m_perm[i] == val)
00209 return i;
00210 }
00211
00212 BOOST_ASSERT(false);
00213 return -1;
00214 }
00215
00216 inline bool Permutation::isIdentity() const {
00217 if (m_isIdentity)
00218 return true;
00219 for (unsigned int i=0; i<m_perm.size(); ++i)
00220 if (at(i) != i)
00221 return false;
00222 return true;
00223 }
00224
00225 inline void Permutation::setTransposition(uint pos, uint val) {
00226 BOOST_ASSERT(pos < m_perm.size());
00227 BOOST_ASSERT(val < m_perm.size());
00228
00229 m_perm[pos] = val;
00230 m_perm[val] = pos;
00231 }
00232
00233 inline std::ostream& operator<<(std::ostream& out, const Permutation& p) {
00234 std::set<ulong> worked;
00235 bool output = false;
00236 for (ulong x=0; x<p.m_perm.size(); ++x) {
00237 unsigned long px, startX;
00238 startX = x;
00239 px = p.m_perm[x];
00240 if (worked.count(x) || x == px) {
00241 continue;
00242 }
00243
00244 worked.insert(x);
00245 out << "(" << (x+1) << ",";
00246 while (p.m_perm[px] != startX) {
00247 out << (px+1) << ",";
00248 worked.insert(px);
00249 px = p.m_perm[px];
00250 }
00251 worked.insert(px);
00252 out << (px+1);
00253 out << ")";
00254 output = true;
00255 }
00256 if (!output)
00257 out << "()";
00258 return out;
00259 }
00260
00261 }
00262
00263 #endif // -- PERMUTATION_H_