Added some more mathematical functions
[libs.git] / src / Sylph / Core / Util.h
bloba0fb6e6071b512d8b3cc1c9dd505e225d3515224
1 /*
2 * LibSylph Class Library
3 * Copyright (C) 2009 Frank "SeySayux" Erens <seysayux@gmail.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the LibSylph Pulbic License as published
7 * by the LibSylph Developers; either version 1.0 of the License, or
8 * (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LibSylph
13 * Public License for more details.
15 * You should have received a copy of the LibSylph Public License
16 * along with this Library, if not, contact the LibSylph Developers.
18 * Created on 9 december 2008, 15:34
21 #ifndef UTIL_H_
22 #define UTIL_H_
24 #include "Object.h" // macros
25 #include "Exception.h"
26 #include "Array.h"
28 #include <math.h>
30 SYLPH_BEGIN_NAMESPACE
31 SYLPH_PUBLIC
33 template<class T>
34 inline void carraycopy(const T src[], std::size_t srcPos, T dest[],
35 std::size_t destPos, std::size_t length) throw (Exception) {
36 for (int i = 0; i < length; i++) {
37 dest[destPos + i] = src[srcPos + i];
41 template<class T>
42 inline void arraycopy(const Array<T> & src, std::size_t srcPos, Array<T> & dest,
43 std::size_t destPos, std::size_t length) throw (Exception) {
45 std::size_t srcSize = src.length;
46 std::size_t destSize = dest.length;
47 if (srcPos + length > srcSize) sthrow(ArrayException, "Source array too short");
48 if (destPos + length > destSize) sthrow(ArrayException, "Dest array too short");
50 for (int i = 0; i < length; i++) {
51 dest[destPos + i] = src[srcPos + i];
55 template <typename T, std::size_t N>
56 inline std::size_t carraysize(T(&)[N]) {
57 return N;
60 template<class T, class U = T>
61 inline U abs(T t) {
62 return t > 0 ? t : -t;
65 template<>
66 inline unsigned int abs<int, unsigned int>(int t) {
67 return t > 0 ? t : -t;
70 template<>
71 inline unsigned long abs<long, unsigned long>(long t) {
72 return t > 0 ? t : -t;
75 template<>
76 inline unsigned short abs<short, unsigned short>(short t) {
77 return t > 0 ? t : -t;
80 template<class T>
81 inline signed char sign(T t) {
82 return t > 0? : t == 0? 0 : -1;
85 template<>
86 inline signed char sign<double>(double t) {
87 return copysign(1.0,t);
91 template<>
92 inline signed char sign<float>(float t) {
93 return copysignf(1.0f, t);
97 SYLPH_END_NAMESPACE
99 #endif /* UTIL_H_ */