Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.46 KB | None | 0 0
  1. #ifndef BVECTOR_H
  2. #define BVECTOR_H
  3.  
  4. #include <memory>
  5.  
  6. static const int MIN_CAPACITY = 16;
  7. static const int GROWTH_FACTOR = 2;
  8. static const int SHRINK_FACTOR = 4;
  9.  
  10. class BVector
  11. {
  12. public:
  13. BVector() = delete;
  14. BVector(int);
  15. BVector(int, int);
  16. //BVector(const BVector&); //Copy Constructor
  17. //BVector(const BVector&&); //Move Constructor
  18.  
  19. //BVector& operator=(BVector&); //Copy assignment operator.
  20. //BVector& operator=(BVector&&); //Move assignment operator.
  21.  
  22. ~BVector() = default;
  23.  
  24. int const& operator[] (int) const;
  25. int& operator[](int);
  26. int at(int);
  27.  
  28. void push(int);
  29. int pop();
  30. void insert(int, int);
  31. void prepend(int);
  32. bool find(int);
  33. void Delete(int idx);
  34.  
  35. int size() const;
  36. int capacity() const;
  37. void resize(int);
  38. bool isEmpty();
  39.  
  40.  
  41. private:
  42. int m_size{0};
  43. int m_capacity{MIN_CAPACITY};
  44.  
  45. std::unique_ptr<int[]> m_data;
  46.  
  47. int DetermineCapacity(int);
  48. void IncreaseCapacity();
  49. void DecreaseCapacity();
  50. };
  51.  
  52.  
  53.  
  54. #endif // BVECTOR_H
  55.  
  56. #include "bvector.h"
  57. #include <iostream>
  58.  
  59. BVector::BVector(int capacity): m_size(0)
  60. {
  61. int new_capacity = DetermineCapacity(capacity);
  62. m_data = std::unique_ptr<int[]>(new int[new_capacity]);
  63. }
  64.  
  65. BVector::BVector(int capacity, int init_val)
  66. {
  67. int new_capacity = DetermineCapacity(capacity);
  68. m_data = std::unique_ptr<int[]>(new int[new_capacity]);
  69.  
  70. for(int i = 0; i < new_capacity; i++)
  71. {
  72. m_data[i] = init_val;
  73. }
  74. }
  75.  
  76. int const& BVector::operator[](int idx) const
  77. {
  78. return m_data[idx];
  79. }
  80.  
  81. int& BVector::operator[](int idx)
  82. {
  83. return m_data[idx];
  84. }
  85.  
  86. int BVector::at(int idx)
  87. {
  88. return m_data[idx];
  89. }
  90.  
  91. void BVector::resize(int requiredSize)
  92. {
  93. if(m_size < requiredSize)
  94. {
  95. if(m_size == m_capacity)
  96. IncreaseCapacity();
  97. }else if(m_size > requiredSize)
  98. {
  99. if(m_size < (m_capacity/SHRINK_FACTOR))
  100. DecreaseCapacity();
  101. }
  102.  
  103. }
  104.  
  105. int BVector::DetermineCapacity(int capacity)
  106. {
  107. int actual_capacity = MIN_CAPACITY;
  108.  
  109. while(capacity > (actual_capacity/GROWTH_FACTOR))
  110. {
  111. actual_capacity *= GROWTH_FACTOR;
  112. }
  113.  
  114. return actual_capacity;
  115. }
  116.  
  117. void BVector::IncreaseCapacity()
  118. {
  119. int old_capacity = m_capacity;
  120. int new_capacity = DetermineCapacity(old_capacity);
  121.  
  122. if(new_capacity != old_capacity)
  123. {
  124. std::unique_ptr<int[]> new_data = std::unique_ptr<int[]>(new int[new_capacity]);
  125.  
  126. for(int i = 0; i < m_size; i++)
  127. {
  128. new_data[i] = m_data[i];
  129. }
  130.  
  131. m_capacity = new_capacity;
  132.  
  133. m_data = std::move(new_data);
  134. }
  135. }
  136.  
  137. void BVector::DecreaseCapacity()
  138. {
  139. int old_capacity = m_capacity;
  140. int new_capacity = old_capacity / 2;
  141.  
  142. if(new_capacity < MIN_CAPACITY)
  143. new_capacity = MIN_CAPACITY;
  144.  
  145. if(new_capacity != old_capacity)
  146. {
  147. std::unique_ptr<int[]> new_data = std::unique_ptr<int[]>(new int[new_capacity]);
  148.  
  149. for(int i = 0; i < m_size; i++)
  150. {
  151. new_data[i] = m_data[i];
  152. }
  153.  
  154. m_capacity = new_capacity;
  155.  
  156. m_data = std::move(new_data);
  157. }
  158. }
  159.  
  160. int BVector::capacity() const
  161. {
  162. return this->m_capacity;
  163. }
  164.  
  165. int BVector::size() const
  166. {
  167. return this->m_size;
  168. }
  169.  
  170. void BVector::push(int val)
  171. {
  172. resize(m_size + 1);
  173. m_data[m_size] = val;
  174. ++m_size;
  175. }
  176.  
  177. bool BVector::isEmpty()
  178. {
  179. return (m_size == 0);
  180. }
  181.  
  182. int BVector::pop()
  183. {
  184. if(!this->isEmpty())
  185. {
  186. resize(m_size-1);
  187. int value = m_data[m_size];
  188. --m_size;
  189. return value;
  190. }else
  191. {
  192. std::cout << "Nothing to pop." << std::endl;
  193. exit(EXIT_FAILURE);
  194. }
  195. }
  196.  
  197. void BVector::insert(int value, int idx)
  198. {
  199. resize(m_size + 1);
  200.  
  201. std::unique_ptr<int[]> newData = std::unique_ptr<int[]>(new int[m_capacity]);
  202.  
  203. for (int i = 0; i < m_size+1; i++)
  204. {
  205. if(i == idx)
  206. {
  207. newData[i] = value;
  208. newData[i+1] = m_data[i];
  209. }
  210. else if(i > idx)
  211. {
  212. newData[i+1] = m_data[i];
  213. }
  214. else
  215. {
  216. newData[i] = m_data[i];
  217. }
  218. }
  219.  
  220. m_data = std::move(newData);
  221.  
  222. ++m_size;
  223. }
  224.  
  225. void BVector::prepend(int value)
  226. {
  227. resize(m_size + 1);
  228.  
  229. for(int i = m_size; i > 0; i--)
  230. {
  231. m_data[i] = m_data[i - 1];
  232. }
  233.  
  234. m_data[0] = value;
  235.  
  236. ++m_size;
  237. }
  238.  
  239. bool BVector::find(int reqVal)
  240. {
  241. for(auto i = 0; i < m_size; i++)
  242. {
  243. if(m_data[i] == reqVal)
  244. return true;
  245. }
  246. return false;
  247. }
  248.  
  249. void BVector::Delete(int idx)
  250. {
  251. resize(m_size - 1);
  252.  
  253. for(int i = idx; i < m_size - 1; i++)
  254. {
  255. m_data[i] = m_data[i+1];
  256. }
  257.  
  258. --m_size;
  259. }
  260.  
  261. #include <iostream>
  262. #include "bvector.h"
  263.  
  264. int main()
  265. {
  266. BVector vec(10);
  267.  
  268. std::cout << vec[3] << std::endl;
  269.  
  270. vec.push(10);
  271. vec.push(20);
  272. vec.push(30);
  273. vec.push(40);
  274. vec.push(50);
  275. vec.push(60);
  276. vec.push(70);
  277. vec.push(80);
  278. vec.push(90);
  279. vec.push(100);
  280. vec.push(110);
  281. vec.push(120);
  282. vec.push(130);
  283. vec.push(140);
  284. vec.push(150);
  285. vec.push(160);
  286. vec.push(170);
  287. vec.push(180);
  288.  
  289. vec.insert(333, 8);
  290.  
  291. vec.Delete(8);
  292.  
  293. std::cout << vec[vec.size()-1] << std::endl;
  294.  
  295. vec[vec.size()-1] = 555;
  296.  
  297. std::cout << vec.at(vec.size()-1) << std::endl;
  298.  
  299. vec.prepend(987);
  300.  
  301. std::cout << vec.at(0) << std::endl;
  302.  
  303. std::cout << vec.at(1) << std::endl;
  304.  
  305. int x = vec.pop();
  306.  
  307. std::cout << "Popped Value: " << x << std::endl;
  308.  
  309. bool flg = vec.find(150);
  310. std::cout << flg << std::endl;
  311.  
  312.  
  313. return 0;
  314. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement