8000 Added Array.h. · coderforlife/ms-compress@741130c · GitHub
[go: up one dir, main page]

Skip to content

Commit 741130c

Browse files
committed
Added Array.h.
1 parent bda1423 commit 741130c

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

include/mscomp/Array.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// ms-compress: implements Microsoft compression algorithms
2+
// Copyright (C) 2012 Jeffrey Bush jeff@coderforlife.com
3+
//
4+
// This library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
17+
18+
/////////////////// Array /////////////////////////////////////////////////////
19+
// A simple array class that uses templates to determine if it is stack or heap
20+
// allocated.
21+
22+
// All of the std::array functions could be added to these, but really they
23+
// aren't necessary for this simple usage. Also, some explicit cast operators
24+
// would be nice...
25+
26+
#ifndef MSCOMP_XPRESS_ARRAY_H
27+
#define MSCOMP_XPRESS_ARRAY_H
28+
#include "internal.h"
29+
30+
template<typename T, size_t N, bool UseStack = false>
31+
class Array { private: Array(); };
32+
33+
34+
template<typename T, size_t N>
35+
class Array<T, N, false>
36+
{
37+
// Heap-allocated version
38+
T* x;
39+
public:
40+
FORCE_INLINE Array() : x((T*)malloc(N*sizeof(T))) { }
41+
FORCE_INLINE ~Array() { free(x); }
42+
FORCE_INLINE T& operator[] (size_t i) { return this->x[i]; }
43+
FORCE_INLINE const T& operator[] (size_t i) const { return this->x[i]; }
44+
FORCE_INLINE T* data() { return this->x; }
45+
FORCE_INLINE const T* data() const { return this->x; }
46+
};
47+
48+
template<typename T, size_t N>
49+
class Array<T, N, true>
50+
{
51+
// Stack-allocated version (no constructor or destructor)
52+
T x[N];
53+
public:
54+
FORCE_INLINE T& operator[] (size_t i) { return this->x[i]; }
55+
FORCE_INLINE const T& operator[] (size_t i) const { return this->x[i]; }
56+
FORCE_INLINE T* data() { return this->x; }
57+
FORCE_INLINE const T* data() const { return this->x; }
58+
};
59+
60+
#endif

0 commit comments

Comments
 (0)
0