Skip to content

Commit 924c435

Browse files
Владимир СеверовВладимир Северов
authored andcommitted
17.04.28 15:23 - Some fixes
1 parent 30158cb commit 924c435

File tree

3 files changed

+59
-21
lines changed

3 files changed

+59
-21
lines changed

M3-LW-1/M3-LW-1/BooleanFunction.cpp

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ BooleanFunction BooleanFunction::operator()(const std::vector<BooleanFunction>&
301301
}
302302
/**/
303303

304-
// ВАРИАНТ 2. Функция от векторов значений
304+
/*/ ВАРИАНТ 2. Функция от векторов значений
305305
BooleanFunction BooleanFunction::operator()(const std::vector<BooleanFunction>& fs) const
306306
{
307307
if (fs.size() != dimension()) {
@@ -334,6 +334,37 @@ BooleanFunction BooleanFunction::operator()(const std::vector<BooleanFunction>&
334334
}
335335
/**/
336336

337+
BooleanFunction BooleanFunction::operator()(const std::vector<BooleanFunction>& fs) const
338+
{
339+
if (fs.size() != dimension()) {
340+
throw IncorrectLengthException();
341+
}
342+
if (fs.size() == 0) {
343+
return BooleanFunction();
344+
}
345+
346+
size_type d = 0;
347+
for (BooleanFunction const& f : fs) {
348+
if (f.dimension() > d) {
349+
d = f.dimension();
350+
}
351+
}
352+
353+
BooleanFunction result(d);
354+
355+
for (size_type i = 0; i < result.func_.size(); ++i) {
356+
std::vector<bool> vars;
357+
for (BooleanFunction const& f : fs) {
358+
vars.push_back(f.func_[i % f.size()]);
359+
}
360+
result.func_[i] = (*this)(vars);
361+
}
362+
363+
//result.func_ = ReduceFunc_(result.func_);
364+
365+
return result;
366+
}
367+
337368
BooleanFunction BooleanFunction::operator()(const std::initializer_list<BooleanFunction> fs) const
338369
{
339370
if (fs.size() != dimension()) {
@@ -360,7 +391,7 @@ BooleanFunction BooleanFunction::operator()(const std::initializer_list<BooleanF
360391
result.func_[i] = (*this)(vars);
361392
}
362393

363-
result.func_ = ReduceFunc_(result.func_);
394+
//result.func_ = ReduceFunc_(result.func_);
364395

365396
return result;
366397
}
@@ -469,22 +500,22 @@ std::vector<BooleanFunction::value_type> BooleanFunction::anf() const
469500
return result;
470501
}
471502

472-
std::vector<BooleanFunction::value_type> BooleanFunction::ReduceFunc_(std::vector<value_type> const& func)
503+
void BooleanFunction::ReduceFunc()
473504
{
474-
size_type dim = static_cast<size_type>(log2(func.size()));
505+
size_type dim = static_cast<size_type>(log2(func_.size()));
475506

476507
std::vector<bool> isEssential(dim, false);
477508

478509
for (size_type j = 0; j < dim; ++j) {
479510
size_type s = static_cast<size_type>(pow(2, j)); // Размер половины участка
480-
size_type c = static_cast<size_type>(func.size() / (s * 2)); // Количество пар
511+
size_type c = static_cast<size_type>(func_.size() / (s * 2)); // Количество пар
481512

482513
for (size_type k = 0; k < c; ++k) {
483514
if (isEssential[j]) {
484515
break;
485516
}
486517
for (size_type i = 2 * s * k; i < 2 * s * k + s; ++i) {
487-
if (func[i] != func[i + s]) {
518+
if (func_[i] != func_[i + s]) {
488519
isEssential[j] = true;
489520
break;
490521
}
@@ -500,12 +531,12 @@ std::vector<BooleanFunction::value_type> BooleanFunction::ReduceFunc_(std::vecto
500531
}
501532

502533
if (dim != newDim) {
503-
std::vector<bool> isResValue(func.size(), true);
534+
std::vector<bool> isResValue(func_.size(), true);
504535

505536
for (size_type j = 0; j < dim; ++j) {
506537
if (!isEssential[j]) {
507538
size_type s = static_cast<size_type>(pow(2, j)); // Размер половины участка
508-
size_type c = static_cast<size_type>(func.size() / (s * 2)); // Количество пар
539+
size_type c = static_cast<size_type>(func_.size() / (s * 2)); // Количество пар
509540

510541
for (size_type k = 0; k < c; ++k) {
511542
for (size_type i = 2 * s * k + s; i < 2 * s * k + 2 * s; ++i) {
@@ -517,18 +548,14 @@ std::vector<BooleanFunction::value_type> BooleanFunction::ReduceFunc_(std::vecto
517548

518549
std::vector<value_type> result;
519550

520-
for (size_type i = 0; i < func.size(); ++i) {
551+
for (size_type i = 0; i < func_.size(); ++i) {
521552
if (isResValue[i]) {
522-
result.push_back(func[i]);
553+
result.push_back(func_[i]);
523554
}
524555
}
525556

526-
return result;
527-
}
528-
else {
529-
return func;
557+
func_ = result;
530558
}
531-
532559
}
533560

534561
std::string get_polynom(const BooleanFunction& rhs)

M3-LW-1/M3-LW-1/BooleanFunction.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ class BooleanFunction
136136
BooleanFunction operator()(const std::initializer_list<BooleanFunction> fs) const;
137137

138138
bool is_monotone() const;
139-
bool is_symmetric() const;
139+
bool is_symmetric() const; /* БОНУС */
140140
bool is_self_dual() const;
141141
bool is_linear() const;
142142
bool is_T1() const;
@@ -149,10 +149,11 @@ class BooleanFunction
149149
// Возвращает АНФ функции
150150
std::vector<value_type> anf() const;
151151

152+
// Удаляет фиктивные переменные
153+
void ReduceFunc(); /* БОНУС */
154+
152155
private:
153156
std::vector<value_type> func_;
154-
155-
static std::vector<value_type> ReduceFunc_(std::vector<value_type> const& func);
156157
};
157158

158159
// пусть BooleanFunction представляет из себя функцию "01110000"

M3-LW-1/M3-LW-1/Source.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -533,12 +533,10 @@ void test_combo()
533533
assert(!coef[2]);
534534
assert(coef[4 + 1]); // x0y
535535
assert(coef[4 + 2]); // x1y
536-
/*/ Единственный неработающий тест
537536
assert(coef[16 + 8 + 1]); // z0z1x0
538537
assert(coef[16 + 8 + 2]); // z0z1x0
539538
assert(coef[16 + 8 + 4]); // z0z1y
540-
assert(!coef[63]);
541-
/**/
539+
assert(!coef[31]);
542540
}
543541

544542
void test_combo2()
@@ -561,6 +559,15 @@ void test_combo2()
561559
assert(test.weight() == 0);
562560
}
563561

562+
void test_bonus()
563+
{
564+
BooleanFunction x = BooleanFunction::var(2, 5);
565+
assert(get_polynom(x) == "x2");
566+
x.ReduceFunc();
567+
assert(get_polynom(x) == "x0");
568+
assert(x.is_symmetric());
569+
}
570+
564571
int main()
565572
{
566573
RUN_TEST(test_ctor0, "successed");
@@ -596,5 +603,8 @@ int main()
596603
RUN_TEST(test_combo, "successed");
597604
RUN_TEST(test_combo2, "successed");
598605

606+
/* Тест бонусных функций */
607+
RUN_TEST(test_bonus, "successed");
608+
599609
system("pause");
600610
}

0 commit comments

Comments
 (0)