medfall

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 19a9bfa46900630e998bb5d8fa82a6e2a3fa2077
parent 27bcb570f0918d4c6fc0595ea246a285f4c58db7
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Wed Sep 14 16:55:46 +0100

Add array.h

Diffstat:
array.h | 78++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 78 insertions(+), 0 deletions(-)
diff --git a/array.h b/array.h @@ -0,0 +1,78 @@ +#ifndef _ARRAY_H_ +#define _ARRAY_H_ + +#include "intrinsics.h" + +template< typename T > +class array { +public: + array( T * memory, size_t size ) : n( size ) { + elems = memory; + } + + T & operator[]( size_t i ) { + assert( i < n ); + return elems[ i ]; + } + + const T & operator[]( size_t i ) const { + assert( i < n ); + return elems[ i ]; + } + + T * operator+( size_t i ) { + assert( i < n ); + return &elems[ i ]; + } + + const T * operator+( size_t i ) const { + assert( i < n ); + return &elems[ i ]; + } + + bool in_range( size_t i ) const { + return i < n; + } + + const T * ptr() const { + return elems; + } + + const size_t n; + +private: + T * elems; +}; + +template< typename T > +class array2d { +public: + array2d( T * memory, size_t width, size_t height ) : w( width ), h( height ) { + elems = memory; + } + + T & operator()( size_t x, size_t y ) { + assert( in_range( x, y ) ); + return elems[ y * w + x ]; + } + + const T & operator()( size_t x, size_t y ) const { + assert( in_range( x, y ) ); + return elems[ y * w + x ]; + } + + bool in_range( size_t x, size_t y ) const { + return x < w && y < h; + } + + const T * ptr() const { + return elems; + } + + const size_t w, h; + +private: + T * elems; +}; + +#endif // _ARRAY_H_