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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
////////////////////////////////////////////////////////////////////////////////
//
// Permutation.cc
//
// produced: 13/03/98 jr
// last change: 13/03/98 jr
//
////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <ctype.h>
#include <string.h>
#include <assert.h>
#include "Permutation.hh"
namespace topcom {
const char Permutation::start_char = '[';
const char Permutation::end_char = ']';
const char Permutation::delim_char = ',';
// functions:
Permutation& Permutation::push_back(const parameter_type i) {
permutation_data::push_back(i);
if (i >= _n) {
_n = i + 1;
}
++_k;
return *this;
}
Permutation& Permutation::push_back(const Permutation& p) {
for (parameter_type i = 0; i < p.size(); ++i) {
push_back(p(i));
}
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)(i)) {
parameter_type tmp = (*this)(i);
(*this)(i) = (*this)(i);
(*this)(i) = 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::istream& Permutation::read(std::istream& ist) {
char c;
parameter_type elem;
permutation_data::resize(0);
ist >> std::ws >> c;
if (c == start_char) {
while (ist >> std::ws >> c) {
if (c == end_char) {
break;
}
if (c == delim_char) {
continue;
}
ist.putback(c);
if (ist >> elem) {
permutation_data::push_back(elem);
}
else {
#ifdef READ_DEBUG
std::cerr << "Permutation::read(std::istream& ist):"
<< c << " not of appropriate type." << std::endl;
#endif
ist.clear(std::ios::failbit);
return ist;
}
}
}
else {
#ifdef READ_DEBUG
std::cerr << "Permutation::read(std::istream& ist):"
<< "missing `" << start_char << "'." << std::endl;
#endif
ist.clear(std::ios::failbit);
return ist;
}
ist.clear(std::ios::goodbit);
return ist;
}
std::ostream& Permutation::write(std::ostream& ost) const {
ost << Permutation::start_char;
if (!empty()) {
parameter_type i = 0;
ost << (*this)(i);
while(++i < size()) {
ost << Permutation::delim_char << (*this)(i);
}
}
ost << Permutation::end_char;
return ost;
}
std::istream& operator>>(std::istream& ist, Permutation& p) {
return p.read(ist);
}
std::ostream& operator<<(std::ostream& ost, const Permutation& p) {
return p.write(ost);
}
}; // namespace topcom
// eof Permutation.cc
|