-
Notifications
You must be signed in to change notification settings - Fork 549
Expand file tree
/
Copy pathseq.cpp
More file actions
87 lines (72 loc) · 2.06 KB
/
seq.cpp
File metadata and controls
87 lines (72 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*******************************************************
* Copyright (c) 2014, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#include <af/array.h>
#include <af/data.h>
#include <af/seq.h>
#include "error.hpp"
#include <cmath>
namespace af {
int end = -1;
seq span(af_span);
void seq::init(double begin, double end, double step) {
this->s.begin = begin;
this->s.end = end;
this->s.step = step;
if (step != 0) { // Not Span
size = std::fabs((end - begin) / step) + 1;
} else {
size = 0;
}
}
#ifndef signbit
// wtf windows?!
inline int signbit(double x) {
if (x < 0) { return -1; }
return 0;
}
#endif
seq::~seq() = default;
seq::seq(double length) : s{}, size{}, m_gfor(false) {
if (length < 0) {
init(0, length, 1);
} else {
init(0, length - 1, 1);
}
}
seq::seq(const af_seq& s_) : s{}, size{}, m_gfor(false) {
init(s_.begin, s_.end, s_.step);
}
seq& seq::operator=(const af_seq& s_) {
init(s_.begin, s_.end, s_.step);
return *this;
}
seq::seq(double begin, double end, double step) : s{}, size{}, m_gfor(false) {
if (step == 0) {
if (begin != end) { // Span
AF_THROW_ERR("Invalid step size", AF_ERR_ARG);
}
}
if ((signbit(end) == signbit(begin)) &&
(signbit(end - begin) != signbit(step))) {
AF_THROW_ERR("Sequence is invalid", AF_ERR_ARG);
}
init(begin, end, step);
}
seq::seq(seq other, // NOLINT(performance-unnecessary-value-param)
bool is_gfor)
: s(other.s), size(other.size), m_gfor(is_gfor) {}
seq::operator array() const {
double diff = s.end - s.begin;
dim_t len = static_cast<int>(
(diff + std::fabs(s.step) * (signbit(diff) == 0 ? 1 : -1)) / s.step);
array tmp = (m_gfor) ? range(1, 1, 1, len, 3) : range(len);
array res = s.begin + s.step * tmp;
return res;
}
} // namespace af