forked from Morwenn/static_math
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added class vector. Currently a mere wrapper around std::array.
- Loading branch information
Showing
3 changed files
with
217 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright (C) 2014 Morwenn | ||
* | ||
* static_math is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation, either version 3 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* static_math is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this program. If not, | ||
* see <http://www.gnu.org/licenses/>. | ||
*/ | ||
#ifndef SMATH_VECTOR_H_ | ||
#define SMATH_VECTOR_H_ | ||
|
||
//////////////////////////////////////////////////////////// | ||
// Headers | ||
//////////////////////////////////////////////////////////// | ||
#include <array> | ||
#include <cstddef> | ||
|
||
namespace smath | ||
{ | ||
/** | ||
* @brief Fixed-size vector. | ||
*/ | ||
template<typename T, std::size_t N> | ||
struct vector | ||
{ | ||
//////////////////////////////////////////////////////////// | ||
// Types | ||
|
||
using value_type = typename std::array<T, N>::value_type; | ||
using size_type = typename std::array<T, N>::size_type; | ||
using const_pointer = typename std::array<T, N>::const_pointer; | ||
|
||
//////////////////////////////////////////////////////////// | ||
// Operators | ||
|
||
constexpr auto operator[](size_type i) const | ||
-> value_type; | ||
|
||
//////////////////////////////////////////////////////////// | ||
// Accessors | ||
|
||
constexpr auto at(size_type i) const | ||
-> value_type; | ||
|
||
constexpr auto front() const | ||
-> value_type; | ||
|
||
constexpr auto back() const | ||
-> value_type; | ||
|
||
constexpr auto data() const | ||
-> const_pointer; | ||
|
||
//////////////////////////////////////////////////////////// | ||
// Capacity | ||
|
||
constexpr auto empty() const | ||
-> bool; | ||
|
||
constexpr auto size() const | ||
-> size_type; | ||
|
||
constexpr auto max_size() const | ||
-> size_type; | ||
|
||
//////////////////////////////////////////////////////////// | ||
// Member data | ||
|
||
// Internal array holding the elements. | ||
// Should be private, but that would prevent | ||
// aggregate initialization. | ||
const std::array<T, N> elems; | ||
}; | ||
|
||
#include "vector.inl" | ||
} | ||
|
||
#endif // SMATH_VECTOR_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright (C) 2014 Morwenn | ||
* | ||
* static_math is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation, either version 3 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* static_math is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this program. If not, | ||
* see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
//////////////////////////////////////////////////////////// | ||
// In-class operators | ||
|
||
template<typename T, std::size_t N> | ||
constexpr auto vector<T, N>::operator[](size_type i) const | ||
-> value_type | ||
{ | ||
return elems[i]; | ||
} | ||
|
||
//////////////////////////////////////////////////////////// | ||
// Accessors | ||
|
||
template<typename T, std::size_t N> | ||
constexpr auto vector<T, N>::at(size_type i) const | ||
-> value_type | ||
{ | ||
return elems.at(i); | ||
} | ||
|
||
template<typename T, std::size_t N> | ||
constexpr auto vector<T, N>::front() const | ||
-> value_type | ||
{ | ||
return elems.front(); | ||
} | ||
|
||
template<typename T, std::size_t N> | ||
constexpr auto vector<T, N>::back() const | ||
-> value_type | ||
{ | ||
return elems.back(); | ||
} | ||
|
||
template<typename T, std::size_t N> | ||
constexpr auto vector<T, N>::data() const | ||
-> const_pointer | ||
{ | ||
return elems.data(); | ||
} | ||
|
||
//////////////////////////////////////////////////////////// | ||
// Capacity | ||
|
||
template<typename T, std::size_t N> | ||
constexpr auto vector<T, N>::empty() const | ||
-> bool | ||
{ | ||
return elems.empty(); | ||
} | ||
|
||
template<typename T, std::size_t N> | ||
constexpr auto vector<T, N>::size() const | ||
-> size_type | ||
{ | ||
return elems.size(); | ||
} | ||
|
||
template<typename T, std::size_t N> | ||
constexpr auto vector<T, N>::max_size() const | ||
-> size_type | ||
{ | ||
return elems.max_size(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright (C) 2014 Morwenn | ||
* | ||
* static_math is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation, either version 3 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* static_math is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this program. If not, | ||
* see <http://www.gnu.org/licenses/>. | ||
*/ | ||
#include <static_math/vector.h> | ||
|
||
int main() | ||
{ | ||
using namespace smath; | ||
|
||
// TEST: basics | ||
// - construction | ||
// - accessors | ||
// - capacity | ||
{ | ||
constexpr vector<int, 1> v0 { 5 }; | ||
constexpr vector<int, 3> v1 = { 1, 2, 3 }; | ||
|
||
static_assert(v0[0] == 5, ""); | ||
static_assert(v0.at(0) == 5, ""); | ||
static_assert(v0.front() == 5, ""); | ||
static_assert(v0.back() == 5, ""); | ||
static_assert(v1[2] == 3, ""); | ||
static_assert(v1.at(1) == 2, ""); | ||
static_assert(v1.front() == 1, ""); | ||
static_assert(v1.back() == 3, ""); | ||
|
||
static_assert(not v0.empty(), ""); | ||
static_assert(v0.size() == 1, ""); | ||
static_assert(v0.max_size() == 1, ""); | ||
static_assert(not v1.empty(), ""); | ||
static_assert(v1.size() == 3, ""); | ||
static_assert(v1.max_size() == v1.size(), ""); | ||
} | ||
} |