File indexing completed on 2024-05-19 11:38:32

0001 /*
0002     Large image displaying library.
0003 
0004     Copyright (C) 2004,2005 Maks Orlovich (maksim@kde.org)
0005 
0006     Permission is hereby granted, free of charge, to any person obtaining a copy
0007     of this software and associated documentation files (the "Software"), to deal
0008     in the Software without restriction, including without limitation the rights
0009     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0010     copies of the Software, and to permit persons to whom the Software is
0011     furnished to do so, subject to the following conditions:
0012 
0013     The above copyright notice and this permission notice shall be included in
0014     all copies or substantial portions of the Software.
0015 
0016     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0017     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0018     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
0019     AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
0020     AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
0021     CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0022 
0023 */
0024 #ifndef ARRAY2D_H
0025 #define ARRAY2D_H
0026 
0027 namespace khtmlImLoad
0028 {
0029 
0030 /**
0031  A little helper template for nicer 2D arrays. This is in no way
0032  fancy, though, so this shouldn't be copied, etc.
0033 */
0034 template <typename T>
0035 class Array2D
0036 {
0037 private:
0038     T *data;
0039     unsigned int cols;
0040     unsigned int rows;
0041 public:
0042     Array2D(unsigned int _cols, unsigned int _rows):
0043         data(new T[_cols *_rows]), cols(_cols), rows(_rows)
0044     {}
0045 
0046     Array2D()
0047     {
0048         data = 0;
0049         rows = 0;
0050         cols = 0;
0051     }
0052 
0053     ~Array2D()
0054     {
0055         delete[] data;
0056     }
0057 
0058     //Note: this is meant to be used only on empty objects
0059     Array2D &operator=(const Array2D &other)
0060     {
0061         assert(data == 0);
0062         data = other.data;
0063         cols = other.cols;
0064         rows = other.rows;
0065         return *this;
0066     }
0067 
0068     T &at(unsigned int col, unsigned int row)
0069     {
0070         return data[row * cols + col];
0071     }
0072 
0073     const T &at(unsigned int col, unsigned int row) const
0074     {
0075         return data[row * cols + col];
0076     }
0077 private:
0078     Array2D(const Array2D &other);
0079 };
0080 
0081 }
0082 
0083 #endif