Skip to content

Commit fba09f8

Browse files
committed
Add operator[].
1 parent 3a278a0 commit fba09f8

File tree

2 files changed

+16
-33
lines changed

2 files changed

+16
-33
lines changed

Diff for: README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ array<int, my_3d_shape_type> my_array(my_3d_shape);
3838
```
3939
4040
The array can be accessed in a number of ways.
41-
`array::operator()(...)` and `array::at(...)` have similar semantics to `std::vector::operator[]` and `std::vector::at`.
41+
`array::operator()` or `array::operator[]`, and `array::at` have similar semantics to `std::vector::operator[]` and `std::vector::at`.
4242
`array::at` will check that the index is in range, and throws `std::out_of_range` if it is not.
4343
There are both variadic and `index_type` overloads of both of these accessors.
4444
`index_type` is a specialization of `std::tuple` defined by `shape` (and `array` and `array_ref`), e.g. `my_3d_shape_type::index_type`.
@@ -51,7 +51,7 @@ for (int z = 0; z < depth; z++) {
5151
my_array(x, y, z) = 5;
5252
// Or the index_type versions:
5353
my_array.at(my_3d_shape_type::index_type(x, y, z)) = 5;
54-
my_array({x, y, z}) = 5;
54+
my_array[{x, y, z}] = 5;
5555
}
5656
}
5757
}
@@ -71,7 +71,7 @@ for_all_indices(my_3d_shape, [&](int x, int y, int z) {
7171
my_array(x, y, z) = 5;
7272
});
7373
for_each_index(my_3d_shape, [&](my_3d_shape_type::index_type i) {
74-
my_array(i) = 5;
74+
my_array[i] = 5;
7575
});
7676
```
7777

Diff for: array.h

+13-30
Original file line numberDiff line numberDiff line change
@@ -484,14 +484,10 @@ class shape {
484484
return at(std::make_tuple(indices...));
485485
}
486486
/** Compute the flat offset of the index 'indices'. */
487-
index_t operator() (const index_type& indices) const {
488-
return internal::flat_offset(dims_, indices);
489-
}
487+
index_t operator() (const index_type& indices) const { return internal::flat_offset(dims_, indices); }
490488
template <typename... Indices,
491489
typename = typename std::enable_if<internal::all_integral<Indices...>::value>::type>
492-
index_t operator() (Indices... indices) const {
493-
return (*this)(std::make_tuple(indices...));
494-
}
490+
index_t operator() (Indices... indices) const { return (*this)(std::make_tuple(indices...)); }
495491

496492
/** Get a specific dim of this shape. */
497493
template <size_t D>
@@ -1100,24 +1096,17 @@ class array_ref {
11001096

11011097
/** Get a reference to the element at the given 'indices'. If the 'indices'
11021098
* are out of range of 'shape()', throws std::out_of_range. */
1103-
reference at(const index_type& indices) const {
1104-
return base_[shape_.at(indices)];
1105-
}
1099+
reference at(const index_type& indices) const { return base_[shape_.at(indices)]; }
11061100
template <typename... Indices,
11071101
typename = typename std::enable_if<internal::all_integral<Indices...>::value>::type>
1108-
reference at(Indices... indices) const {
1109-
return base_[shape_.at(indices...)];
1110-
}
1102+
reference at(Indices... indices) const { return base_[shape_.at(indices...)]; }
11111103

11121104
/** Get a reference to the element at the given indices. */
1113-
reference operator() (const index_type& indices) const {
1114-
return base_[shape_(indices)];
1115-
}
1105+
reference operator() (const index_type& indices) const { return base_[shape_(indices)]; }
1106+
reference operator[] (const index_type& indices) const { return base_[shape_(indices)]; }
11161107
template <typename... Indices,
11171108
typename = typename std::enable_if<internal::all_integral<Indices...>::value>::type>
1118-
reference operator() (Indices... indices) const {
1119-
return base_[shape_(indices...)];
1120-
}
1109+
reference operator() (Indices... indices) const { return base_[shape_(indices...)]; }
11211110

11221111
/** Call a function with a reference to each value in this array_ref. The
11231112
* order in which 'fn' is called is undefined. */
@@ -1463,22 +1452,16 @@ class array {
14631452

14641453
/** Compute the flat offset of the indices. Does not check if the indices are
14651454
* in bounds. */
1466-
reference operator() (const index_type& indices) {
1467-
return base_[shape_(indices)];
1468-
}
1455+
reference operator() (const index_type& indices) { return base_[shape_(indices)]; }
1456+
reference operator[] (const index_type& indices) { return base_[shape_(indices)]; }
1457+
const_reference operator() (const index_type& indices) const { return base_[shape_(indices)]; }
1458+
const_reference operator[] (const index_type& indices) const { return base_[shape_(indices)]; }
14691459
template <typename... Indices,
14701460
typename = typename std::enable_if<internal::all_integral<Indices...>::value>::type>
1471-
reference operator() (Indices... indices) {
1472-
return base_[shape_(indices...)];
1473-
}
1474-
const_reference operator() (const index_type& indices) const {
1475-
return base_[shape_(indices)];
1476-
}
1461+
reference operator() (Indices... indices) { return base_[shape_(indices...)]; }
14771462
template <typename... Indices,
14781463
typename = typename std::enable_if<internal::all_integral<Indices...>::value>::type>
1479-
const_reference operator() (Indices... indices) const {
1480-
return base_[shape_(indices...)];
1481-
}
1464+
const_reference operator() (Indices... indices) const { return base_[shape_(indices...)]; }
14821465

14831466
/** Call a function with a reference to each value in this array. The order in
14841467
* which 'fn' is called is undefined. */

0 commit comments

Comments
 (0)