2015-03-06 01:46:01 +11:00
|
|
|
/*
|
2015-04-13 18:05:28 +10:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2015-03-06 01:46:01 +11:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-03-06 01:46:01 +11:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2015-03-06 01:46:01 +11:00
|
|
|
*
|
2016-03-11 12:48:19 +11:00
|
|
|
* Copyright 2012-2016 Danny Robson <danny@nerdcruft.net>
|
2015-03-06 01:46:01 +11:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __UTIL_COORD_BASE_HPP
|
|
|
|
#define __UTIL_COORD_BASE_HPP
|
|
|
|
|
|
|
|
#include "init.hpp"
|
2017-08-24 14:34:46 +10:00
|
|
|
|
|
|
|
#include "./ops.hpp"
|
2015-04-09 17:58:47 +10:00
|
|
|
#include "../maths.hpp"
|
2015-03-06 01:46:01 +11:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdlib>
|
2016-10-25 19:57:37 +11:00
|
|
|
#include <type_traits>
|
2015-03-06 01:46:01 +11:00
|
|
|
|
2017-01-05 15:06:49 +11:00
|
|
|
namespace util::coord {
|
2015-03-06 01:46:01 +11:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
2015-10-19 12:01:29 +11:00
|
|
|
template <
|
|
|
|
size_t S,
|
|
|
|
typename T,
|
|
|
|
template <size_t, typename> class KLASS,
|
|
|
|
typename ...tags
|
|
|
|
>
|
2016-04-28 16:06:48 +10:00
|
|
|
struct base : public init<S,T,tags...> {
|
2016-10-25 19:57:37 +11:00
|
|
|
static_assert (S > 0);
|
|
|
|
static_assert (std::is_arithmetic<T>::value);
|
2015-03-06 01:46:01 +11:00
|
|
|
|
2016-03-11 12:48:19 +11:00
|
|
|
using value_type = T;
|
2015-03-06 01:46:01 +11:00
|
|
|
static constexpr size_t dimension = S;
|
|
|
|
static constexpr size_t elements = S;
|
|
|
|
|
2017-08-24 14:34:22 +10:00
|
|
|
/// returns the number of elements we contain
|
2015-09-21 15:34:46 +10:00
|
|
|
static constexpr size_t size (void) { return S; }
|
2015-03-06 01:46:01 +11:00
|
|
|
|
2017-08-24 14:34:22 +10:00
|
|
|
// inherit the fancy elementwise constructors from `init'.
|
2015-03-06 01:46:01 +11:00
|
|
|
using init<S,T,tags...>::init;
|
2017-08-24 14:34:22 +10:00
|
|
|
|
|
|
|
/// constructs, but does not initialise, the data.
|
|
|
|
///
|
|
|
|
/// used to avoid unnecessary initialisation in many situations where
|
|
|
|
/// we have arrays of these types that are about to be overwritten. it
|
|
|
|
/// is a very important performance optimisation.
|
2015-03-06 01:46:01 +11:00
|
|
|
base () = default;
|
|
|
|
|
2017-08-24 14:34:22 +10:00
|
|
|
/// constructs an instance where all elements are initialised to `val'.
|
|
|
|
constexpr explicit
|
|
|
|
base (T val)
|
2016-08-10 17:33:45 +10:00
|
|
|
{ std::fill (begin (), end (), val); }
|
2015-03-06 01:46:01 +11:00
|
|
|
|
2015-10-19 12:01:29 +11:00
|
|
|
constexpr base (const base<S,T,KLASS,tags...> &rhs) = default;
|
2015-04-09 17:47:35 +10:00
|
|
|
base& operator= (const base<S,T,KLASS,tags...> &rhs) = default;
|
2015-03-06 01:46:01 +11:00
|
|
|
|
|
|
|
// element accessors
|
2015-10-19 12:01:29 +11:00
|
|
|
T& operator[] (size_t i) { return this->data[i]; }
|
|
|
|
constexpr const T& operator[] (size_t i) const { return this->data[i]; }
|
2015-03-06 01:46:01 +11:00
|
|
|
|
2016-09-21 22:23:20 +10:00
|
|
|
auto cbegin (void) const { return std::cbegin (this->data); }
|
|
|
|
auto cend (void) const { return std::cend (this->data); }
|
|
|
|
|
2015-10-19 12:01:29 +11:00
|
|
|
auto begin (void) const { return std::begin (this->data); }
|
|
|
|
auto end (void) const { return std::end (this->data); }
|
2015-03-06 01:46:01 +11:00
|
|
|
|
2015-10-19 12:01:29 +11:00
|
|
|
auto begin (void) { return std::begin (this->data); }
|
|
|
|
auto end (void) { return std::end (this->data); }
|
2015-03-11 22:58:14 +11:00
|
|
|
|
2016-08-10 18:26:40 +10:00
|
|
|
const T& front (void) const { return this->data[0]; }
|
|
|
|
T& front (void) { return this->data[0]; }
|
|
|
|
|
|
|
|
const T& back (void) const { return this->data[S-1]; }
|
|
|
|
T& back (void) { return this->data[S-1]; }
|
|
|
|
|
2015-04-09 17:47:35 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////
|
2015-03-11 22:58:14 +11:00
|
|
|
// conversions
|
|
|
|
template <template <size_t, typename> class K>
|
|
|
|
K<S,T> as (void) const
|
|
|
|
{
|
|
|
|
K<S,T> k;
|
|
|
|
std::copy (begin (), end (), k.begin ());
|
|
|
|
return k;
|
|
|
|
}
|
2015-04-09 17:58:47 +10:00
|
|
|
|
2015-09-21 15:34:46 +10:00
|
|
|
//---------------------------------
|
2015-04-09 17:58:47 +10:00
|
|
|
template <typename U>
|
|
|
|
KLASS<S,U>
|
|
|
|
cast (void) const
|
|
|
|
{
|
|
|
|
KLASS<S,U> out;
|
2017-06-13 14:18:00 +10:00
|
|
|
std::copy (std::cbegin (this->data),
|
|
|
|
std::cend (this->data),
|
|
|
|
std::begin (out.data));
|
2015-04-09 17:58:47 +10:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
2017-08-24 14:34:46 +10:00
|
|
|
/// returns an instance with the same data, but truncated to `D'
|
|
|
|
/// elements
|
|
|
|
///
|
|
|
|
/// explicitly does not allow a fill parameter given it can't be used
|
|
|
|
/// when reducing dimensions.
|
|
|
|
template <std::size_t D>
|
|
|
|
std::enable_if_t<
|
|
|
|
D <= S, KLASS<D,T>
|
|
|
|
>
|
2015-04-09 17:58:47 +10:00
|
|
|
redim (void) const
|
|
|
|
{
|
|
|
|
KLASS<D,T> out;
|
2017-08-24 14:34:46 +10:00
|
|
|
std::copy_n (cbegin (), D, std::begin (out.data));
|
2015-04-09 17:58:47 +10:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
2017-08-24 14:34:46 +10:00
|
|
|
/// returns an instance with the same data, but more elements, where
|
|
|
|
/// the new elements are initialised with values with the same index
|
|
|
|
/// in the coordinate `fill'.
|
|
|
|
///
|
|
|
|
/// explicitly requires a fill parameter so that we avoid undefined
|
|
|
|
/// values.
|
|
|
|
template <std::size_t D>
|
|
|
|
std::enable_if_t<
|
|
|
|
(D > S), KLASS<D,T>
|
|
|
|
>
|
2015-04-09 17:58:47 +10:00
|
|
|
redim (const KLASS<D,T> fill) const
|
|
|
|
{
|
|
|
|
KLASS<D,T> out;
|
|
|
|
|
2017-08-24 14:34:46 +10:00
|
|
|
auto next = std::copy (cbegin (), cend (), std::begin (out));
|
|
|
|
std::copy (std::cbegin (fill) + S, std::cend (fill), next);
|
2015-04-09 17:58:47 +10:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
2017-08-24 14:34:46 +10:00
|
|
|
/// returns an instance with the same data, but more elements, where
|
|
|
|
/// all the new elemenst are initialised with the scalar `fill'.
|
|
|
|
///
|
|
|
|
/// explicitly requires a fill parameter so that we avoid undefined
|
|
|
|
/// values.
|
|
|
|
template <std::size_t D>
|
|
|
|
std::enable_if_t<
|
|
|
|
(D > S), KLASS<D,T>
|
|
|
|
>
|
2015-04-09 17:58:47 +10:00
|
|
|
redim (T fill) const
|
|
|
|
{
|
|
|
|
KLASS<D,T> out;
|
|
|
|
|
2017-08-24 14:34:46 +10:00
|
|
|
auto next = std::copy (cbegin (), cend (), std::begin (out));
|
|
|
|
std::fill (next, std::end (out), fill);
|
2015-04-09 17:58:47 +10:00
|
|
|
return out;
|
|
|
|
}
|
2015-03-06 01:46:01 +11:00
|
|
|
};
|
2017-01-05 15:06:49 +11:00
|
|
|
}
|
2015-03-06 01:46:01 +11:00
|
|
|
|
|
|
|
|
|
|
|
#endif
|