Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ©2014 Joseph Burger
- // All rights reserved
- #ifndef _MATRIX_H_
- #define _MATRIX_H_
- /*
- //class matrix is a wrapper for a vector of integers which
- //allows matrix objects to behave conveniently with coordinate pairs.
- //For example, if you want a 10 by 10 array, and want to
- //then access element 0,0, it's as easy as:
- //matrix<int> m(10, 10);
- //m(0, 0) = 5; // m(0) would work as well.
- //class matrix can turn an integer between 0 and length * width
- //into a coordinate pair, like this:
- //m(12) represents m(2, 1).
- //m.twoD(12) == pair<int>(2, 1), and,
- //m.oneD(2, 1) == m.oneD(pair<int>(2, 1)) == 12
- //EDIT: Changed matrix to a template class (cuz' templates == awesome)
- */
- template <class T>
- class matrix
- {
- protected:
- vector<T> arr;
- int length, width;
- public:
- int& getLength() { return length; }
- int& getWidth() { return width; }
- matrix(){};
- // constructor
- matrix(int x, int y){
- this->length = x, this->width = y;
- for (int i = 0; i < (x * y); i++)
- this->arr.push_back(T());
- }
- // copy contstructor
- matrix(const matrix& other){
- this->length = other.length, this->width = other.width;
- this->arr = other.arr;
- }
- // get() for direct manipulation (for_each() )
- vector<int>& get(){ return arr; }
- // Takes a pair and returns an int
- int oneD(pair<int, int> p){ return p.second * width + p.first; }
- int oneD(int x, int y){ return oneD(pair<int, int>(x, y)); }
- // Takes an int and returns a pair
- pair<int, int> twoD(int compositeCoord){ return pair<int, int> (compositeCoord % length, compositeCoord / width); }
- // Access to the internal vector (These are the good parts :D )
- T& operator()(int x, int y){ return this->arr[oneD(x, y)]; }
- T& operator()(pair<int, int> p){ return operator()(p.first, p.second); }
- T& operator()(int compositeCoordinate) { return this->arr[oneD(twoD(compositeCoordinate))]; }
- };
- #endif
- /*
- int _tmain(int argc, _TCHAR* argv[])
- {
- matrix mat(10, 10);
- mat(4, 5) = 54;
- cout << "Element 54, or (4, 5) is " << mat(54) << endl;
- cout << "Retrieving element 54 using a coordinate pair: (4, 5): " << mat(4, 5) << endl;
- pair<int, int> imacoordpair(4, 5);
- cout << "Retrieving element (4, 5) using std::pair(): " << mat(imacoordpair) << endl;
- cout << "\ntesting with another matrix\n";
- matrix matty(100, 100);
- cout << "\nSetting matty(1000) to 2, and matty(99, 99) to 5:\n";
- matty(1000) = 2;
- matty(99, 99) = 5;
- cout << "\tmatty(1000) = ";
- cout << matty(1000) << endl;
- cout << "convert 9,999 to a coordinate pair using member functions:\n";
- imacoordpair = matty.twoD(9999);
- cout << "\timacoordpair.first: " << imacoordpair.first << " imacoordpair.second = " << imacoordpair.second << endl;
- cout << "convert 1,001 to a coordinate pair using member functions:\n";
- imacoordpair = matty.twoD(1001);
- cout << "\timacoordpair.first: " << imacoordpair.first << " imacoordpair.second = " << imacoordpair.second << endl;
- cout << "What's the single-int representation of 99, 99?\n";
- cout << "\t" << matty.oneD(99, 99);
- cout << "\nWhat's inside matty(99, 99)? \n\t" << matty(matty.oneD(99, 99)) << endl;
- return 0;
- }
- */
- // output:
- /*
- Element 54, or (4, 5) is 54
- Retrieving element 54 using a coordinate pair: (4, 5): 54
- Retrieving element (4, 5) using std::pair(): 54
- testing with another matrix
- Setting matty(1000) to 2, and matty(99, 99) to 5:
- matty(1000) = 2
- convert 9,999 to a coordinate pair using member functions:
- imacoordpair.first: 99 imacoordpair.second = 99
- convert 1,001 to a coordinate pair using member functions:
- imacoordpair.first: 1 imacoordpair.second = 10
- What's the single-int representation of 99, 99?
- 9999
- What's inside matty(99, 99)?
- 5
- Press any key to continue . . .
- */
- // ©2014 Joseph Burger
- // All rights reserved
Advertisement
Add Comment
Please, Sign In to add comment