1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
////////////////////////////////////////////////////////////////////////////////
//
// Permutation.cc
//
// produced: 13/03/98 jr
// last change: 13/03/98 jr
//
////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <ctype.h>
#include <string.h>
#include "Permutation.hh"
// functions:
Permutation& Permutation::append(const parameter_type i) {
permutation_data::append(i);
if (i > _n) {
_n = i;
}
++_k;
return *this;
}
Permutation& Permutation::append(const Permutation& p) {
permutation_data::append(p);
if (p._n > _n) {
_n = p._n;
}
_k += p._k;
return *this;
}
const int Permutation::sign() const {
int result = 1;
for (parameter_type i = 0; i < _k; ++i) {
for (parameter_type j = 0; j < i; ++j) {
if ((*this)[i] < (*this)[j]) {
result *= -1;
}
}
}
return result;
}
const int Permutation::sign(const parameter_type split) const {
int result = 1;
for (parameter_type i = split; i < _k; ++i) {
for (parameter_type j = 0; j < split; ++j) {
if ((*this)[i] < (*this)[j]) {
result *= -1;
}
}
}
return result;
}
const int Permutation::sort() {
int result = 1;
for (parameter_type i = 0; i < _k; ++i) {
for (parameter_type j = 0; j < i; ++j) {
if ((*this)[i] < (*this)[j]) {
parameter_type tmp = (*this)[i];
(*this)[i] = (*this)[j];
(*this)[j] = tmp;
result *= -1;
}
}
}
return result;
}
Permutation Permutation::complement() const {
Permutation result(_n, _n - _k);
size_type count(0);
for (parameter_type j = 0; j < (*this)[0]; ++j) {
result[count++] = j;
}
for (parameter_type i = 0; i < _k - 1; ++i) {
for (parameter_type j = (*this)[i] + 1; j < (*this)[i + 1]; ++j) {
result[count++] = j;
}
}
for (parameter_type j = (*this)[_k - 1] + 1; j < _n; ++j) {
result[count++] = j;
}
return result;
}
Permutation Permutation::deletion(const parameter_type m) const {
Permutation result(*this);
for (parameter_type i = m; i < _k - 1; ++i) {
result[i] = result[i + 1];
}
result.resize(--result._k);
return result;
}
Permutation Permutation::reverse() const {
Permutation result(_n,_k);
for (parameter_type i = 0; i < _k; ++i) {
result[i] = (*this)[_k-i-1];
}
return result;
}
bool Permutation::lexnext() {
for (parameter_type i = 0; i < _k; ++i) {
if ((*this)[_k - i - 1] == _n - i - 1) {
continue;
}
else {
++(*this)[_k - i - 1];
for (parameter_type j = 0; j < i; ++j) {
(*this)[_k - j - 1] = (*this)[_k - i - 1] + (i - j);
}
return true;
}
}
return false;
}
std::ostream& operator<<(std::ostream& ost, const Permutation& p) {
return (ost << (const permutation_data&)p);
}
std::istream& operator>>(std::istream& ist, Permutation& p) {
ist >> (permutation_data&)p;
p._k = p.maxindex();
p._n = p._k;
for (parameter_type i = 0; i < p.maxindex(); ++i) {
if (p[i] + 1 > p._n) {
p._n = p[i] + 1;
}
}
return ist;
}
// eof Permutation.cc
|