Skip to content

Commit

Permalink
Fixed pw_inter complement
Browse files Browse the repository at this point in the history
  • Loading branch information
Kalashnikovni committed Jul 27, 2023
1 parent 4c26b56 commit ac843dd
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 44 deletions.
8 changes: 7 additions & 1 deletion sbg/interval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace SBG {
namespace LIB {

Interval::Interval() : begin_(1), step_(1), end_(0) {}
Interval::Interval(NAT x) : begin_(x), step_(1), end_(x) {}
Interval::Interval(NAT begin, NAT step, NAT end) : begin_(begin), step_(step), end_(end)
{
if (end >= begin) {
Expand Down Expand Up @@ -65,7 +66,12 @@ std::ostream &operator<<(std::ostream &out, const Interval &i)

// Set functions ---------------------------------------------------------------

unsigned int cardinal(Interval i) { return (i.end() - i.begin()) / i.step() + 1; }
unsigned int cardinal(Interval i)
{
if (!isEmpty(i)) return (i.end() - i.begin()) / i.step() + 1;

return 0;
}

bool isEmpty(Interval i) { return i.end() < i.begin(); }

Expand Down
6 changes: 3 additions & 3 deletions sbg/interval.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,11 @@
******************************************************************************/

#ifndef SBG_NATERVAL_HPP
#define SBG_NATERVAL_HPP
#ifndef SBG_INTERVAL_HPP
#define SBG_INTERVAL_HPP

#include <boost/foreach.hpp>
#include <cmath>
#include <iostream>
#include <numeric>
#include <optional>

Expand All @@ -51,6 +50,7 @@ struct Interval {
member_class(NAT, end);

Interval();
Interval(NAT x);
Interval(NAT begin, NAT step, NAT end);

eq_class(Interval);
Expand Down
78 changes: 49 additions & 29 deletions sbg/pw_inter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,22 @@ PWInterval cup(PWInterval pwi1, PWInterval pwi2)
// As the complement operation will add intervals to our set, we choose the
// one with least quantity of them.
PWInterval lt_pieces, gt_pieces;
if (pwi1.pieces().size() > pwi2.pieces().size()) {
lt_pieces = pwi2;
gt_pieces = pwi1;
}
int c_size1 = 0, c_size2 = 0;
BOOST_FOREACH (Interval i1, pwi1.pieces())
c_size1 += i1.step();
BOOST_FOREACH (Interval i2, pwi2.pieces())
c_size2 += i2.step();

else {
if (c_size1 < c_size2) {
lt_pieces = pwi1;
gt_pieces = pwi2;
}

else {
lt_pieces = pwi2;
gt_pieces = pwi1;
}

PWInterval diff = difference(gt_pieces, lt_pieces);
if (isCompact(lt_pieces) && isCompact(gt_pieces))
un = traverse(lt_pieces, diff, &least);
Expand All @@ -194,41 +200,55 @@ PWInterval cup(PWInterval pwi1, PWInterval pwi2)
return PWInterval(un);
}

PWInterval complement(PWInterval pwi)
PWInterval complement(Interval i)
{
InterSet c;

if (isEmpty(pwi)) return PWInterval(SetPiece(0, 1, Util::Inf));
// Before interval
if (i.begin() != 0) {
SetPiece i_res(0, 1, i.begin() - 1);
if (!isEmpty(i_res))
c.emplace_hint(c.cend(), i_res);
}

Util::NAT last_end = 0;
BOOST_FOREACH (SetPiece i, pwi.pieces()){
// Before interval
if (i.begin() != 0) {
SetPiece i_res(last_end, 1, i.begin() - 1);
// "During" interval
if (i.begin() < Util::Inf) {
for (Util::NAT j = 1; j < i.step(); j++) {
SetPiece i_res(i.begin() + j, i.step(), i.end());
if (!isEmpty(i_res))
c.emplace_hint(c.cend(), i_res);
}

// "During" interval
if (i.begin() < Util::Inf)
for (Util::NAT j = 1; j < i.step(); j++) {
SetPiece i_res(i.begin() + j, i.step(), i.end());
if (!isEmpty(i_res))
c.emplace_hint(c.cend(), i_res);
}

last_end = i.end() + 1;
}
}

// After intervals
last_end = maxElem(pwi);
if (last_end < Util::Inf)
c.insert(SetPiece(last_end + 1, 1, Util::Inf));
// After interval
if (maxElem(i) < Util::Inf)
c.insert(SetPiece(maxElem(i) + 1, 1, Util::Inf));

else
c.insert(SetPiece(Util::Inf, 1, Util::Inf));
c.insert(SetPiece(Util::Inf));


return c;
}

PWInterval complement(PWInterval pwi)
{
PWInterval res;

if (isEmpty(pwi)) return PWInterval(SetPiece(0, 1, Util::Inf));

InterSetIt first_it = pwi.pieces_ref().begin();
SetPiece first = *first_it;
res = complement(first);

++first_it;
InterSet second(first_it, pwi.pieces_ref().end());
BOOST_FOREACH (Interval i, second) {
PWInterval c = complement(i);
res = intersection(res, c);
}

return PWInterval(c);
return res;
}

PWInterval difference(PWInterval pwi1, PWInterval pwi2) { return intersection(pwi1, complement(pwi2)); }
Expand Down
1 change: 1 addition & 0 deletions sbg/pw_inter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ Util::NAT minElem(PWInterval pwi);
Util::NAT maxElem(PWInterval pwi);
PWInterval intersection(PWInterval i1, PWInterval i2);
PWInterval cup(PWInterval i1, PWInterval i2);
PWInterval complement(Interval i);
PWInterval complement(PWInterval i);
PWInterval difference(PWInterval i1, PWInterval i2);

Expand Down
23 changes: 16 additions & 7 deletions test/eval/gt_data/set/SBG.log
Original file line number Diff line number Diff line change
Expand Up @@ -55,28 +55,37 @@ maxElem({[500:3:1000], [1:1:100]})
--> {[1:100], [200:400]}

{[1:3:100], [2:3:100]}/\{[0:1:2]}
--> {[1:3:1], [2:3:2]}
--> {[1:1], [2:2]}

{[1:10:100], [2:10:100], [3:10:100], [4:10:100], [5:10:100], [6:10:100], [7:10:100], [8:10:100]}/\{[3:1:5], [7:1:8]}
--> {[3:10:3], [4:10:4], [5:10:5], [7:10:7], [8:10:8]}
--> {[3:3], [4:4], [5:5], [7:7], [8:8]}

{[1:3:100], [2:3:100]}/\{[0:1:2], [4:6:94], [5:6:95], [6:6:96], [7:6:97], [8:6:98], [100:1:10000]}
--> {[1:3:1], [2:3:2], [4:6:94], [5:6:95], [7:6:97], [8:6:98], [100:3:100]}
--> {[1:1], [2:2], [4:6:94], [5:6:95], [7:6:97], [8:6:98], [100:100]}

{[3:6:100]}'
--> {[0:2], [4:6:94], [5:6:95], [6:6:96], [7:6:97], [8:6:98], [100:Inf]}

{[1:1:100], [101:2:300]}'
--> {[0:0], [102:2:298], [300:Inf]}

{[1:1:100], [101:3:300]}'
--> {[0:0], [102:3:297], [103:3:298], [300:Inf]}

{[3:6:100]}'
--> {[0:2], [4:6:94], [5:6:95], [6:6:96], [7:6:97], [8:6:98], [100:Inf]}
{[3:10:100], [4:10:100], [5:10:100], [6:10:100], [7:10:100], [8:10:100]}'
--> {[0:2], [9:10:89], [10:10:90], [11:10:91], [12:10:92], [99:Inf]}

{[3:10:100], [4:10:100], [5:10:100], [6:10:100], [7:10:100], [8:10:100], [10:2:12]}'
--> {[0:2], [9:9], [11:11], [19:10:89], [20:10:90], [21:10:91], [22:10:92], [99:Inf]}

{[3:10:100], [4:10:100], [7:2:11], [28:10:100], [39:10:100]}'
--> {[0:2], [5:5], [6:6], [8:8], [10:10], [12:10:22], [15:10:25], [16:10:26], [17:10:27], [18:18], [19:19], [20:20], [21:21], [29:29], [30:30], [31:31], [32:32], [35:35], [36:36], [37:37], [40:10:90], [41:10:91], [42:10:92], [45:10:85], [46:10:86], [47:10:87], [95:95], [96:96], [97:97], [100:Inf]}

{[50:2:150], [300:1:500]}\{[1:1:100], [101:2:300]}
--> {[102:2:150], [300:500]}

{[1:3:100], [2:3:100]}\{[3:6:100]}
--> {[1:3:1], [2:3:2], [4:6:94], [5:6:95], [7:6:97], [8:6:98], [100:3:100]}
--> {[1:1], [2:2], [4:6:94], [5:6:95], [7:6:97], [8:6:98], [100:100]}

{}\/{[1:1:100]}
--> {[1:100]}
Expand All @@ -94,7 +103,7 @@ maxElem({[500:3:1000], [1:1:100]})
--> {[1:100], [50:2:150], [101:2:299], [300:500]}

{[1:1:100], [150:1:250], [450:1:600]}\/{[200:1:300], [425:1:430], [550:1:1000]}
--> {[1:100], [150:250], [251:300], [425:430], [450:600], [601:1000]}
--> {[1:100], [150:199], [200:300], [425:430], [450:549], [550:1000]}

{}=={}
--> 1
Expand Down
5 changes: 4 additions & 1 deletion test/parser/gt_data/set/SBG.log
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ maxElem({[500:3:1000], [1:1:100]})
{[1:3:100], [2:3:100]}/\{[0:1:2]}
{[1:10:100], [2:10:100], [3:10:100], [4:10:100], [5:10:100], [6:10:100], [7:10:100], [8:10:100]}/\{[3:1:5], [7:1:8]}
{[1:3:100], [2:3:100]}/\{[0:1:2], [4:6:94], [5:6:95], [6:6:96], [7:6:97], [8:6:98], [100:1:10000]}
{[3:6:100]}'
{[1:1:100], [101:2:300]}'
{[1:1:100], [101:3:300]}'
{[3:6:100]}'
{[3:10:100], [4:10:100], [5:10:100], [6:10:100], [7:10:100], [8:10:100]}'
{[3:10:100], [4:10:100], [5:10:100], [6:10:100], [7:10:100], [8:10:100], [10:2:12]}'
{[3:10:100], [4:10:100], [7:2:11], [28:10:100], [39:10:100]}'
{[50:2:150], [300:1:500]}\{[1:1:100], [101:2:300]}
{[1:3:100], [2:3:100]}\{[3:6:100]}
{}\/{[1:1:100]}
Expand Down
4 changes: 1 addition & 3 deletions test/set.test
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,12 @@ maxElem({[500:3:1000], [1:1:100]})
/\ {[3:1:5], [7:1:8]})
({[1:3:100], [2:3:100]} /\ {[0:1:2], [4:6:94], [5:6:95], [6:6:96], [7:6:97], [8:6:98], [100:1:10000]})

-{[3:6:100]}
-{[1:1:100], [101:2:300]}
-{[1:1:100], [101:3:300]}
-{[3:6:100]}
-{[3:10:100], [4:10:100], [5:10:100], [6:10:100], [7:10:100], [8:10:100]}
-{[3:10:100], [4:10:100], [5:10:100], [6:10:100], [7:10:100], [8:10:100], [10:2:12]}
-{[3:10:100], [4:10:100], [7:2:11], [28:10:100], [39:10:100]}
{[0:2], [4:10:100], ..., [12:10:100]}
{[0:3], [5:10:100], ..., [13:10:100]}

({[50:2:150], [300:1:500]} \ {[1:1:100], [101:2:300]})
({[1:3:100], [2:3:100]} \ {[3:6:100]})
Expand Down

0 comments on commit ac843dd

Please sign in to comment.