Skip to content

Commit 84f0cbc

Browse files
committed
Corrects slice temporary support
1 parent b02bee7 commit 84f0cbc

1 file changed

Lines changed: 25 additions & 27 deletions

File tree

slice.hpp

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace iter {
1515
//class Slice;
1616

1717
//template <typename T>
18-
//Slice<std::initializer_list<T>> slice( std::initializer_list<T> &&);
18+
//Slice<std::initializer_list<T>> slice( std::initializer_list<T>);
1919

2020
//template <typename Container, typename DifferenceType>
2121
//Slice<Container> slice(Container &&);
@@ -33,19 +33,19 @@ namespace iter {
3333
enum { value = sizeof(test<T>(0)) == sizeof(char) };
3434
};
3535
template <typename Container>
36-
typename std::enable_if<has_size<Container>::value,size_t>::type
37-
size(Container & container) {
36+
typename std::enable_if<has_size<Container>::value, std::size_t>::type
37+
size(const Container& container) {
3838
return container.size();
3939
}
4040

4141
template <typename Container>
42-
typename std::enable_if<!has_size<Container>::value,size_t>::type
43-
size(Container & container) {
42+
typename std::enable_if<!has_size<Container>::value, std::size_t>::type
43+
size(const Container& container) {
4444
return std::distance(std::begin(container), std::end(container));
4545
}
4646

47-
template <typename T, size_t N>
48-
size_t size(T (&)[N]) {
47+
template <typename T, std::size_t N>
48+
std::size_t size(const T (&)[N]) {
4949
return N;
5050
}
5151

@@ -54,7 +54,7 @@ namespace iter {
5454
template <typename Container, typename DifferenceType>
5555
class Slice {
5656
private:
57-
Container & container;
57+
Container container;
5858
DifferenceType start;
5959
DifferenceType stop;
6060
DifferenceType step;
@@ -63,17 +63,11 @@ namespace iter {
6363
// the slice function
6464
//friend Slice slice<Container, DifferenceType>(Container &&);
6565
//template <typename T>
66-
//friend Slice<std::initializer_list<T>> slice(std::initializer_list<T> &&);
67-
68-
69-
70-
71-
72-
66+
//friend Slice<std::initializer_list<T>> slice(std::initializer_list<T>);
7367
public:
74-
Slice(Container & container, DifferenceType start,
68+
Slice(Container in_container, DifferenceType start,
7569
DifferenceType stop, DifferenceType step)
76-
: container{container},
70+
: container(std::forward<Container>(in_container)),
7771
start{start},
7872
stop{stop},
7973
step{step}
@@ -83,16 +77,20 @@ namespace iter {
8377
(start > stop && step >=0)){
8478
this->stop = start;
8579
}
86-
if (this->stop > static_cast<DifferenceType>(size(container))) {
87-
this->stop = static_cast<DifferenceType>(size(container));
80+
if (this->stop > static_cast<DifferenceType>(
81+
size(this->container))) {
82+
this->stop = static_cast<DifferenceType>(size(
83+
this->container));
84+
std::cout << "stop is too large\n";
85+
std::cout << "stop is now: " << this->stop << '\n';
8886
}
8987
if (this->start < 0) {
9088
this->start = 0;
9189
}
9290
}
9391

94-
Slice () = delete;
95-
Slice & operator=(const Slice &) = delete;
92+
Slice() = delete;
93+
Slice& operator=(const Slice&) = delete;
9694

9795
Slice(const Slice &) = default;
9896

@@ -117,7 +115,7 @@ namespace iter {
117115
return *this->sub_iter;
118116
}
119117

120-
Iterator & operator++() {
118+
Iterator& operator++() {
121119
std::advance(this->sub_iter, this->step);
122120
this->current += this->step;
123121
return *this;
@@ -144,28 +142,28 @@ namespace iter {
144142
// Helper function to instantiate a Slice
145143
template <typename Container, typename DifferenceType>
146144
Slice<Container, DifferenceType> slice(
147-
Container && container, DifferenceType start,
148-
DifferenceType stop, DifferenceType step=1) {
145+
Container&& container,
146+
DifferenceType start, DifferenceType stop, DifferenceType step=1) {
149147
return {std::forward<Container>(container), start, stop, step};
150148
}
151149

152150
//only give the end as an arg and assume step is 1 and begin is 0
153151
template <typename Container, typename DifferenceType>
154152
Slice<Container, DifferenceType> slice(
155-
Container && container, DifferenceType stop) {
153+
Container&& container, DifferenceType stop) {
156154
return {std::forward<Container>(container), 0, stop, 1};
157155
}
158156

159157
template <typename T, typename DifferenceType>
160158
Slice<std::initializer_list<T>, DifferenceType> slice(
161-
std::initializer_list<T> && il, DifferenceType start,
159+
std::initializer_list<T> il, DifferenceType start,
162160
DifferenceType stop, DifferenceType step=1) {
163161
return {il, start, stop, step};
164162
}
165163

166164
template <typename T, typename DifferenceType>
167165
Slice<std::initializer_list<T>, DifferenceType> slice(
168-
std::initializer_list<T> && il, DifferenceType stop) {
166+
std::initializer_list<T> il, DifferenceType stop) {
169167
return {il, 0, stop, 1};
170168
}
171169
}

0 commit comments

Comments
 (0)