CASM  1.1.0
A Clusters Approach to Statistical Mechanics
Permutation.hh
Go to the documentation of this file.
1 // Permutation.hh
2 
3 #ifndef PERMUTATION_HH
4 #define PERMUTATION_HH
5 
6 #include <cassert>
7 #include <numeric>
8 #include <ostream>
9 #include <vector>
10 
12 
13 namespace CASM {
14 
39 
40 class Permutation {
41  private:
46  std::vector<Index> m_perm_array;
47 
48  public:
49  explicit Permutation(Index N) : m_perm_array(N) {
50  std::iota(m_perm_array.begin(), m_perm_array.end(), 0);
51  }
52 
53  template <typename Iterator>
54  Permutation(Iterator begin, Iterator end) : m_perm_array(begin, end) {}
55  explicit Permutation(const std::vector<Index> &init_perm)
56  : m_perm_array(init_perm) {}
57  explicit Permutation(std::vector<Index> &&init_perm)
58  : m_perm_array(std::move(init_perm)) {}
59 
60  Index size() const { return m_perm_array.size(); }
61 
62  const std::vector<Index> &perm_array() const { return m_perm_array; }
63 
66  bool is_perm() const;
67 
69  bool has_fixed_points() const;
70 
72  Index character() const;
73 
75  bool is_identity() const;
76 
78  void append_fixed_points(Index N_new);
79 
81  Permutation inverse() const;
82 
87  const std::vector<Index> &block_dims) const;
88 
91  Permutation transformed_by(const Permutation &trans_perm) const;
92 
94  const Index &operator[](Index i) const { return m_perm_array[i]; }
95 
97  Index &set(Index i) { return m_perm_array[i]; }
98 
100  template <typename Container>
101  Container permute(const Container &_before) const;
102 
104  template <typename Container>
105  Container ipermute(const Container &_before) const;
106 
107  template <typename Container>
108  Container operator*(const Container &_before) const {
109  return this->permute(_before);
110  }
111 
112  Permutation operator*(const Permutation &RHS) const;
113 };
114 
115 std::ostream &operator<<(std::ostream &, const Permutation &);
116 
117 //************************************************************************************************************************************//
118 //************************************************************************************************************************************//
119 
123 template <typename Container>
124 Container Permutation::permute(const Container &before_vec) const {
125  assert(before_vec.size() == size() &&
126  "WARNING: You're trying to permute a Container with an incompatible "
127  "permutation!");
128 
129  Container after_vec(before_vec);
130  // after_vec.reserve(size());
131 
132  for (Index i = 0; i < size(); i++) {
133  after_vec[i] = before_vec[m_perm_array[i]];
134  }
135  return after_vec;
136 }
137 
138 //**************************************************************
139 
143 template <typename Container>
144 Container Permutation::ipermute(const Container &before_array) const {
145  assert(before_array.size() == size() &&
146  "WARNING: You're trying to permute an Container with an incompatible "
147  "permutation!");
148 
149  Container after_array(before_array);
150  // after_array.reserve(size());
151 
152  for (Index i = 0; i < size(); i++) {
153  if (i != m_perm_array[i]) {
154  after_array[m_perm_array[i]] = before_array[i];
155  }
156  }
157  return after_array;
158 }
159 
160 } // namespace CASM
161 
162 #endif
Permutation inverse() const
Construct permutation that undoes the permutation performed by 'this'.
Definition: Permutation.cc:111
bool is_perm() const
Definition: Permutation.cc:57
Container permute(const Container &_before) const
Generate permuted copy of indexed container.
Definition: Permutation.hh:124
bool has_fixed_points() const
Checks whether any indices remain unchanged by permutation.
Definition: Permutation.cc:79
Container ipermute(const Container &_before) const
Generate inversely permuted copy of type-T std::vector.
Definition: Permutation.hh:144
Permutation(const std::vector< Index > &init_perm)
Definition: Permutation.hh:55
Index character() const
Character of permutation is number of fixed points.
Definition: Permutation.cc:68
bool is_identity() const
Checks whether permutation is identity (i.e., m_perm_aray[i]==i for all i)
Definition: Permutation.cc:89
Permutation transformed_by(const Permutation &trans_perm) const
Definition: Permutation.cc:149
Permutation(Index N)
Definition: Permutation.hh:49
std::vector< Index > m_perm_array
Definition: Permutation.hh:46
Permutation make_block_permutation(const std::vector< Index > &block_dims) const
Definition: Permutation.cc:123
void append_fixed_points(Index N_new)
Add new indices that remain unchanged by permutation.
Definition: Permutation.cc:98
Index size() const
Definition: Permutation.hh:60
const Index & operator[](Index i) const
const access of m_perm_array for doing low-level permutation algebra
Definition: Permutation.hh:94
Container operator*(const Container &_before) const
Definition: Permutation.hh:108
Permutation(std::vector< Index > &&init_perm)
Definition: Permutation.hh:57
const std::vector< Index > & perm_array() const
Definition: Permutation.hh:62
Index & set(Index i)
non-const access of m_perm_array for doing low-level permutation algebra
Definition: Permutation.hh:97
Permutation(Iterator begin, Iterator end)
Definition: Permutation.hh:54
Main CASM namespace.
Definition: APICommand.hh:8
std::ostream & operator<<(std::ostream &_stream, const FormattedPrintable &_formatted)
INDEX_TYPE Index
For long integer indexing:
Definition: definitions.hh:39
Definition: stream_io.hh:24