Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
array.h
1 /*
2  * Fast array utilities.
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2011, IRIT UPS.
6  *
7  * OTAWA is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * OTAWA is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with OTAWA; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #ifndef ELM_ARRAY_H_
23 #define ELM_ARRAY_H_
24 
25 #include <new>
26 #include <string.h>
27 #include <elm/meta.h>
28 #include <elm/type_info.h>
29 #include <elm/util/misc.h>
30 
31 namespace elm {
32 
33 namespace array {
34 
35 // fast copies
36 template <class T> class fast {
37 public:
38  static inline void copy(T *target, const T *source, int size)
39  { ::memcpy(target, source, size * sizeof(T)); }
40  static inline void move(T *target, const T *source, int size)
41  { ::memmove(target, source, size * sizeof(T)); }
42  static inline void clear(T *target, int size)
43  { ::memset(target, 0, size * sizeof(T)); }
44  static inline bool equals(const T* t1, const T* t2, int size)
45  { return ::memcmp(t1, t2, size) == 0; }
46  static inline void construct(T *t, int size) { }
47  static inline void destruct(T *t, int size) { }
48 };
49 
50 // slow copies (cause of constructor, destructor, etc)
51 template <class T> class slow {
52 public:
53  static inline void copy(T *target, const T *source, int size)
54  { for(int i = 0; i < size; i++) target[i] = source[i]; }
55  static inline void copy_back(T *target, const T *source, int size)
56  { for(int i = size - 1; i >= 0; i--) target[i] = source[i]; }
57  static inline void move(T *target, const T *source, int size)
58  { if(target < source) copy(target, source, size); else copy_back(target, source, size); }
59  static inline void clear(T *target, int size)
60  { for(int i = 0; i < size; i++) target[i] = T(); }
61  static inline bool equals(const T* t1, const T* t2, int size)
62  { for(int i = 0; i < size; i++) if(!(t1[i] == t2[i])) return false; return true; }
63  static inline void construct(T *t, int size)
64  { for(int i = 0; i < size; i++) ::new((void *)(t + i)) T(); }
65  static inline void destruct(T *t, int size)
66  { for(int i = 0; i < size; i++) (t + i)->~T(); }
67 };
68 
69 // copy definitions
70 template <class T> inline void copy(T *target, const T *source, int size)
71  { _if<type_info<T>::is_deep, slow<T>, fast<T> >::copy(target, source, size); }
72 template <class T> inline void copy_back(T *target, const T *source, int size)
73  { for(int i = size - 1; i >= 0; i--) target[i] = source[i]; }
74 template <class T> inline void move(T *target, const T *source, int size)
75  { _if<type_info<T>::is_deep, slow<T>, fast<T> >::move(target, source, size); }
76 template <class T> inline void set(T *target, int size, const T& v)
77  { for(int i = 0; i < size; i++) target[i] = v; }
78 template <class T> inline void clear(T *target, int size)
80 template <class T> inline bool equals(const T* t1, const T* t2, int size)
82 template <class T> inline void construct(T *t, int size)
84 template <class T> inline void destruct(T *t, int size)
86 inline void copy(cstring *d, cstring *a, int s)
87  { copy(reinterpret_cast<char *>(d), reinterpret_cast<char *>(a), sizeof(cstring) * s); }
88 inline void move(cstring *d, cstring *a, int s)
89  { move(reinterpret_cast<char *>(d), reinterpret_cast<char *>(a), sizeof(cstring) * s); }
90 inline void clear(cstring *d, int s)
91  { clear(reinterpret_cast<char *>(d), sizeof(cstring) * s); }
92 
93 
94 // other useful operations
95 template <class T> void reverse(T *a, int n)
96  { for(int i = 0; i < n / 2; i++) swap(a[i], a[n - 1 - i]); }
97 
98 } } // elm::array
99 
100 #endif /* ELM_H_ */
elm::array::copy
void copy(T *target, const T *source, int size)
Definition: array.h:70
elm::_if
Definition: meta.h:43
elm::array::slow
Definition: array.h:51
elm::array::copy_back
void copy_back(T *target, const T *source, int size)
Definition: array.h:72
elm::array::fast
Definition: array.h:36
elm::array::slow::equals
static bool equals(const T *t1, const T *t2, int size)
Definition: array.h:61
elm::array::fast::equals
static bool equals(const T *t1, const T *t2, int size)
Definition: array.h:44
elm::array::fast::destruct
static void destruct(T *t, int size)
Definition: array.h:47
elm::array::slow::copy
static void copy(T *target, const T *source, int size)
Definition: array.h:53
elm::array::slow::destruct
static void destruct(T *t, int size)
Definition: array.h:65
elm::array::equals
bool equals(const T *t1, const T *t2, int size)
Definition: array.h:80
elm::CString
Definition: CString.h:17
elm::array::fast::construct
static void construct(T *t, int size)
Definition: array.h:46
elm::array::clear
void clear(T *target, int size)
Definition: array.h:78
elm
Definition: adapter.h:26
elm::array::set
void set(T *target, int size, const T &v)
Definition: array.h:76
elm::t::size
uint64 size
Definition: arch.h:35
array
elm::array::slow::construct
static void construct(T *t, int size)
Definition: array.h:63
elm::array::slow::clear
static void clear(T *target, int size)
Definition: array.h:59
elm::array::move
void move(T *target, const T *source, int size)
Definition: array.h:74
elm::array::construct
void construct(T *t, int size)
Definition: array.h:82
elm::array::fast::copy
static void copy(T *target, const T *source, int size)
Definition: array.h:38
elm::array::slow::copy_back
static void copy_back(T *target, const T *source, int size)
Definition: array.h:55
elm::swap
void swap(T &x, T &y)
Definition: misc.h:27
elm::array::destruct
void destruct(T *t, int size)
Definition: array.h:84
elm::array::slow::move
static void move(T *target, const T *source, int size)
Definition: array.h:57
elm::array::fast::clear
static void clear(T *target, int size)
Definition: array.h:42
elm::array::fast::move
static void move(T *target, const T *source, int size)
Definition: array.h:40
elm::array::reverse
void reverse(T *a, int n)
Definition: array.h:95