candyapplecorn

class Matrix

Jun 30th, 2014
473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.77 KB | None | 0 0
  1. // ©2014 Joseph Burger
  2. // All rights reserved
  3. #ifndef _MATRIX_H_
  4. #define _MATRIX_H_
  5. /*
  6. //class matrix is a wrapper for a vector of integers which
  7. //allows matrix objects to behave conveniently with coordinate pairs.
  8. //For example, if you want a 10 by 10 array, and want to
  9. //then access element 0,0, it's as easy as:
  10. //matrix<int> m(10, 10);
  11. //m(0, 0) = 5; // m(0) would work as well.
  12. //class matrix can turn an integer between 0 and length * width
  13. //into a coordinate pair, like this:
  14. //m(12) represents m(2, 1).
  15. //m.twoD(12) == pair<int>(2, 1), and,
  16. //m.oneD(2, 1) == m.oneD(pair<int>(2, 1)) == 12
  17.  
  18. //EDIT: Changed matrix to a template class (cuz' templates == awesome)
  19. */
  20. template <class T>
  21. class matrix
  22. {
  23. protected:
  24.     vector<T> arr;
  25.     int length, width;
  26. public:
  27.     int& getLength() { return length; }
  28.     int& getWidth() { return width; }
  29.     matrix(){};
  30.     // constructor
  31.     matrix(int x, int y){
  32.         this->length = x, this->width = y;
  33.         for (int i = 0; i < (x * y); i++)
  34.             this->arr.push_back(T());
  35.     }
  36.     // copy contstructor
  37.     matrix(const matrix& other){
  38.         this->length = other.length, this->width = other.width;
  39.         this->arr = other.arr;
  40.     }
  41.     // get() for direct manipulation (for_each() )
  42.     vector<int>& get(){ return arr; }
  43.     // Takes a pair and returns an int
  44.     int oneD(pair<int, int> p){ return p.second * width + p.first; }
  45.     int oneD(int x, int y){ return oneD(pair<int, int>(x, y)); }
  46.     // Takes an int and returns a pair
  47.     pair<int, int> twoD(int compositeCoord){ return pair<int, int> (compositeCoord % length, compositeCoord / width); }
  48.     // Access to the internal vector (These are the good parts :D )
  49.     T& operator()(int x, int y){ return this->arr[oneD(x, y)]; }
  50.     T& operator()(pair<int, int> p){ return operator()(p.first, p.second); }
  51.     T& operator()(int compositeCoordinate) { return this->arr[oneD(twoD(compositeCoordinate))]; }
  52. };
  53.  
  54. #endif
  55.  
  56. /*
  57. int _tmain(int argc, _TCHAR* argv[])
  58. {
  59.     matrix mat(10, 10);
  60.     mat(4, 5) = 54;
  61.     cout << "Element 54, or (4, 5) is " << mat(54) << endl;
  62.     cout << "Retrieving element 54 using a coordinate pair: (4, 5): " << mat(4, 5) << endl;
  63.     pair<int, int> imacoordpair(4, 5);
  64.     cout << "Retrieving element (4, 5) using std::pair(): " << mat(imacoordpair) << endl;
  65.     cout << "\ntesting with another matrix\n";
  66.     matrix matty(100, 100);
  67.     cout << "\nSetting matty(1000) to 2, and matty(99, 99) to 5:\n";
  68.     matty(1000) = 2;
  69.     matty(99, 99) = 5;
  70.     cout << "\tmatty(1000) = ";
  71.     cout << matty(1000) << endl;
  72.     cout << "convert 9,999 to a coordinate pair using member functions:\n";
  73.     imacoordpair = matty.twoD(9999);
  74.     cout << "\timacoordpair.first: " << imacoordpair.first << " imacoordpair.second = " << imacoordpair.second << endl;
  75.     cout << "convert 1,001 to a coordinate pair using member functions:\n";
  76.     imacoordpair = matty.twoD(1001);
  77.     cout << "\timacoordpair.first: " << imacoordpair.first << " imacoordpair.second = " << imacoordpair.second << endl;
  78.     cout << "What's the single-int representation of 99, 99?\n";
  79.     cout << "\t" << matty.oneD(99, 99);
  80.     cout << "\nWhat's inside matty(99, 99)? \n\t" << matty(matty.oneD(99, 99)) << endl;
  81.  
  82.     return 0;
  83. }
  84. */
  85. // output:
  86. /*
  87. Element 54, or (4, 5) is 54
  88. Retrieving element 54 using a coordinate pair: (4, 5): 54
  89. Retrieving element (4, 5) using std::pair(): 54
  90.  
  91. testing with another matrix
  92.  
  93. Setting matty(1000) to 2, and matty(99, 99) to 5:
  94. matty(1000) = 2
  95. convert 9,999 to a coordinate pair using member functions:
  96. imacoordpair.first: 99 imacoordpair.second = 99
  97. convert 1,001 to a coordinate pair using member functions:
  98. imacoordpair.first: 1 imacoordpair.second = 10
  99. What's the single-int representation of 99, 99?
  100. 9999
  101. What's inside matty(99, 99)?
  102. 5
  103. Press any key to continue . . .
  104. */
  105. // ©2014 Joseph Burger
  106. // All rights reserved
Advertisement
Add Comment
Please, Sign In to add comment