Skip to content

Commit 714cdc0

Browse files
committed
BUGFIX: seq.begin can now use negative offsets just like seq.end
1 parent 887ae72 commit 714cdc0

4 files changed

Lines changed: 13 additions & 22 deletions

File tree

src/api/cpp/array.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -374,8 +374,7 @@ namespace af
374374

375375
const array::array_proxy array::row(int index) const
376376
{
377-
seq idx(index, index, 1);
378-
return this->operator()(idx, span, span, span);
377+
return this->operator()(index, span, span, span);
379378
}
380379

381380
array::array_proxy array::row(int index)
@@ -385,8 +384,7 @@ namespace af
385384

386385
const array::array_proxy array::col(int index) const
387386
{
388-
seq idx(index, index, 1);
389-
return this->operator()(span, idx, span, span);
387+
return this->operator()(span, index, span, span);
390388
}
391389

392390
array::array_proxy array::col(int index)
@@ -396,8 +394,7 @@ namespace af
396394

397395
const array::array_proxy array::slice(int index) const
398396
{
399-
seq idx(index, index, 1);
400-
return this->operator()(span, span, idx, span);
397+
return this->operator()(span, span, index, span);
401398
}
402399

403400
array::array_proxy array::slice(int index)

src/api/cpp/index.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ index::index(const af::array& idx0) {
7575
impl.isBatch = false;
7676
}
7777

78-
index::index(const af::index& idx0) {
78+
index::index(const af::index& idx0) {
7979
*this = idx0;
8080
}
8181

src/api/cpp/seq.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ seq::~seq()
4646
seq::seq(double n): m_gfor(false)
4747
{
4848
if (n < 0) {
49-
init(n + 1, 0, 1); // seq(-4) = -3, -2, -1, 0
49+
init(0, n, 1);
5050
} else {
5151
init(0, n - 1, 1);
5252
}

src/backend/dim4.cpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ dim4 operator*(const dim4& first, const dim4& second)
176176

177177

178178
bool
179-
isEnd(const af_seq &seq) { return (seq.end <= -1); }
179+
hasEnd(const af_seq &seq) { return (seq.begin <= -1 || seq.end <= -1); }
180180

181181
bool
182182
isSpan(const af_seq &seq) { return (seq.step == 0 && seq.begin == 1 && seq.end == 1); }
@@ -196,18 +196,11 @@ dim_t calcDim(const af_seq &seq, const dim_t &parentDim)
196196
dim_t outDim = 1;
197197
if (isSpan(seq)) {
198198
outDim = parentDim;
199-
} else if (isEnd(seq)) {
200-
if(seq.begin == -1) { // only end is passed as seq
201-
outDim = 1;
202-
} else if (seq.begin < 0) {
203-
af_seq temp = {parentDim + seq.begin,
204-
parentDim + seq.end,
205-
seq.step};
206-
outDim = seqElements(temp);
207-
} else { // end is passed as a part of seq
208-
af_seq temp = {seq.begin, parentDim + seq.end, seq.step};
209-
outDim = seqElements(temp);
210-
}
199+
} else if (hasEnd(seq)) {
200+
af_seq temp = {seq.begin, seq.end, seq.step};
201+
if (seq.begin < 0) temp.begin += parentDim;
202+
if (seq.end < 0) temp.end += parentDim;
203+
outDim = seqElements(temp);
211204
} else {
212205
DIM_ASSERT(1, seq.begin >= -DBL_MIN && seq.begin < parentDim);
213206
DIM_ASSERT(1, seq.end < parentDim);
@@ -216,7 +209,8 @@ dim_t calcDim(const af_seq &seq, const dim_t &parentDim)
216209

217210
return outDim;
218211
}
219-
}
212+
213+
} // end namespace af
220214

221215
using af::dim4;
222216
using std::vector;

0 commit comments

Comments
 (0)