Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. #ifndef MY_LIST
  2. #define MY_LIST
  3.  
  4. #include "Intefaces.hpp"
  5.  
  6. template<class T>
  7. class List : public IList<T>
  8. {
  9. private:
  10. T *data;
  11. int len;
  12. int last;
  13.  
  14. void getMoreMemory()
  15. {
  16. T *newData = new T[len * 2];
  17. for (int i = 0; i < len; i++)
  18. newData[i] = data[i];
  19. len *= 2;
  20. delete data;
  21. data = newData;
  22. }
  23.  
  24. template<class T>
  25. class ListIterator : public IEnumerator<T>
  26. {
  27. private:
  28. T *data;
  29. int last;
  30. int current;
  31. public:
  32. ListIterator(T *data, int last)
  33. {
  34. this->data = data;
  35. this->last = last;
  36. current = -1;
  37. }
  38.  
  39.  
  40. virtual bool MoveNext()
  41. {
  42. if(current == last)
  43. return false;
  44. current++;
  45. return true;
  46. }
  47. virtual void Reset()
  48. {
  49. current = -1;
  50. }
  51. virtual T Current()
  52. {
  53. if(current == -1)
  54. throw "MoveNext must be called.";
  55. return data[current];
  56. }
  57.  
  58. virtual ~ListIterator() {}
  59. };
  60. public:
  61. List()
  62. {
  63. data = new T[4];
  64. last = -1;
  65. len = 4;
  66. }
  67.  
  68. List(IEnumerable<T> *e)
  69. {
  70. data = new T[4];
  71. len = 4;
  72. last = -1;
  73. IEnumerator<T> *enumerator = e->GetEnumerator();
  74. while (enumerator->MoveNext())
  75. {
  76. Add(enumerator->Current());
  77. }
  78. delete enumerator;
  79. }
  80.  
  81. List(int lenght)
  82. {
  83. data = new T[lenght];
  84. len = lenght;
  85. last = -1;
  86. }
  87.  
  88. List(const List<T> &obj)
  89. {
  90. IEnumerator<T> *e = obj.GetEnumerator();
  91. while (e->MoveNext())
  92. {
  93. Add(e->Current());
  94. }
  95. delete e;
  96.  
  97. len = obj.len;
  98. last = obj.last;
  99. }
  100.  
  101. virtual IEnumerator<T> *GetEnumerator()
  102. {
  103. return new ListIterator<T>(data, last);
  104. }
  105.  
  106. virtual void Add(T item)
  107. {
  108. if (last + 1 == len)
  109. getMoreMemory();
  110. data[++last] = item;
  111. }
  112.  
  113. virtual bool Remove(T item)
  114. {
  115. for (int i = 0; i < last + 1; i++)
  116. {
  117. if (data[i] == item)
  118. {
  119. for(int j = i; j < last + 1; j++)
  120. {
  121. data[j] = data[j + 1];
  122. }
  123. last--;
  124. return true;
  125. }
  126. }
  127. return false;
  128. }
  129.  
  130. virtual void Insert(int index, T item)
  131. {
  132. if (index < 0 || index > last + 1)
  133. throw "index";
  134.  
  135. if (last + 1 >= len)
  136. {
  137. getMoreMemory();
  138. }
  139.  
  140. for (int i = last + 1; i > index; i--)
  141. {
  142. data[i] = data[i - 1];
  143. }
  144.  
  145. data[index] = item;
  146. last++;
  147. }
  148.  
  149. virtual void RemoveAt(int index)
  150. {
  151. if(index < 0 || index > last)
  152. throw "index";
  153.  
  154. for(int i = index; i < last + 1; i++)
  155. {
  156. data[i] = data[i + 1];
  157. }
  158. last--;
  159. }
  160.  
  161. virtual bool IsReadOnly()
  162. {
  163. return false;
  164. }
  165.  
  166. virtual T &operator[](int index)
  167. {
  168. if(index < 0 || index > last + 1)
  169. throw "index";
  170. return data[index];
  171. }
  172.  
  173. virtual void Clear()
  174. {
  175. last = -1;
  176. }
  177. };
  178.  
  179. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement