CASM  1.1.0
A Clusters Approach to Statistical Mechanics
GenericCluster.hh
Go to the documentation of this file.
1 #ifndef CASM_GenericCluster
2 #define CASM_GenericCluster
3 
4 #include <algorithm>
5 #include <vector>
6 
10 
11 namespace CASM {
12 
18 /* -- GenericCluster Declarations ------------------------------------- */
19 
46 template <typename Base>
47 class GenericCluster : public Comparisons<Base> {
48  public:
49  typedef typename Base::MostDerived MostDerived;
50  using Base::derived;
51 
53  typedef Index size_type;
54 
55  typedef typename std::vector<Element>::value_type value_type;
56  typedef typename std::vector<Element>::iterator iterator;
57  typedef typename std::vector<Element>::const_iterator const_iterator;
58 
60  iterator begin() { return derived().elements().begin(); }
61 
63  const_iterator begin() const { return derived().elements().begin(); }
64 
66  iterator end() { return derived().elements().end(); }
67 
69  const_iterator end() const { return derived().elements().end(); }
70 
72  const_iterator cbegin() const { return derived().elements().cbegin(); }
73 
75  const_iterator cend() const { return derived().elements().cend(); }
76 
78  size_type size() const { return derived().elements().size(); }
79 
81  value_type &operator[](size_type index) { return derived().element(index); }
82 
84  value_type const &operator[](size_type index) const {
85  return derived().element(index);
86  }
87 
89  value_type &element(size_type index) { return derived().elements()[index]; }
90 
92  value_type const &element(size_type index) const {
93  return derived().elements()[index];
94  }
95 
96  MostDerived &sort() { return derived().sort_impl(); }
97 
98  MostDerived sorted() const {
99  MostDerived tmp{derived()};
100  return tmp.sort();
101  }
102 
103  bool is_sorted() const { return derived().is_sorted_impl(); }
104 
106  return derived().sort_permutation_impl();
107  }
108 
109  bool operator<(MostDerived const &B) const {
110  return derived().compare_impl(B);
111  }
112 
113  protected:
116 
118  std::sort(begin(), end());
119  return derived();
120  }
121 
122  bool is_sorted_impl() const { return std::is_sorted(begin(), end()); }
123 
125  if (size() == 0) return Permutation(0);
126  std::vector<Index> ind = sequence(Index(0), Index(size() - 1));
127 
128  std::sort(ind.begin(), ind.end(), [&](const Index &a, const Index &b) {
129  return (this->derived().element(a) < this->derived().element(b));
130  });
131 
132  return Permutation(std::move(ind));
133  }
134 
135  bool compare_impl(MostDerived const &B) const {
136  if (size() != B.size()) {
137  return size() < B.size();
138  }
139  return lexicographical_compare(begin(), end(), B.begin(), B.end());
140  }
141 };
142 } // namespace CASM
143 
144 #endif
A CRTP base class for a cluster of anything.
value_type const & element(size_type index) const
Access a UnitCellCoord in the cluster by index.
GenericCluster()
Construct an empty GenericCluster.
value_type & element(size_type index)
Access an element in the cluster by index.
std::vector< Element >::const_iterator const_iterator
MostDerived & sort_impl()
const_iterator begin() const
Iterator to first element in the cluster.
bool is_sorted_impl() const
iterator end()
Iterator to the past-the-last element in the cluster.
bool operator<(MostDerived const &B) const
value_type & operator[](size_type index)
Access an element in the cluster by index.
const_iterator cbegin() const
Iterator to first element in the cluster.
const_iterator cend() const
Iterator to the past-the-last element in the cluster.
bool compare_impl(MostDerived const &B) const
traits< MostDerived >::Element Element
iterator begin()
Iterator to first element in the cluster.
MostDerived & sort()
size_type size() const
Number of elements in the cluster.
const_iterator end() const
Iterator to the past-the-last element in the cluster.
Base::MostDerived MostDerived
Permutation sort_permutation() const
std::vector< Element >::value_type value_type
std::vector< Element >::iterator iterator
Permutation sort_permutation_impl() const
value_type const & operator[](size_type index) const
Access an element in the cluster by index.
MostDerived sorted() const
Main CASM namespace.
Definition: APICommand.hh:8
std::vector< T > sequence(T first, T last)
Definition: algorithm.hh:9
INDEX_TYPE Index
For long integer indexing:
Definition: definitions.hh:39
Implements other comparisons in terms of '<'.
Definition: Comparisons.hh:25