00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef OMC_PERMUTATION_H
00017 #define OMC_PERMUTATION_H
00018
00019 #include "Core.h"
00020 #include <vector>
00021 #include <algorithm>
00022 #include <iostream>
00023
00024 namespace omc
00025 {
00026 class Permutation
00027 {
00028 friend std::ostream & operator<< (std::ostream &, const Permutation &);
00029
00030 public:
00031 Permutation (size_t);
00032 Permutation (std::vector<index_t> &);
00033
00034 index_t & operator[] (index_t);
00035 index_t operator[] (index_t) const;
00036
00037 index_t operator() (index_t) const;
00038
00039 bool next ();
00040
00041 protected:
00042 std::vector <index_t> m_Perm;
00043 };
00044
00045
00046 std::ostream & operator<< (std::ostream &, const Permutation &);
00047
00049 inline Permutation::Permutation (size_t n) {
00050 for (index_t i=0; i<n; i++)
00051 m_Perm.push_back (i);
00052 }
00053
00055 inline Permutation::Permutation (std::vector<index_t> & Perm)
00056 : m_Perm (Perm)
00057 {}
00058
00060 inline index_t & Permutation::operator[] (index_t i) {
00061 return m_Perm[i];
00062 }
00063
00065 inline index_t Permutation::operator[] (index_t i) const {
00066 return m_Perm[i];
00067 }
00068
00070 inline index_t Permutation::operator() (index_t i) const {
00071 if (i >= m_Perm.size())
00072 throw OMCException ("Index out of bound!", "Permutation::operator()");
00073 return m_Perm[i];
00074 }
00075
00077 inline bool Permutation::next () {
00078 return std::next_permutation (m_Perm.begin(), m_Perm.end());
00079 }
00080
00081
00083 inline std::ostream & operator<< (std::ostream & os,
00084 const Permutation & pi)
00085 {
00086 for (index_t i=0; i<pi.m_Perm.size(); i++)
00087 os << pi.m_Perm[i];
00088 return os;
00089 }
00090
00091 }
00092
00093 #endif // OMC_PERMUTATION_H