Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.56 KB | None | 0 0
  1. // Copyright 2012, University of Freiburg,
  2. // Chair of Algorithms and Data Structures.
  3. // Author: Hannah Bast <bast>, Anton Stepan <stepana>
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include "./String.h"
  8.  
  9. // Max length of a line.
  10. const int MAX_LINE_LENGTH = 1000;
  11.  
  12. // _____________________________________________________________________________
  13. template<class T>
  14. String<T>::String()
  15. {
  16.   _size = 0;
  17.   _contents = NULL;
  18. }
  19.  
  20. // _____________________________________________________________________________
  21. String<bool>::String()
  22. {
  23.   _size = 0;
  24.   _contents = NULL;
  25. }
  26.  
  27. // _____________________________________________________________________________
  28. template<class T>
  29. String<T>::~String()
  30. {
  31.   delete[] _contents;
  32. }
  33.  
  34. // _____________________________________________________________________________
  35. String<bool>::~String()
  36. {
  37.   delete[] _contents;
  38. }
  39.  
  40. // _____________________________________________________________________________
  41. template<class T>
  42. String<T>::String(const String<T>& s)
  43. {
  44.   _size = s._size;
  45.   _contents = new char[_size];
  46.   for (int i = 0; i < s._size; i++) _contents[i] = s._contents[i];
  47. }
  48.  
  49. // _____________________________________________________________________________
  50. template<class T>
  51. String<T>& String<T>::operator=(const String<T>& x)
  52. {
  53.   if (_contents != NULL)
  54.   {
  55.     delete[] _contents;
  56.   }
  57.   _contents = new char[_size];
  58.   for (int i = 0; i < _size; i++) _contents[i] = x._contents[i];
  59.   return *this;
  60. }
  61.  
  62. // _____________________________________________________________________________
  63. String<bool>& String<bool>::operator=(const char* x)
  64. {
  65.   // if left String already exists, delete.
  66.   if (_contents != NULL)
  67.   {
  68.     delete[] _contents;
  69.   }
  70.   _size = 0;
  71.   _contents = NULL;
  72.   // if const char is "".
  73.   if (x[0] == 0)
  74.   {
  75.     _contents = NULL;
  76.     return *this;
  77.   }
  78.   // new string is created by appending the const char*.
  79.   int i = 0;
  80.   while (x[i] != 0)
  81.   {
  82.     if (x[i] == '1')
  83.     {
  84.       append(1);
  85.     }
  86.     else if (x[i] == '0')
  87.     {
  88.       append(0);
  89.     }
  90.     else
  91.     {
  92.       fprintf(stderr, "Wrong user input. %c\n", x[i]);
  93.     }
  94.     i++;
  95.   }
  96.   return *this;
  97. }
  98.  
  99. // _____________________________________________________________________________
  100. template<class T>
  101. void String<T>::set(const char* s)
  102. {
  103.   _size = 0;
  104.   if (_contents != NULL) delete[] _contents;
  105.   while (s[_size] != 0) _size++;
  106.   _contents = new char[_size];
  107.   for (int i = 0; i < _size; i++) _contents[i] = s[i];
  108. }
  109.  
  110. // _____________________________________________________________________________
  111. template<class T>
  112. int String<T>::size() const
  113. {
  114.   _numCallsToSizeMethod++;
  115.   return _size;
  116. }
  117.  
  118. // _____________________________________________________________________________
  119. template<class T>
  120. void String<T>::append(String<T> s)
  121. {
  122.   char* _newContents = new char[_size + s._size];
  123.   for (int i = 0; i < _size; i++) _newContents[i] = _contents[i];
  124.   for (int i = 0; i < s._size; i++) _newContents[_size + i] = s._contents[i];
  125.   delete[] _contents;
  126.   _contents = _newContents;
  127.   _size += s._size;
  128. }
  129.  
  130. // _____________________________________________________________________________
  131. void String<bool>::append(bool x)
  132. {
  133.   /*if (_size == 0)
  134.   {
  135.     char* _tmpContents = new char;
  136.     _tmpContents[0] = 0;
  137.     _contents = _tmpContents;
  138.   }
  139.   else if (_size / 8 > 1 && _size % 8 == 0)
  140.   {
  141.     char* _newContents = new char[(_size / 8) + 1];
  142.     for (int i = 0; i < _size; i++) _contents[i] |= _newContents[i];
  143.     _contents = _newContents;
  144.     delete[] _newContents;
  145.   }*/
  146.  
  147.   // if memory is not enough, allocate 1 more char.
  148.   if (_size % 8 == 0)
  149.   {
  150.     // allocate a temporary Array with one more char as before.
  151.     char* _newContents = new char[_size/8 + 1];
  152.     // initiate every bit with 0.
  153.     for (int i = 0; i < _size/8+1; i++) _newContents[i] = 0;
  154.     // copy old content into new content.
  155.     for (int i = 0; i < _size/8; i++)
  156.     {
  157.       _newContents[i] = _contents[i];
  158.     }
  159.     // delete old content and redirect the pointer to the temp.array
  160.     delete[] _contents;
  161.     _contents = _newContents;
  162.   }
  163.   // If the bool which will be appended is true, add 1, else it's
  164.   // already true.
  165.   if (x == true)
  166.   {
  167.     int byteIndex = _size / 8;
  168.     int bitIndex = _size % 8;
  169.     char bitMask = 1 << bitIndex;
  170.     _contents[byteIndex] |= bitMask;
  171.   }
  172.   // else, just increase size.
  173.   _size++;
  174. }
  175.  
  176. // _____________________________________________________________________________
  177. template<class T>
  178. void String<T>::insert(int pos, const String<T> &x)
  179. {
  180.   // create a temporary character array with old size plus size of the string
  181.   // which has to be inserted.
  182.  
  183.   if (pos < 0 || pos > _size) return;
  184.  
  185.   char* _tmpContents = new char[_size + x.size()];
  186.  
  187.  
  188.   for (int i = 0; i < pos; i++) _tmpContents[i] = _contents[i];
  189.   for (int i = 0; i < x._size; i++) _tmpContents[pos + i] = x._contents[i];
  190.   for (int i = pos; i < _size; i++)
  191.   {
  192.     _tmpContents[x._size + i] = _contents[i];
  193.   }
  194.   delete[] _contents;
  195.   _size += x.size();
  196.   _contents = _tmpContents;
  197. }
  198.  
  199. // _____________________________________________________________________________
  200. void String<bool>::insert(int pos, const String<bool>& b)
  201. {
  202.   if (pos < 0 || pos > _size) return;
  203.  
  204.   String<bool> tmp;
  205.   // creating temporary String and appending old and new string to it.
  206.   for (int i = 0; i < pos; i++)
  207.   {
  208.     tmp.append((*this)[i]);
  209.     /*
  210.     if ((*this)[i])
  211.     {
  212.       tmp.append(true);
  213.     }
  214.     else
  215.     {
  216.       tmp.append(false);
  217.     }*/
  218.   }
  219.  
  220.   for (int i = 0; i < b.size(); i++)
  221.   {
  222.     tmp.append(b[i]);
  223.     /*
  224.     if (b[i] == true)
  225.     {
  226.       tmp.append(true);
  227.     }
  228.     else
  229.     {
  230.       tmp.append(false);
  231.     }*/
  232.   }
  233.  
  234.   for (int i = pos; i < _size; i++)
  235.   {
  236.     tmp.append((*this)[i]);
  237.     /*
  238.     if ((*this)[i])
  239.     {
  240.       tmp.append(true);
  241.     }
  242.     else
  243.     {
  244.       tmp.append(false);
  245.     }*/
  246.   }
  247.   _size += b.size();
  248.   assert(_contents != NULL);
  249.   delete[] _contents;
  250.   _contents = new char[_size/8 + (_size%8 > 0)];
  251.   int lengthOfNewArray = (_size/8 + (_size != 0));
  252.   for (int i = 0; i < lengthOfNewArray; i++) _contents[i] = tmp._contents[i];
  253. }
  254.  
  255.  
  256.  
  257. // _____________________________________________________________________________
  258. template<class T>
  259. void String<T>::erase(int pos, int length)
  260. {
  261.   // creates a temporary character array with
  262.   // size minus 'erase-length'.
  263.  
  264.   // catch wrong input.
  265.   if (pos < 0 || pos > _size) return;
  266.   if ((pos + length) > _size) length = _size - pos;
  267.  
  268.   if (length == _size)
  269.   {
  270.     delete[] _contents;
  271.     _contents = NULL;
  272.     _size = 0;
  273.     return;
  274.   }
  275.  
  276.   char* _tmpContents = new char[_size - length];
  277.   // copy all characters until position pos.
  278.   for (int i = 0; i < pos; i++) _tmpContents[i] = _contents[i];
  279.   // copy all characters after pos + length.
  280.   for (int i = pos+length; i < _size; i++)
  281.   {
  282.     _tmpContents[i - length] = _contents[i];
  283.   }
  284.   // give the old memory free.
  285.   delete[] _contents;
  286.   // point to the new stuff.
  287.   _contents = _tmpContents;
  288.   _size -= length;
  289. }
  290.  
  291. // _____________________________________________________________________________
  292. int String<bool>::size() const
  293. {
  294.   return _size;
  295. }
  296.  
  297. // _____________________________________________________________________________
  298. void String<bool>::erase(int pos, int length)
  299. {
  300.   if (pos < 0 || pos > _size) return;
  301.   if ((pos + length) > _size) length = _size - pos;
  302.  
  303.   if (length == _size)
  304.   {
  305.     delete[] _contents;
  306.     _contents = NULL;
  307.     _size = 0;
  308.     return;
  309.   }
  310.   String<bool> tmp;
  311.   for (int i= 0; i < pos; i++)
  312.   {
  313.     if ((*this)[i])
  314.     {
  315.       tmp.append(true);
  316.     }
  317.     else
  318.     {
  319.       tmp.append(false);
  320.     }
  321.   }
  322.   for (int i = pos+length; i < _size; i++)
  323.   {
  324.     if ((*this)[i])
  325.     {
  326.       tmp.append(true);
  327.     }
  328.     else
  329.     {
  330.       tmp.append(false);
  331.     }
  332.   }
  333.   _size -= length;
  334.   assert(_contents != NULL);
  335.   delete[] _contents;
  336.   _contents = new char[_size/8 + (_size%8 != 0)];
  337.   int lengthOfNewArray = _size/8 + (_size%8 != 0);
  338.   for (int i = 0; i < lengthOfNewArray; i++) _contents[i] = tmp._contents[i];
  339. }
  340.  
  341. // _____________________________________________________________________________
  342. template<class T>
  343. char String<T>::operator[](int n) const
  344. {
  345.   return _contents[n];
  346. }
  347.  
  348. // _____________________________________________________________________________
  349. bool String<bool>::operator[](int i) const
  350. {
  351.   assert(i >= 0 && i < _size);
  352.   int byteIndex = i / 8;
  353.   int bitIndex = i % 8;
  354.   char bitMask = 1 << bitIndex;
  355.   return (_contents[byteIndex] & bitMask) == 0 ? false : true;
  356. }
  357.  
  358. // _____________________________________________________________________________
  359. template<class T>
  360. void String<T>::print()
  361. {
  362.   for (int i = 0; i < _size; i++) printf("%c", _contents[i]);
  363.   printf("\n");
  364. }
  365.  
  366. // _____________________________________________________________________________
  367. void String<bool>::print() const
  368. {
  369.   for (int i = 0; i < _size; i++) printf("%s ", (*this)[i] ? "1" : "0");
  370.   printf("\n");
  371. }
  372.  
  373. // _____________________________________________________________________________
  374. template<class T>
  375. bool String<T>::operator==(const String &s)
  376. {
  377.   for (int i = 0; i < _size; i++)
  378.   {
  379.     // check if all characters are the same.
  380.     if (_contents[i] != s._contents[i]) return false;
  381.   }
  382.   return true;
  383. }
  384.  
  385. // _____________________________________________________________________________
  386. template<class T>
  387. int String<T>::find(int start, const String<T> &s)
  388. {
  389.   // Catch wrong input.
  390.   if (start < 0 || start >= _size) return -1;
  391.   if (s._size > _size) return -1;
  392.   if (s._size == 0) return -1;
  393.  
  394.  
  395.   int i = start;
  396.   int length = s.size();
  397.  
  398.   // starts at the given position.
  399.   while (i < _size)
  400.   {
  401.     // if first letter of the input is found, check if its the word or just
  402.     // another String with the same starting character.
  403.     if (_contents[i] == s[0])
  404.     {
  405.       bool check = true;
  406.       for (int j = 0; j < length; j++)
  407.       {
  408.         if (_contents[i + j] != s[j]) check = false;
  409.       }
  410.       if (check) return i;
  411.     }
  412.     i++;
  413.   }
  414.   // return -1 if found nothing.
  415.   return -1;
  416. }
  417.  
  418. /*
  419. // _____________________________________________________________________________
  420. void String::replace(const String& str, const String& other, String* resultStr)
  421. {
  422.   resultStr = this;
  423.   while (resultStr->find(0, str) != -1)
  424.   {
  425.     int start = resultStr->find(0, str);
  426.     resultStr->erase(start, str.size());
  427.     resultStr->insert(start, other);
  428.   }
  429. }
  430. */
  431. // _____________________________________________________________________________
  432. template<class T>
  433. void String<T>::replace(const String<T>& str, const String<T>& other)
  434. {
  435.   // Catch wrong input.
  436.   if (_size == 0 || str._size == 0) return;
  437.  
  438.  
  439.   // While str is still in the String.
  440.   while (find(0, str) != -1)
  441.   {
  442.     int start = find(0, str);
  443.     // delete str in the String.
  444.     erase(start, str.size());
  445.     // insert other at the deleted position.
  446.     insert(start, other);
  447.   }
  448. }
  449.  
  450. // _____________________________________________________________________________
  451. template<class T>
  452. bool String<T>::getLine(FILE* file)
  453. {
  454.   if (file == NULL) return false;
  455.   char line[MAX_LINE_LENGTH + 1];
  456.   fgets(line, MAX_LINE_LENGTH, file);
  457.   if (feof(file)) return false;
  458.   set(line);
  459.   return true;
  460. }
  461.  
  462. // _____________________________________________________________________________
  463. template<class T>
  464. void String<T>::printToFile(FILE* file)
  465. {
  466.   if (file == NULL)
  467.   {
  468.     return;
  469.   }
  470.   else
  471.   {
  472.     fprintf(file, "%s", _contents);
  473.   }
  474. }
  475.  
  476. // Explicit instantiation
  477. template class String<char>;
  478. // template class String<bool>;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement