From 842b2c91c85eadf3e9a62d35f359bd7bc50f679e Mon Sep 17 00:00:00 2001 From: Robert Vollmert Date: Sun, 15 Nov 2009 19:33:08 +0100 Subject: Rename fixary.h and fixvec.h. --- crawl-ref/source/fixedarray.h | 136 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 crawl-ref/source/fixedarray.h (limited to 'crawl-ref/source/fixedarray.h') diff --git a/crawl-ref/source/fixedarray.h b/crawl-ref/source/fixedarray.h new file mode 100644 index 0000000000..c1dabfd040 --- /dev/null +++ b/crawl-ref/source/fixedarray.h @@ -0,0 +1,136 @@ +/* + * File: fixedarray.h + * Summary: Fixed size 2D vector class that asserts if you do something bad. + * Written by: Jesse Jones + */ + +#ifndef FIXARY_H +#define FIXARY_H + +#include "fixedvector.h" + + +// ========================================================================== +// class FixedArray +// ========================================================================== +template class FixedArray { + +//----------------------------------- +// Types +// +public: + typedef TYPE value_type; + typedef TYPE& reference; + typedef const TYPE& const_reference; + typedef TYPE* pointer; + typedef const TYPE* const_pointer; + + typedef unsigned long size_type; + typedef long difference_type; + + // operator[] should return one of these to avoid breaking + // client code (if inlining is on there won't be a speed hit) + typedef FixedVector Column; + +//----------------------------------- +// Initialization/Destruction +// +public: + ~FixedArray() {} + + FixedArray() {} + + FixedArray(TYPE def) + { + init(def); + } + +//----------------------------------- +// API +// +public: + // ----- Size ----- + bool empty() const { return WIDTH == 0 || HEIGHT == 0; } + int size() const { return WIDTH*HEIGHT; } + int width() const { return WIDTH; } + int height() const { return HEIGHT; } + + // ----- Access ----- + Column& operator[](unsigned long index) { return mData[index]; } + const Column& operator[](unsigned long index) const { + return mData[index]; + } + + template TYPE& operator () (const Indexer &i) { + return mData[i.x][i.y]; + } + + template const TYPE& operator () (const Indexer &i) const { + return mData[i.x][i.y]; + } + + void init(const TYPE& def) { + for ( int i = 0; i < WIDTH; ++i ) + mData[i].init(def); + } + +protected: + FixedVector mData; +}; + +// A fixed array centered around the origin. +template class SquareArray { +//----------------------------------- +// Types +// +public: + typedef TYPE value_type; + typedef TYPE& reference; + typedef const TYPE& const_reference; + typedef TYPE* pointer; + typedef const TYPE* const_pointer; + + typedef unsigned long size_type; + typedef long difference_type; + +//----------------------------------- +// Initialization/Destruction +// +public: + ~SquareArray() {} + + SquareArray() {} + + SquareArray(TYPE def) + { + init(def); + } + +//----------------------------------- +// API +// +public: + // ----- Size ----- + bool empty() const { return data.empty(); } + int size() const { return data.size(); } + int width() const { return data.width(); } + int height() const { return data.height(); } + + // ----- Access ----- + template TYPE& operator () (const Indexer &i) { + return data[i.x+RADIUS][i.y+RADIUS]; + } + + template const TYPE& operator () (const Indexer &i) const { + return data[i.x+RADIUS][i.y+RADIUS]; + } + + void init(const TYPE& def) { + data.init(def); + } + +protected: + FixedArray data; +}; + +#endif // FIXARY_H -- cgit v1.2.3-54-g00ecf