Skip to content

Commit

Permalink
Remove names starting with underscore
Browse files Browse the repository at this point in the history
  • Loading branch information
james-d-mitchell committed Oct 29, 2023
1 parent 97777b8 commit b9375fd
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 84 deletions.
8 changes: 4 additions & 4 deletions include/perm_generic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@

namespace HPCombi {

template <size_t _Size, typename Expo = uint8_t>
struct PermGeneric : public VectGeneric<_Size, Expo> {
using vect = VectGeneric<_Size, Expo>;
template <size_t Size, typename Expo = uint8_t>
struct PermGeneric : public VectGeneric<Size, Expo> {
using vect = VectGeneric<Size, Expo>;

static constexpr size_t size() { return _Size; }
static constexpr size_t size() { return Size; }

PermGeneric() = default;
PermGeneric(const vect v) : vect(v) {} // NOLINT
Expand Down
73 changes: 36 additions & 37 deletions include/perm_generic_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,34 @@

namespace HPCombi {

template <size_t _Size, typename Expo>
PermGeneric<_Size, Expo>::PermGeneric(std::initializer_list<Expo> il) {
assert(il.size() <= _Size);
template <size_t Size, typename Expo>
PermGeneric<Size, Expo>::PermGeneric(std::initializer_list<Expo> il) {
assert(il.size() <= Size);
std::copy(il.begin(), il.end(), this->v.begin());
for (Expo i = il.size(); i < _Size; i++)
for (Expo i = il.size(); i < Size; i++)
this->v[i] = i;
}

template <size_t _Size, typename Expo>
PermGeneric<_Size, Expo>
PermGeneric<_Size, Expo>::elementary_transposition(uint64_t i) {
assert(i < _Size);
template <size_t Size, typename Expo>
PermGeneric<Size, Expo>
PermGeneric<Size, Expo>::elementary_transposition(uint64_t i) {
assert(i < Size);
PermGeneric res{{}};
res[i] = i + 1;
res[i + 1] = i;
return res;
}

template <size_t _Size, typename Expo>
PermGeneric<_Size, Expo> PermGeneric<_Size, Expo>::inverse() const {
template <size_t Size, typename Expo>
PermGeneric<Size, Expo> PermGeneric<Size, Expo>::inverse() const {
PermGeneric res;
for (uint64_t i = 0; i < _Size; i++)
for (uint64_t i = 0; i < Size; i++)
res[this->v[i]] = i;
return res;
}

template <size_t _Size, typename Expo>
PermGeneric<_Size, Expo> PermGeneric<_Size, Expo>::random() {
template <size_t Size, typename Expo>
PermGeneric<Size, Expo> PermGeneric<Size, Expo>::random() {
static std::random_device rd;
static std::mt19937 g(rd());

Expand All @@ -51,41 +51,40 @@ PermGeneric<_Size, Expo> PermGeneric<_Size, Expo>::random() {
return res;
}

template <size_t _Size, typename Expo>
typename PermGeneric<_Size, Expo>::vect
PermGeneric<_Size, Expo>::lehmer() const {
template <size_t Size, typename Expo>
typename PermGeneric<Size, Expo>::vect PermGeneric<Size, Expo>::lehmer() const {
vect res{};
for (size_t i = 0; i < _Size; i++)
for (size_t j = i + 1; j < _Size; j++)
for (size_t i = 0; i < Size; i++)
for (size_t j = i + 1; j < Size; j++)
if (this->v[i] > this->v[j])
res[i]++;
return res;
}

template <size_t _Size, typename Expo>
uint64_t PermGeneric<_Size, Expo>::length() const {
template <size_t Size, typename Expo>
uint64_t PermGeneric<Size, Expo>::length() const {
uint64_t res = 0;
for (size_t i = 0; i < _Size; i++)
for (size_t j = i + 1; j < _Size; j++)
for (size_t i = 0; i < Size; i++)
for (size_t j = i + 1; j < Size; j++)
if (this->v[i] > this->v[j])
res++;
return res;
}

template <size_t _Size, typename Expo>
uint64_t PermGeneric<_Size, Expo>::nb_descents() const {
template <size_t Size, typename Expo>
uint64_t PermGeneric<Size, Expo>::nb_descents() const {
uint64_t res = 0;
for (size_t i = 0; i < _Size - 1; i++)
for (size_t i = 0; i < Size - 1; i++)
if (this->v[i] > this->v[i + 1])
res++;
return res;
}

template <size_t _Size, typename Expo>
uint64_t PermGeneric<_Size, Expo>::nb_cycles() const {
std::array<bool, _Size> b{};
template <size_t Size, typename Expo>
uint64_t PermGeneric<Size, Expo>::nb_cycles() const {
std::array<bool, Size> b{};
uint64_t c = 0;
for (size_t i = 0; i < _Size; i++) {
for (size_t i = 0; i < Size; i++) {
if (!b[i]) {
for (size_t j = i; !b[j]; j = this->v[j])
b[j] = true;
Expand All @@ -95,10 +94,10 @@ uint64_t PermGeneric<_Size, Expo>::nb_cycles() const {
return c;
}

template <size_t _Size, typename Expo>
bool PermGeneric<_Size, Expo>::left_weak_leq(PermGeneric other) const {
for (size_t i = 0; i < _Size; i++) {
for (size_t j = i + 1; j < _Size; j++) {
template <size_t Size, typename Expo>
bool PermGeneric<Size, Expo>::left_weak_leq(PermGeneric other) const {
for (size_t i = 0; i < Size; i++) {
for (size_t j = i + 1; j < Size; j++) {
if ((this->v[i] > this->v[j]) && (other[i] < other[j]))
return false;
}
Expand All @@ -110,10 +109,10 @@ bool PermGeneric<_Size, Expo>::left_weak_leq(PermGeneric other) const {

namespace std {

template <size_t _Size, typename Expo>
struct hash<HPCombi::PermGeneric<_Size, Expo>> {
size_t operator()(const HPCombi::PermGeneric<_Size, Expo> &ar) const {
return hash<HPCombi::VectGeneric<_Size, Expo>>()(ar);
template <size_t Size, typename Expo>
struct hash<HPCombi::PermGeneric<Size, Expo>> {
size_t operator()(const HPCombi::PermGeneric<Size, Expo> &ar) const {
return hash<HPCombi::VectGeneric<Size, Expo>>()(ar);
}
};

Expand Down
86 changes: 43 additions & 43 deletions include/vect_generic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ std::array<Expo, Size> sorted_vect(std::array<Expo, Size> v) {

/** A generic class for combinatorial integer vectors.
*/
template <size_t _Size, typename Expo = uint8_t> struct VectGeneric {
static constexpr size_t size() { return _Size; }
using array = std::array<Expo, _Size>;
template <size_t Size, typename Expo = uint8_t> struct VectGeneric {
static constexpr size_t size() { return Size; }
using array = std::array<Expo, Size>;
array v;

VectGeneric() = default;
VectGeneric(const std::array<Expo, _Size> &_v) : v(_v) {} // NOLINT
VectGeneric(const std::array<Expo, Size> &_v) : v(_v) {} // NOLINT
VectGeneric(std::initializer_list<Expo> il, Expo def = 0) {
assert(il.size() <= _Size);
assert(il.size() <= Size);
std::copy(il.begin(), il.end(), v.begin());
std::fill(v.begin() + il.size(), v.end(), def);
}
Expand All @@ -56,20 +56,20 @@ template <size_t _Size, typename Expo = uint8_t> struct VectGeneric {
Expo operator[](uint64_t i) const { return v[i]; }
Expo &operator[](uint64_t i) { return v[i]; }

size_t first_diff(const VectGeneric &u, size_t bound = _Size) const {
size_t first_diff(const VectGeneric &u, size_t bound = Size) const {
for (size_t i = 0; i < bound; i++)
if (v[i] != u[i])
return i;
return _Size;
return Size;
}

size_t last_diff(const VectGeneric &u, size_t bound = _Size) const {
size_t last_diff(const VectGeneric &u, size_t bound = Size) const {
while (bound != 0) {
--bound;
if (u[bound] != v[bound])
return bound;
}
return _Size;
return Size;
}

using value_type = Expo;
Expand All @@ -81,33 +81,33 @@ template <size_t _Size, typename Expo = uint8_t> struct VectGeneric {
const_iterator end() const { return v.end(); }

bool operator==(const VectGeneric &u) const {
return first_diff(u) == _Size;
return first_diff(u) == Size;
}
bool operator!=(const VectGeneric &u) const {
return first_diff(u) != _Size;
return first_diff(u) != Size;
}

bool operator<(const VectGeneric &u) const {
uint64_t diff = first_diff(u);
return (diff != _Size) && v[diff] < u[diff];
return (diff != Size) && v[diff] < u[diff];
}

int8_t less_partial(const VectGeneric &u, int k) const {
uint64_t diff = first_diff(u, k);
return (diff == _Size) ? 0 : int8_t(v[diff]) - int8_t(u[diff]);
return (diff == Size) ? 0 : int8_t(v[diff]) - int8_t(u[diff]);
}

VectGeneric permuted(const VectGeneric &u) const {
VectGeneric res;
for (uint64_t i = 0; i < _Size; i++)
for (uint64_t i = 0; i < Size; i++)
res[i] = v[u[i]];
return res;
}

void sort() { std::sort(v.begin(), v.end()); }

bool is_sorted() const {
for (uint64_t i = 1; i < _Size; i++)
for (uint64_t i = 1; i < Size; i++)
if (v[i - 1] < v[i])
return false;
return true;
Expand All @@ -117,95 +117,95 @@ template <size_t _Size, typename Expo = uint8_t> struct VectGeneric {
static std::random_device rd;
static std::mt19937 g(rd());

VectGeneric<_Size, Expo> res = VectGeneric<_Size, Expo>(0, 0);
VectGeneric<Size, Expo> res = VectGeneric<Size, Expo>(0, 0);
std::shuffle(res.begin(), res.end(), g);
return res;
}

uint64_t first_non_zero(size_t bound = _Size) const {
uint64_t first_non_zero(size_t bound = Size) const {
for (uint64_t i = 0; i < bound; i++)
if (v[i] != 0)
return i;
return _Size;
return Size;
}
uint64_t first_zero(size_t bound = _Size) const {
uint64_t first_zero(size_t bound = Size) const {
for (uint64_t i = 0; i < bound; i++)
if (v[i] == 0)
return i;
return _Size;
return Size;
}
uint64_t last_non_zero(size_t bound = _Size) const {
uint64_t last_non_zero(size_t bound = Size) const {
for (int64_t i = bound - 1; i >= 0; i--)
if (v[i] != 0)
return i;
return _Size;
return Size;
}
uint64_t last_zero(size_t bound = _Size) const {
uint64_t last_zero(size_t bound = Size) const {
for (int64_t i = bound - 1; i >= 0; i--)
if (v[i] == 0)
return i;
return _Size;
return Size;
}

bool is_permutation(const size_t k = _Size) const {
bool is_permutation(const size_t k = Size) const {
auto temp = v;
std::sort(temp.begin(), temp.end());
for (uint64_t i = 0; i < _Size; i++)
for (uint64_t i = 0; i < Size; i++)
if (temp[i] != i)
return false;
for (uint64_t i = k; i < _Size; i++)
for (uint64_t i = k; i < Size; i++)
if (v[i] != i)
return false;
return true;
}

uint64_t horiz_sum() const {
Expo res = 0;
for (uint64_t i = 0; i < _Size; i++)
for (uint64_t i = 0; i < Size; i++)
res += v[i];
return res;
}

VectGeneric partial_sums() const {
auto res = *this;
for (uint64_t i = 1; i < _Size; i++)
for (uint64_t i = 1; i < Size; i++)
res[i] += res[i - 1];
return res;
}

void partial_sums_inplace() {
for (uint64_t i = 1; i < _Size; i++)
for (uint64_t i = 1; i < Size; i++)
v[i] += v[i - 1];
}

Expo horiz_max() const {
Expo res = v[0];
for (uint64_t i = 1; i < _Size; i++)
for (uint64_t i = 1; i < Size; i++)
res = std::max(res, v[i]);
return res;
}

void partial_max_inplace() {
for (uint64_t i = 1; i < _Size; i++)
for (uint64_t i = 1; i < Size; i++)
v[i] = std::max(v[i], v[i - 1]);
}

Expo horiz_min() const {
Expo res = v[0];
for (uint64_t i = 1; i < _Size; i++)
for (uint64_t i = 1; i < Size; i++)
res = std::min(res, v[i]);
return res;
}

void partial_min_inplace() {
for (uint64_t i = 1; i < _Size; i++)
for (uint64_t i = 1; i < Size; i++)
v[i] = std::min(v[i], v[i - 1]);
}

VectGeneric eval() const {
VectGeneric res{};
for (size_t i = 0; i < _Size; i++)
if (v[i] < _Size)
for (size_t i = 0; i < Size; i++)
if (v[i] < Size)
res[v[i]]++;
return res;
}
Expand All @@ -218,21 +218,21 @@ static_assert(std::is_trivial<VectGeneric<12>>(),

namespace std {

template <size_t _Size, typename Expo>
template <size_t Size, typename Expo>
std::ostream &operator<<(std::ostream &stream,
const HPCombi::VectGeneric<_Size, Expo> &v) {
const HPCombi::VectGeneric<Size, Expo> &v) {
stream << "{" << std::setw(2) << unsigned(v[0]);
for (unsigned i = 1; i < _Size; ++i)
for (unsigned i = 1; i < Size; ++i)
stream << "," << std::setw(2) << unsigned(v[i]);
stream << "}";
return stream;
}

template <size_t _Size, typename Expo>
struct hash<HPCombi::VectGeneric<_Size, Expo>> {
size_t operator()(const HPCombi::VectGeneric<_Size, Expo> &ar) const {
template <size_t Size, typename Expo>
struct hash<HPCombi::VectGeneric<Size, Expo>> {
size_t operator()(const HPCombi::VectGeneric<Size, Expo> &ar) const {
size_t h = 0;
for (size_t i = 0; i < _Size; i++)
for (size_t i = 0; i < Size; i++)
h = hash<Expo>()(ar[i]) + (h << 6) + (h << 16) - h;
return h;
}
Expand Down

0 comments on commit b9375fd

Please sign in to comment.