libsemigroups
partition.h
1 //
2 // libsemigroups - C++ library for semigroups and monoids
3 // Copyright (C) 2017 Michael Torpey
4 //
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 //
18 
19 #ifndef LIBSEMIGROUPS_SRC_PARTITION_H_
20 #define LIBSEMIGROUPS_SRC_PARTITION_H_
21 
22 #include <algorithm>
23 #include <vector>
24 
25 #include "libsemigroups-debug.h"
26 
27 namespace libsemigroups {
33  template <typename T> class Partition {
34  // TODO: do something else instead of using this object?
35 
36  public:
40  explicit Partition(size_t nr_parts = 0)
41  : _parts(new std::vector<std::vector<T*>*>()) {
42  for (size_t i = 0; i < nr_parts; i++) {
43  _parts->push_back(new std::vector<T*>());
44  }
45  }
46 
51  // FIXME delete if not used
52  explicit Partition(std::vector<std::vector<T*>*>* parts) : _parts(parts) {}
53 
59  for (std::vector<T*>* block : *_parts) {
60  for (T* elm : *block) {
61  delete elm;
62  }
63  delete block;
64  }
65  delete _parts;
66  }
67 
70  Partition& operator=(Partition const& part) = delete;
71 
74  Partition(Partition const& copy) = delete;
75 
77  // FIXME rename this to nr_parts
78  inline size_t size() const {
79  return _parts->size();
80  }
81 
86  inline std::vector<T*>* operator[](size_t part_index) const {
87  LIBSEMIGROUPS_ASSERT(part_index < size());
88  return (*_parts)[part_index];
89  }
90 
95  inline std::vector<T*>* at(size_t part_index) const {
96  return _parts->at(part_index);
97  }
98 
100  T* at(size_t part_index, size_t elm_nr) const {
101  return _parts->at(part_index)->at(elm_nr);
102  }
103 
104  private:
105  std::vector<std::vector<T*>*>* _parts;
106  };
107 } // namespace libsemigroups
108 #endif // LIBSEMIGROUPS_SRC_PARTITION_H_
Partition(std::vector< std::vector< T * > * > *parts)
A constructor.
Definition: partition.h:52
std::vector< T * > * operator[](size_t part_index) const
Returns the part with index part_index.
Definition: partition.h:86
std::vector< T * > * at(size_t part_index) const
Returns the part with index part_index.
Definition: partition.h:95
~Partition()
A default destructor.
Definition: partition.h:58
Partition(size_t nr_parts=0)
A constructor.
Definition: partition.h:40
Class for partitions of a set used by Congruence::nontrivial_classes.
Definition: partition.h:33
T * at(size_t part_index, size_t elm_nr) const
Returns the element with index elm_nr in part part_index.
Definition: partition.h:100
Namespace for everything in the libsemigroups library.
Definition: blocks.cc:32
Partition & operator=(Partition const &part)=delete
The assignment operator is deleted for Partition to avoid unintended copying.
size_t size() const
Returns the number of parts in the partition.
Definition: partition.h:78