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
|
|
|
*
|
|
|
|
* Copyright 2012-2015 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __UTIL_COORD_BASE_HPP
|
|
|
|
#define __UTIL_COORD_BASE_HPP
|
|
|
|
|
|
|
|
#include "init.hpp"
|
2015-04-09 17:58:47 +10:00
|
|
|
#include "../maths.hpp"
|
2015-03-06 01:46:01 +11:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdlib>
|
|
|
|
|
|
|
|
namespace util { namespace coord {
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
2015-04-09 17:47:35 +10:00
|
|
|
template <size_t S, typename T, template <size_t, typename> class KLASS, typename ...tags>
|
2015-03-06 01:46:01 +11:00
|
|
|
struct base : public init <S,T,tags...> {
|
|
|
|
static_assert (S > 0, "coord dimensions must be strictly positive");
|
|
|
|
|
|
|
|
typedef T value_type;
|
|
|
|
static constexpr size_t dimension = S;
|
|
|
|
static constexpr size_t elements = S;
|
|
|
|
|
2015-09-21 15:34:46 +10:00
|
|
|
static constexpr size_t size (void) { return S; }
|
2015-03-06 01:46:01 +11:00
|
|
|
|
|
|
|
// constructors
|
|
|
|
using init<S,T,tags...>::init;
|
|
|
|
base () = default;
|
|
|
|
|
2015-07-21 01:38:36 +10:00
|
|
|
constexpr explicit base (T val)
|
2015-04-09 21:50:22 +10:00
|
|
|
{ std::fill (std::begin (this->data), std::end (this->data), val); }
|
2015-03-06 01:46:01 +11:00
|
|
|
|
2015-09-09 18:37:26 +10:00
|
|
|
constexpr explicit 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
|
|
|
|
T& operator[] (size_t i) { return this->data[i]; }
|
|
|
|
T operator[] (size_t i) const { return this->data[i]; }
|
|
|
|
|
|
|
|
const T* begin (void) const { return std::begin (this->data); }
|
|
|
|
const T* end (void) const { return std::end (this->data); }
|
|
|
|
|
|
|
|
T* begin (void) { return std::begin (this->data); }
|
|
|
|
T* end (void) { return std::end (this->data); }
|
2015-03-11 22:58:14 +11:00
|
|
|
|
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;
|
|
|
|
std::copy (std::begin (this->data),
|
|
|
|
std::end (this->data),
|
|
|
|
std::begin (out.data));
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
// redimension
|
|
|
|
template <size_t D>
|
|
|
|
KLASS<D,T>
|
|
|
|
redim (void) const
|
|
|
|
{
|
|
|
|
KLASS<D,T> out;
|
|
|
|
std::copy_n (std::begin (this->data),
|
|
|
|
min (S, D),
|
|
|
|
std::begin (out.data));
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
template<size_t D>
|
|
|
|
KLASS<D,T>
|
|
|
|
redim (const KLASS<D,T> fill) const
|
|
|
|
{
|
|
|
|
KLASS<D,T> out;
|
|
|
|
|
|
|
|
static constexpr auto L1 = min (S, D);
|
|
|
|
static constexpr auto L2 = D - L1;
|
|
|
|
|
|
|
|
std::copy_n (std::begin (this->data),
|
|
|
|
L1,
|
|
|
|
std::begin (out.data));
|
|
|
|
|
|
|
|
std::copy_n (fill.data + L1,
|
|
|
|
L2,
|
|
|
|
out.data + L1);
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
template <size_t D>
|
|
|
|
KLASS<D,T>
|
|
|
|
redim (T fill) const
|
|
|
|
{
|
|
|
|
KLASS<D,T> out;
|
|
|
|
|
|
|
|
auto cursor = std::copy_n (std::begin (this->data),
|
|
|
|
min (S, D),
|
|
|
|
std::begin (out.data));
|
|
|
|
std::fill (cursor, std::end (out.data), fill);
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
2015-03-06 01:46:01 +11:00
|
|
|
};
|
|
|
|
} }
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|