Advertisement
Guest User

Untitled

a guest
May 3rd, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.02 KB | None | 0 0
  1. /*
  2.  
  3. Ashley Vu PIC 10B Intermediate Programming
  4. ID: 704341775 Spring 2016
  5. Email: avu5@ucla.edu Assignment #4
  6. Section: 4C
  7.  
  8. */
  9.  
  10. #ifndef templated_vector_h
  11. #define templated_vector_h
  12.  
  13. #include <iostream>
  14. #include <vector>
  15. #include <string>
  16. #include <cassert>
  17. #include <iomanip>
  18. #include <cmath>
  19.  
  20.  
  21.  
  22. // Interface
  23. template<typename T>
  24. class MyVector {
  25.  
  26.  
  27. public:
  28. // 1. Default constructor
  29. MyVector(){
  30. std::cout << "Standard constructor called..." << std::endl;
  31. currentCapacity = INITIAL_SIZE;
  32. numItems = 0;
  33. theData = new T[INITIAL_SIZE];
  34. }
  35.  
  36. // 2. Destructor
  37. ~MyVector(){
  38. std::cout << "Destructor called. Releasing memory..." << std::endl;
  39. delete[] theData;
  40. theData = NULL ;
  41. }
  42.  
  43. // 3. Copy constructor
  44. MyVector( const MyVector& b ){
  45. std::cout << "Copy constructor called..." << std::endl;
  46. // shallow copy non-pointers
  47. currentCapacity = b.currentCapacity;
  48. numItems = b.numItems;
  49.  
  50. // deep copy pointers
  51. if ( b.theData ){ // same as if ( b.theData != NULL )
  52. theData = new T[b.currentCapacity];
  53.  
  54. for (int i=0 ; i < b.numItems ; i++ )
  55. theData[i] = b.theData[i];
  56.  
  57. }
  58. else
  59. theData = NULL;
  60.  
  61. }
  62.  
  63. // 4. Operator=. The return is by reference to allow for chaining.
  64. MyVector& operator=( const MyVector& b ){
  65. std::cout << "Operator= called..." << std::endl;
  66. // check for self-assignment
  67. if ( this == &b )
  68. return *this;
  69.  
  70. // de-allocate memory in current object
  71. delete[] this->theData;
  72.  
  73. // shallow copy non-pointers
  74. currentCapacity = b.currentCapacity;
  75. numItems = b.numItems;
  76.  
  77. // deep copy pointers (if necessary)
  78. if ( b.theData ){ // same as if ( b.theData != NULL )
  79. theData = new T[b.currentCapacity];
  80.  
  81. for ( int i=0 ; i < b.numItems ; i++ )
  82. theData[i] = b.theData[i];
  83.  
  84. }
  85. else
  86. theData = NULL;
  87.  
  88. return *this;
  89.  
  90. }
  91.  
  92.  
  93. // OTHER MEMBER FUNCTIONS
  94.  
  95. // Pushes back paramater into vector
  96. void push_back( T newValue ){
  97. // make sure there is enough space
  98. if ( numItems == currentCapacity )
  99. reserve( currentCapacity + 1 );
  100.  
  101. theData[numItems] = newValue; // same as *(theData + numItems) = newValue
  102. numItems++; //updates numItems
  103.  
  104. return;
  105. }
  106.  
  107. // Returns number of items in vector
  108. int size() const{
  109. return numItems;
  110. }
  111.  
  112. // Setter for operator[] -- sets value of index in vector
  113. T& operator[](int index){
  114. assert((index>=0) && (index < numItems));
  115. return theData[index];
  116. }
  117.  
  118. // Getter for operator[] -- returns value of index in vector
  119. const T& operator[](int index) const{
  120. assert((index>=0) && (index < numItems));
  121. return theData[index];
  122. }
  123.  
  124. // Sums value of index i in LHS vector with value of index i in RHS vector
  125. MyVector operator+( const MyVector& b) {
  126. assert(b.size() == size());
  127.  
  128. MyVector<T> newVec; //create new vector to store sum values
  129. for (int i=0; i <b.size() ; i++)
  130. newVec.push_back( theData[i] + b[i] );
  131. return newVec;
  132. }
  133.  
  134. // Sums values of index i in LHS vector with value of index i in RHS vector; updates implicit vector with sums
  135. MyVector& operator+=( const MyVector& b) {
  136. assert(b.size() == size());
  137.  
  138. for (int i=0; i <b.size() ; i++)
  139. theData[i] += b[i];
  140. return *this;
  141. }
  142.  
  143.  
  144. // Normalization function
  145. T normal() const
  146. {
  147. return sqrt((*this) * (*this)); //prevents negatives
  148. }
  149.  
  150. // Dot Product Operator Friend Declaration -- do I need??
  151. template<typename Tx>
  152. friend Tx operator*(const MyVector<Tx>& a, const MyVector<Tx>& b);
  153.  
  154.  
  155. private:
  156.  
  157. // Requests memory from heap and copies old data to new location
  158. void reserve(int newCapacity){
  159. if ( newCapacity > currentCapacity ){
  160.  
  161. // Make sure we at least double the current capacity
  162. if ( newCapacity > 2*currentCapacity )
  163. currentCapacity = newCapacity;
  164. else
  165. currentCapacity *= 2;
  166.  
  167. // request more space in the heap
  168. T* newData = new T[currentCapacity];
  169.  
  170. //copy the data
  171. for ( int i=0 ; i < numItems ; i++ )
  172. newData[i] = theData[i]; // same as *(newData + i) = *(theData + i)
  173.  
  174. // free memory from old location. The pointer survives
  175. delete[] theData;
  176.  
  177. // update memory address
  178. theData = newData;
  179. }
  180.  
  181. return;
  182.  
  183. }
  184.  
  185. // Use static to avoid having to make unnecessary copies of INITIAL_SIZE
  186. static const int INITIAL_SIZE=10;
  187.  
  188. int currentCapacity;
  189. int numItems;
  190. T* theData;
  191. };
  192.  
  193.  
  194. /*******************************************************
  195. FRIEND FUNCTIONS
  196. *******************************************************/
  197.  
  198. // Dot Product
  199. template<typename T>
  200. T operator*(const MyVector<T>& a, const MyVector<T>& b) {
  201. assert(a.size() == b.size());
  202.  
  203. T sum = 0;
  204. for (int i=0 ; i<b.size() ; i++)
  205. sum += a[i] * b[i];
  206.  
  207. return sum;
  208. }
  209.  
  210. /**************************************************
  211. NON MEMBER OPERATORS
  212. ************************************************* */
  213.  
  214. /*
  215. Dot Product for String (TEMPLATE SPECIALIZATION)
  216. Operator* acting on two objects of type MyVector<string>
  217. Should return a string constructed by
  218. 1) concatenate the 1st entry of the left operand with the 1st entry of the right operand
  219. 2) add " + " to the string
  220. 3) repeat steps 1 and 2 with the next entries of both operands for as long as there are unprocessed entries.
  221. 4) before returning the string, the last " + " should be removed.
  222. E.g:
  223. cout << v * v; // displays: entry_1entry_1 + entry_2entry_2
  224. */
  225.  
  226. template <>
  227. std::string operator* (const MyVector<std::string>& a, const MyVector<std::string>& b) {
  228. assert(a.size() == b.size());
  229.  
  230. std::string ret;
  231. for( int i=0; i<b.size(); i++){
  232. ret += a[i] + b[i];
  233. if (i != b.size()-1) // dont add a + for the last string
  234. {
  235. ret += " + ";
  236. }
  237. }
  238.  
  239. return ret;
  240. }
  241.  
  242. std::string sqrt(std::string a){
  243. std::string b;
  244. b = "sqrt( " + a + " )";
  245. return b;
  246. }
  247.  
  248.  
  249.  
  250. // Scalar Product Operator *
  251. template<typename T>
  252. MyVector<T> operator*( T a, MyVector<T> v){
  253. for ( int i=0 ; i < v.size() ; i++ ){
  254. v[i] = a * v[i];
  255. }
  256. return v; // v is a local copy of the vector
  257. }
  258.  
  259. template<typename T>
  260. MyVector<T> operator*( MyVector<T> v , T a){
  261. return a * v;
  262. }
  263.  
  264.  
  265.  
  266. /* STRING TEMPLATE SPECIALIZATION
  267. operator* acting on two objects of type string and MyVector<string>.
  268. Should return a MyVector<string> object where the i-th entry of this object is the concatenation ( string addition ) of s and the i-th entry of the MyVector<string> parameter. The concatenation should preserve the order in which the parameters were passed.
  269. E.g:
  270. cout << a * v; // displays: This is: entry_1 This is: entry_2
  271. cout << v * b; // displays: entry_1, this is. entry_2, this is.
  272. */
  273. template <>
  274. MyVector<std::string> operator* (std::string a, MyVector<std::string> v){
  275.  
  276. MyVector<std::string> newVec;
  277. for (int i=0; i < v.size(); i++){
  278. newVec.push_back(a + v[i]);
  279. }
  280. return newVec;
  281. }
  282.  
  283. template <>
  284. MyVector<std::string> operator* (MyVector<std::string> v, std::string a){
  285.  
  286. MyVector<std::string> newVec;
  287. for (int i=0; i < v.size(); i++){
  288. newVec.push_back(v[i] + a );
  289. }
  290. return newVec;
  291. }
  292.  
  293.  
  294. // >
  295. template<typename T>
  296. bool operator>( const MyVector<T>& a, const MyVector<T>& b ) {
  297. return a.normal() > b.normal() ;
  298. }
  299.  
  300. template <>
  301. bool operator> (const MyVector<std::string>& a, const MyVector<std::string>& b){
  302. for (int i=0; i < b.size(); i++){
  303. if (a[i] != b[i]){
  304. return a[i] > b[i];
  305. }
  306.  
  307. }
  308. return false; //equal
  309. }
  310.  
  311. // >=
  312. template<typename T>
  313. bool operator>=( const MyVector<T>&a, const MyVector<T>& b ) {
  314. return a.normal() >= b.normal();
  315. }
  316.  
  317. template <>
  318. bool operator>=( const MyVector<std::string>& a, const MyVector<std::string>& b){
  319. for (int i=0; i < b.size(); i++){
  320. if (a[i] >= b[i]){
  321. return true;
  322. }
  323. }
  324. return false; //equal
  325. }
  326.  
  327. // <
  328. template<typename T>
  329. bool operator<( const MyVector<T>&a, const MyVector<T>& b ){
  330. return a.normal() < b.normal();
  331. }
  332.  
  333. template <>
  334. bool operator<( const MyVector<std::string>& a, const MyVector<std::string>& b){
  335. for (int i=0; i < b.size(); i++){
  336. if (a[i] < b[i]){
  337. return true;
  338. }
  339. }
  340. return false; //equal
  341. }
  342.  
  343. // <=
  344. template<typename T>
  345. bool operator<=( const MyVector<T>&a, const MyVector<T>& b ){
  346. return a.normal() <= b.normal();
  347. }
  348.  
  349. template <>
  350. bool operator<=( const MyVector<std::string>& a, const MyVector<std::string>& b){
  351. for (int i=0; i < b.size(); i++){
  352. if (a[i] <= b[i]){
  353. return true;
  354. }
  355. }
  356. return false; //equal
  357. }
  358.  
  359. // ==
  360. template<typename T>
  361. bool operator==( const MyVector<T>&a, const MyVector<T>& b ) {
  362. // False if different sizes
  363. if( a.size() != b.size() )
  364. return false;
  365. else{
  366. // False if at least one entry does not match
  367. for (int i=0 ; i<a.size() ; i++){
  368. if ( a[i] != b[i] )
  369. return false;
  370. }
  371. // True if all entries match
  372. return true;
  373. }
  374. }
  375.  
  376. template <>
  377. bool operator== (const MyVector<std::string>& a, const MyVector<std::string>& b){
  378. for (int i=0; i < b.size(); i++){
  379. if (a[i] != b[i]){
  380. return false;
  381. }
  382. }
  383. return true; //equal
  384. }
  385.  
  386. // !=
  387. template<typename T>
  388. bool operator!=( const MyVector<T>& a, const MyVector<T>& b ) {
  389. return !( a == b );
  390. }
  391.  
  392. template <>
  393. bool operator!=( const MyVector<std::string>& a, const MyVector<std::string>& b){
  394. for (int i=0; i < b.size(); i++){
  395. if (a[i] != b[i]){
  396. return true;
  397. }
  398. }
  399. return false; //equal
  400. }
  401.  
  402.  
  403. template<typename T>
  404. std::ostream& operator<<( std::ostream& out, const MyVector<T>& v ){
  405. for ( int i=0 ; i < v.size() ; i++ )
  406. out << std::setw(8) << v[i] ;
  407. out << std::endl;
  408.  
  409. return out;
  410. }
  411.  
  412.  
  413. template <>
  414. std::ostream& operator<<( std::ostream& out, const MyVector<std::string>& v )
  415. {
  416. for (int i=0; i < v.size(); i++)
  417. out << v[i] << " ";
  418. out << std::endl;
  419. return out;
  420. }
  421.  
  422.  
  423.  
  424.  
  425. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement