Skip to content

Commit

Permalink
Change partial ordering underlying array to vector
Browse files Browse the repository at this point in the history
  • Loading branch information
mezpusz committed Jan 3, 2025
1 parent 60607bc commit 345d7c5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 48 deletions.
46 changes: 3 additions & 43 deletions Kernel/PartialOrdering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,29 +147,6 @@ string pocompToInfix(PoComp c) {
ASSERTION_VIOLATION;
}

PartialOrdering::PartialOrdering()
: _size(0), _array(nullptr) {}

PartialOrdering::PartialOrdering(const PartialOrdering& other)
: _size(other._size), _array(nullptr)
{
size_t arrSize = ((_size - 1) * _size / 2);
if (arrSize) {
void* mem = ALLOC_KNOWN(arrSize*sizeof(PoComp), "Kernel::PartialOrdering");
_array = array_new<PoComp>(mem, arrSize);
memcpy(_array,other._array,arrSize*sizeof(PoComp));
}
}

PartialOrdering::~PartialOrdering()
{
size_t arrSize = ((_size - 1) * _size / 2);
if (arrSize) {
array_delete(_array, arrSize);
DEALLOC_KNOWN(_array, arrSize*sizeof(PoComp), "Kernel::PartialOrdering");
}
}

PoComp PartialOrdering::get(size_t x, size_t y) const
{
ASS_L(x,_size);
Expand Down Expand Up @@ -239,31 +216,14 @@ const PartialOrdering* PartialOrdering::extend(const PartialOrdering* po)

void PartialOrdering::extend()
{
// extend array
size_t prevSize = ((_size - 1) * _size / 2);
auto prevArray = _array;
_size++;
if (_size>1) {
size_t newSize = prevSize + _size;
void* mem = ALLOC_KNOWN(newSize*sizeof(PoComp), "Kernel::PartialOrdering");
_array = array_new<PoComp>(mem, newSize);
std::memset(_array, 0, newSize*sizeof(PoComp));
static_assert(static_cast<unsigned>(PoComp::UNKNOWN) == 0);
if (prevArray) {
memcpy(_array,prevArray,prevSize*sizeof(PoComp));
}
}
// remove previous array
if (prevSize) {
array_delete(prevArray, prevSize);
DEALLOC_KNOWN(prevArray, prevSize*sizeof(PoComp), "Kernel::PartialOrdering");
}
_array.insert(_array.end(),_size,PoComp::UNKNOWN);
}

bool PartialOrdering::setRel(size_t x, size_t y, PoComp v, bool& changed)
{
size_t idx = y*(y-1)/2 + x;
ASS_L(idx,((_size - 1) * _size / 2));
ASS_L(idx,_array.size());
PoComp new_v;
if (!checkCompatibility(_array[idx], v, new_v)) {
changed = false;
Expand All @@ -286,7 +246,7 @@ bool PartialOrdering::setRelSafe(size_t x, size_t y, PoComp v, bool& changed)
PoComp PartialOrdering::getUnsafe(size_t x, size_t y) const
{
size_t idx = y*(y-1)/2 + x;
ASS_L(idx,((_size - 1) * _size / 2));
ASS_L(idx,_array.size());
return _array[idx];
}

Expand Down
11 changes: 6 additions & 5 deletions Kernel/PartialOrdering.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#define __PartialOrdering__

#include <string>
#include <vector>

namespace Kernel {

Expand Down Expand Up @@ -72,9 +73,9 @@ class PartialOrdering
friend std::ostream& operator<<(std::ostream& str, const PartialOrdering& po);

private:
PartialOrdering();
~PartialOrdering();
PartialOrdering(const PartialOrdering& other);
PartialOrdering() = default;
~PartialOrdering() = default;
PartialOrdering(const PartialOrdering&) = default;
PartialOrdering& operator=(const PartialOrdering&) = delete;

void extend();
Expand All @@ -88,8 +89,8 @@ class PartialOrdering
bool setInferredHelperInc(size_t x, size_t y, PoComp wkn);
bool setInferredHelperEq(size_t x, size_t y);

size_t _size;
PoComp* _array;
size_t _size = 0;
std::vector<PoComp> _array;
};

};
Expand Down

0 comments on commit 345d7c5

Please sign in to comment.