Commit 291678d6 authored by CARDOSI Paul's avatar CARDOSI Paul
Browse files

Fix issue : lastIdx should be subtracted step in case where the item to be...

Fix issue : lastIdx should be subtracted step in case where the item to be removed is the last element.
parent cd415494
......@@ -44,7 +44,7 @@ class SpArrayView {
long int step;
long int lastElement() const {
return std::max(0L,((lastIdx-firstIdx-1)/step)*step) + firstIdx;
return ((lastIdx-firstIdx-1)/step)*step + firstIdx;
}
bool isInside(const long int inIdx) const{
......@@ -53,7 +53,7 @@ class SpArrayView {
}
long int nbElements() const{
return (lastIdx-firstIdx)/step;
return (lastIdx-firstIdx-1)/step + 1;
}
};
......@@ -225,16 +225,14 @@ public:
if(iter != intervals.end() && (*iter).isInside(inIdx)){
assert((*iter).nbElements() != 0);
if((*iter).firstIdx == inIdx){
(*iter).firstIdx += (*iter).step;
if((*iter).nbElements() == 0){
if((*iter).nbElements() == 1){
intervals.erase(iter);
} else {
(*iter).firstIdx += (*iter).step;
}
}
else if(inIdx == (*iter).lastElement()){
(*iter).lastIdx -= 1;
if((*iter).nbElements() == 0){
intervals.erase(iter);
}
(*iter).lastIdx -= (*iter).step;
}
else{
auto beforeIter = intervals.insert(iter, *iter);
......
......@@ -170,6 +170,58 @@ class TestArrayView : public UTester< TestArrayView > {
CompareWithArray(view, shouldBe, __LINE__);
}
}
{
SpArrayView view(5, 21, 5);
UASSERTETRUE(view.getNbIntervals() == 1);
{
small_vector<long int> shouldBe(21, 0);
shouldBe[20] = 1;
shouldBe[15] = 1;
shouldBe[10] = 1;
shouldBe[5] = 1;
CompareWithArray(view, shouldBe, __LINE__);
}
view.removeItem(20);
UASSERTETRUE(view.getNbIntervals() == 1);
{
small_vector<long int> shouldBe(21, 0);
shouldBe[15] = 1;
shouldBe[10] = 1;
shouldBe[5] = 1;
CompareWithArray(view, shouldBe, __LINE__);
}
view.removeItem(15);
UASSERTETRUE(view.getNbIntervals() == 1);
{
small_vector<long int> shouldBe(21, 0);
shouldBe[10] = 1;
shouldBe[5] = 1;
CompareWithArray(view, shouldBe, __LINE__);
}
view.removeItem(10);
UASSERTETRUE(view.getNbIntervals() == 1);
{
small_vector<long int> shouldBe(21, 0);
shouldBe[5] = 1;
CompareWithArray(view, shouldBe, __LINE__);
}
view.removeItem(5);
UASSERTETRUE(view.getNbIntervals() == 0);
{
small_vector<long int> shouldBe(21, 0);
CompareWithArray(view, shouldBe, __LINE__);
}
}
}
void SetTests() {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment