Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
array2.h
Go to the documentation of this file.
1 
18 #ifndef ION_BASE_ARRAY2_H_
19 #define ION_BASE_ARRAY2_H_
20 
21 #include "ion/base/allocatable.h"
22 #include "ion/base/invalid.h"
23 #include "ion/base/logging.h"
25 
26 namespace ion {
27 namespace base {
28 
31 template <typename T> class Array2 : public Allocatable {
32  public:
34  Array2() : width_(0), height_(0), data_(GetAllocator()) {}
35 
38  Array2(size_t width, size_t height)
39  : width_(width), height_(height), data_(GetAllocator()) {
40  data_.resize(width * height);
41  }
42 
45  Array2(size_t width, size_t height, const T& initial_value)
46  : width_(width),
47  height_(height),
48  data_(GetAllocator(), width * height, initial_value) {
49  }
50 
51  ~Array2() override {}
52 
53  size_t GetWidth() const { return width_; }
54  size_t GetHeight() const { return height_; }
55  size_t GetSize() const { return data_.size(); }
56 
59  bool Set(size_t column, size_t row, const T& val) {
60  const size_t index = GetIndex(column, row);
61  if (index == base::kInvalidIndex) {
62  return false;
63  } else {
64  data_[index] = val;
65  return true;
66  }
67  }
68 
71  const T& Get(size_t column, size_t row) const {
72  const size_t index = GetIndex(column, row);
73  return index == base::kInvalidIndex ?
74  base::InvalidReference<T>() : data_[index];
75  }
76 
79  T* GetMutable(size_t column, size_t row) {
80  const size_t index = GetIndex(column, row);
81  return index == base::kInvalidIndex ? NULL : &data_[index];
82  }
83 
84  private:
87  size_t GetIndex(size_t column, size_t row) const {
88  if (column >= width_ || row >= height_) {
89  LOG(ERROR) << "Bad indices (" << column << ", " << row
90  << ") for Array2 of size " << width_ << " x " << height_;
91  return base::kInvalidIndex;
92  }
93  return row * width_ + column;
94  }
95 
96  size_t width_;
97  size_t height_;
98  AllocVector<T> data_;
99 };
100 
101 } // namespace base
102 } // namespace ion
103 
104 #endif // ION_BASE_ARRAY2_H_
const T & Get(size_t column, size_t row) const
Returns the indexed element of the array.
Definition: array2.h:71
const size_t kInvalidIndex
kInvalidIndex is a size_t value that is very unlikely to be a valid index.
Definition: invalid.cc:23
size_t GetSize() const
Definition: array2.h:55
#define LOG(severity)
Logs the streamed message unconditionally with a severity of severity.
Definition: logging.h:216
~Array2() override
Definition: array2.h:51
size_t GetWidth() const
Definition: array2.h:53
Simple rectangular 2D array class with range-checked indexing, templatized by the element type...
Definition: array2.h:31
Array2()
The default constructor creates an empty (0x0) array.
Definition: array2.h:34
Allocatable is an abstract base class for classes whose memory is managed by an Allocator.
Definition: allocatable.h:60
bool Set(size_t column, size_t row, const T &val)
Sets one element of the array.
Definition: array2.h:59
T * GetMutable(size_t column, size_t row)
Returns a pointer the indexed element of the array.
Definition: array2.h:79
Copyright 2016 Google Inc.
int width
Array2(size_t width, size_t height, const T &initial_value)
Constructor that creates an array of specified size with elements all set to initial_value.
Definition: array2.h:45
const AllocatorPtr & GetAllocator() const
Returns the Allocator that was used for the instance.
Definition: allocatable.h:68
size_t GetHeight() const
Definition: array2.h:54
Array2(size_t width, size_t height)
Constructor that creates an array of specified size with undefined elements.
Definition: array2.h:38