1 Basic Test Results 2 Statement - PDF 3 Vlvector - HPP
1 Basic Test Results 2 Statement - PDF 3 Vlvector - HPP
2 STATEMENT.pdf 4
3 VLVector.hpp 5
1
1 Basic Test Results
1 ********************************************
2 * *
3 * Hello C++ Workshop Student, *
4 * We wish you good luck on your exam! *
5 * *
6 ********************************************
7 Running...
8
9 Opening tar file
10 OK
11 Tar extracted O.K.
12
13 Checking files...
14 OK
15 Making sure files are not empty...
16 OK
17 Compilation check...
18
19 Compiling...
20 OK
21 Compiling...
22 OK
23
24 **********************************
25 * *
26 * Compilation seems OK! *
27 * Check if you got warnings! *
28 * *
29 **********************************
30
31 =====================
32 Public test cases
33 =====================
34
35 [#0][Presubmission] Test __presubmit_testCreateVLAs... OK!
36 [#1][Presubmission] Test __presubmit_testPushBack... OK!
37 [#2][Presubmission] Test __presubmit_testSize... OK!
38 [#3][Presubmission] Test __presubmit_testCapacity... OK!
39 [#4][Presubmission] Test __presubmit_testEmpty... OK!
40 [#5][Presubmission] Test __presubmit_testClear... OK!
41 [#6][Presubmission] Test __presubmit_testPopBack... OK!
42 [#7][Presubmission] Test __presubmit_testGetElement... OK!
43 [#8][Presubmission] Test __presubmit_testData... OK!
44 [#9][Presubmission] Test __presubmit_testComparison... OK!
45 [#10][Presubmission] Test __presubmit_testAssignment... OK!
46 [#11][Presubmission] Test __presubmit_testIterator... OK!
47 [#12][Presubmission] Test __presubmit_testInsert1... OK!
48 [#13][Presubmission] Test __presubmit_testInsert2... OK!
49 [#14][Presubmission] Test __presubmit_testErase1... OK!
50 [#15][Presubmission] Test __presubmit_testIteratorsCtor... OK!
51 [#16][Presubmission] Test __presubmit_testCopyCtor... OK!
52 Running PresubmissionTests
53 OK
54 ***********************************
55 * *
56 * presubmission script passed *
57 * *
58 ***********************************
59
2
60 =========================
61 = Checking coding style =
62 =========================
63 VLVector.hpp(572, 7): func_name_special {Function name(push_back) should not contains special characters or underbars}
64 VLVector.hpp(600, 7): func_name_special {Function name(pop_back) should not contains special characters or underbars}
65 ** Total Violated Rules : 1
66 ** Total Errors Occurs : 2
67 ** Total Violated Files Count: 1
3
אני מצהיר/ה בזאת כי ידוע לי שאם יתגלה כי עברתי עבירת העתקה מסוג זה,
תוגש נגדי תלונה על כך לוועדת המשמעת של האוניברסיטה העברית.
5
60 Iterator(T *ptr) : _ptr(ptr)
61 {}
62
63
64 /**
65 * get iterator pointer
66 * @return pointer
67 */
68 pointer getPtr()
69 { return _ptr; }
70
71 //read and write
72 /**
73 * dereference operator
74 * @return reference to value
75 */
76 reference operator*()
77 { return *_ptr; }
78
79 //iteration
80 /**
81 * prefix increment
82 * @return iterator
83 */
84 Iterator &operator++()
85 {
86 _ptr++;
87 return *this;
88 }
89
90 /**
91 * postfix increment
92 * @return iterator
93 */
94 Iterator operator++(int)
95 {
96 Iterator it(*this);
97 _ptr++;
98 return it;
99 }
100
101 /**
102 * prefix decrement
103 * @return iterator
104 */
105 Iterator &operator--()
106 {
107 _ptr--;
108 return *this;
109 }
110
111 /**
112 * postfix decrement
113 * @return iterator
114 */
115 Iterator operator--(int)
116 {
117 Iterator it(*this);
118 _ptr--;
119 return it;
120 }
121
122 /**
123 * offset iterator by int
124 * @param i int
125 * @return this iterator
126 */
127 Iterator &operator+=(int i)
6
128 {
129 _ptr += i;
130 return *this;
131 }
132
133 /**
134 * offset iterator by int
135 * @param i int
136 * @return this iterator
137 */
138 Iterator &operator-=(int i)
139 {
140 _ptr -= i;
141 return *this;
142 }
143
144 /**
145 * return offset of iterator as new iterator
146 * @param i int
147 * @return iterator
148 */
149 Iterator operator+(int i) const
150 {
151 Iterator it(*this);
152 return it += i;
153 }
154
155 /**
156 * return offset of iterator as new iterator
157 * @param i int
158 * @return iterator
159 */
160 Iterator operator-(int i) const
161 {
162 Iterator it(*this);
163 return it -= i;
164 }
165
166 /**
167 * return distance between two iterators
168 * @param other iterator
169 * @return distance
170 */
171 difference_type operator-(const Iterator &other) const
172 { return _ptr - other._ptr; }
173
174 //comparison
175 /**
176 * smaller-than operator
177 * @param other iterator
178 * @return true or false
179 */
180 bool operator<(const Iterator &other) const
181 { return _ptr < other._ptr; }
182
183 /**
184 * smaller-than or equal operator
185 * @param other iterator
186 * @return true or false
187 */
188 bool operator<=(const Iterator &other) const
189 { return _ptr <= other._ptr; }
190
191 /**
192 * larger-than operator
193 * @param other iterator
194 * @return true or false
195 */
7
196 bool operator>(const Iterator &other) const
197 { return _ptr > other._ptr; }
198
199 /**
200 * larger-than or equal operator
201 * @param other iterator
202 * @return true or false
203 */
204 bool operator>=(const Iterator &other) const
205 { return _ptr >= other._ptr; }
206
207 /**
208 * comparison operator
209 * @param other iterator
210 * @return true or false
211 */
212 bool operator==(const Iterator &other) const
213 { return _ptr == other._ptr; }
214
215 /**
216 * unequal comparison operator
217 * @param other iterator
218 * @return true or false
219 */
220 bool operator!=(const Iterator &other) const
221 { return _ptr != other._ptr; }
222 };
223
224 /**
225 * random access const iterator class for VLVector<T,C> container
226 */
227 class ConstIterator
228 {
229 T *_ptr;
230
231 public:
232 //const_iterator traits
233 typedef T value_type;
234 typedef const T *pointer;
235 typedef const T &reference;
236 typedef ptrdiff_t difference_type;
237 typedef random_access_iterator_tag iterator_category;
238
239 /**
240 * default ctor
241 */
242 ConstIterator() : _ptr(nullptr)
243 {}
244
245 /**
246 * pointer ctor
247 * @param ptr pointer
248 */
249 ConstIterator(T *ptr) : _ptr(ptr)
250 {}
251
252 /**
253 * conversion constructor
254 * @param it
255 */
256 ConstIterator(Iterator it) : _ptr(it.getPtr())
257 {}
258
259 //read only
260 /**
261 * dereference operator
262 * @return reference to const value
263 */
8
264 reference operator*() const
265 { return *_ptr; }
266
267 //iteration:
268 /**
269 * prefix increment operator
270 * @return const iterator
271 */
272 ConstIterator &operator++()
273 {
274 _ptr++;
275 return *this;
276 }
277
278 /**
279 * postfix increment operator
280 * @return const iterator
281 */
282 ConstIterator operator++(int)
283 {
284 ConstIterator it(*this);
285 _ptr++;
286 return it;
287 }
288
289 /**
290 * prefix decrement operator
291 * @return const iterator
292 */
293 ConstIterator &operator--()
294 {
295 _ptr--;
296 return *this;
297 }
298
299 /**
300 * postfix decrement operator
301 * @return const iterator
302 */
303 ConstIterator operator--(int)
304 {
305 ConstIterator it(*this);
306 _ptr--;
307 return it;
308 }
309
310 /**
311 * offset const iterator by int
312 * @param i int
313 * @return this const iterator
314 */
315 ConstIterator &operator+=(int i)
316 {
317 _ptr += i;
318 return *this;
319 }
320
321 /**
322 * offset const iterator by int
323 * @param i int
324 * @return this const iterator
325 */
326 ConstIterator &operator-=(int i)
327 {
328 _ptr -= i;
329 return *this;
330 }
331
9
332 /**
333 * offset and return new const iterator
334 * @param i int
335 * @return const iterator
336 */
337 ConstIterator operator+(int i) const
338 {
339 ConstIterator it(*this);
340 return it += i;
341 }
342
343 /**
344 * offset and return new const iterator
345 * @param i int
346 * @return this const iterator
347 */
348 ConstIterator operator-(int i) const
349 {
350 ConstIterator it(*this);
351 return it -= i;
352 }
353
354 /**
355 * find distance between two const iterators
356 * @param other const iterator
357 * @return distance
358 */
359 difference_type operator-(const ConstIterator &other) const
360 { return _ptr - other._ptr; }
361
362 //comparison
363 /**
364 * smaller-than operator
365 * @param other const iterator
366 * @return true or false
367 */
368 bool operator<(const ConstIterator &other) const
369 { return _ptr < other._ptr; }
370
371 /**
372 * smaller-than or equal operator
373 * @param other const iterator
374 * @return true or false
375 */
376 bool operator<=(const ConstIterator &other) const
377 { return _ptr <= other._ptr; }
378
379 /**
380 * larger-than operator
381 * @param other const iterator
382 * @return true or false
383 */
384 bool operator>(const ConstIterator &other) const
385 { return _ptr > other._ptr; }
386
387 /**
388 * larger-than or equal operator
389 * @param other const iterator
390 * @return true or false
391 */
392 bool operator>=(const ConstIterator &other) const
393 { return _ptr >= other._ptr; }
394
395 /**
396 * comparison operator
397 * @param other const iterator
398 * @return true or false
399 */
10
400 bool operator==(const ConstIterator &other) const
401 { return _ptr == other._ptr; }
402
403 /**
404 * unequal comparison operator
405 * @param other const iterator
406 * @return true or false
407 */
408 bool operator!=(const ConstIterator &other) const
409 { return _ptr != other._ptr; }
410 };
411
412 public:
413 typedef Iterator iterator;
414 typedef ConstIterator constIterator;
415
416 /**
417 * default constructor
418 */
419 VLVector<T, C>() : _vectorSize(0), _buff(_staticBuff), _staticCap(C), _currentCap(_staticCap)
420 {}
421
422 /**
423 * copy constructor
424 * @param other VLVector<T,C>
425 */
426 VLVector<T, C>(VLVector<T, C> const &other) : _vectorSize(other.size()),
427 _staticCap(C),
428 _currentCap(other.capacity())
429 {
430 //check if data should be copied to static buffer or to dynamically allocated buffer
431 _buff = _currentCap > _staticCap ? new T[_currentCap] : _staticBuff;
432 copy(other.begin(), other.end(), begin());
433 }
434
435 /**
436 * iterator range constructor, copies elements [first, last)
437 * @tparam InputIterator an iterator to an element of another container
438 * @param first element in range
439 * @param last element in range
440 */
441 template<class InputIterator>
442 VLVector<T, C>(const InputIterator first, const InputIterator last): _staticCap(C)
443 {
444 _vectorSize = distance(first, last);
445 _currentCap = _getCurrentCap(_vectorSize);
446 _buff = _currentCap > _staticCap ? new T[_currentCap] : _staticBuff;
447 std::copy(first, last, begin());
448 }
449
450 /**
451 * destructor
452 */
453 ~VLVector<T, C>()
454 {
455 if (_currentCap != _staticCap)
456 {
457 //buffer is dynamically allocated
458 delete[] _buff;
459 }
460 }
461
462 /**
463 * get VlVector size parameter
464 * @return size
465 */
466 const size_t &size() const
467 { return _vectorSize; }
11
468
469 /**
470 * get VlVector capacity param
471 * @return capacity
472 */
473 const size_t &capacity() const
474 { return _currentCap; }
475
476 /**
477 * check if VlVector is empty
478 * @return true or false
479 */
480 bool empty() const
481 { return _vectorSize == 0; }
482
483 /**
484 * access to ith index, throw exception of index is illegal
485 * @param i index
486 * @return reference to element at index
487 */
488 T &at(const size_t i)
489 {
490 if (i >= _vectorSize)
491 {
492 throw out_of_range(RANGE_ERROR);
493 }
494 return _buff[i];
495 }
496
497 /**
498 * const version, can't be written into
499 * @param i index
500 * @return reference to element at index
501 */
502 const T &at(const size_t i) const
503 {
504 if (i >= _vectorSize)
505 {
506 throw out_of_range(RANGE_ERROR);
507 }
508 return _buff[i];
509 }
510
511 /**
512 * access to VlVector element, non exception-throwing
513 * @param i index
514 * @return element at index
515 */
516 T &operator[](const size_t i)
517 { return _buff[i]; }
518
519 /**
520 * access to const VlVector element, non exception-throwing
521 * @param i index
522 * @return element at index
523 */
524 const T &operator[](const size_t i) const
525 { return _buff[i]; }
526
527 /**
528 * assignment operator
529 * @param other VlVector
530 * @return reference to this VlVector
531 */
532 VLVector<T, C> &operator=(const VLVector<T, C> &other)
533 {
534 if (this == &other)
535 {
12
536 return *this;
537 }
538 if (_currentCap != _staticCap)
539 {
540 //dynamically allocated buffer
541 delete[] _buff;
542 }
543 _vectorSize = other.size();
544 _currentCap = other.capacity();
545 _buff = _vectorSize >= _staticCap ? new T[_currentCap] : _staticBuff;
546 copy(other.begin(), other.end(), begin());
547 return *this;
548 }
549
550 /**
551 * vector comparison operator, return true if equal
552 * @param other VlVlVector
553 * @return true if hold same data
554 */
555 bool operator==(const VLVector<T, C> &other) const
556 {
557 return equal(begin(), end(), other.begin());
558 }
559
560 /**
561 * VlVector comparison operator, return true if unequal
562 * @param other VlVector
563 * @return true if hold same data
564 */
565 bool operator!=(const VLVector<T, C> &other) const
566 { return !(*this == other); }
567
568 /**
569 * add element to VlVector at O(1) amortized
570 * @param elem to be added
571 */
572 void push_back(const T &elem)
573 {
574 if (_vectorSize == _currentCap)
575 {
576 _buff = _upSize(_vectorSize);
577 }
578 _buff[_vectorSize] = elem;
579 _vectorSize++;
580 }
581
582 /**
583 * get pointer to VlVector data
584 * @return reference to pointer
585 */
586 T *&data()
587 { return _buff; }
588
589
590 /**
591 * * get pointer to const VlVector data
592 * @return reference to pointer
593 */
594 const T *&data() const
595 { return _buff; }
596
597 /**
598 * make last element no longer part of the VlVector
599 */
600 void pop_back()
601 {
602 if (_vectorSize == 0)
603 {
13
604 return;
605 }
606 _vectorSize--;
607 if (_currentCap > _staticCap && _vectorSize < _staticCap)
608 {
609 _buff = _downSize();
610 }
611 }
612
613 /**
614 * reset VlVector to default constructor
615 */
616 void clear()
617 {
618 if (_currentCap != _staticCap)
619 {
620 delete[] _buff;
621 }
622 _vectorSize = 0;
623 _currentCap = _staticCap;
624 _buff = _staticBuff;
625 }
626
627 /**
628 * insert element to VlVector to the left of iterator position
629 * @param pos iterator to element of VLVector
630 * @param val value to be inserted
631 * @return iterator to new value
632 */
633 iterator insert(iterator pos, const T &val)
634 {
635 //helps us find starting point of insertion in case vector is resized
636 size_t dist = distance(begin(), pos);
637 push_back(val);
638 //iterator points to where elem will be after rotation
639 iterator it = begin() + dist;
640 rotate(it, end() - 1, end());
641 return it;
642 }
643
644 /**
645 * insert element to VlVector to the left of iterator position
646 * @param pos iterator to element of VLVector
647 * @param val value to be inserted
648 * @return iterator to new value
649 */
650 constIterator insert(constIterator pos, const T &val)
651 {
652 size_t dist = distance(cbegin(), pos);
653 push_back(val);
654 iterator it = begin() + dist;
655 std::rotate(it, end() - 1, end());
656 return begin() + dist;
657 }
658
659 /**
660 * insert range of values [first, last) from a different container left of the iterator position
661 * @tparam InputIterator type
662 * @param pos iterator to VLVector
663 * @param first iterator to other container
664 * @param last iterator to other container
665 * @return iterator to first new value inserted
666 */
667 template<class InputIterator>
668 iterator insert(iterator pos, InputIterator first, InputIterator last)
669 {
670 size_t dist1 = distance(begin(), pos);
671 size_t dist2 = distance(first, last);
14
672 while (first != last)
673 {
674 push_back(*first);
675 first++;
676 }
677 iterator it = begin() + dist1;
678 rotate(it, end() - dist2, end());
679 return it;
680 }
681
682 /**
683 * insert range of values [first, last) from a different container left of the constIterator
684 * position
685 * @tparam InputIterator type
686 * @param pos constIterator to VLVector
687 * @param first iterator to other container
688 * @param last iterator to other container
689 * @return constIterator to first new value inserted
690 */
691 template<class InputIterator>
692 constIterator insert(constIterator pos, InputIterator first, InputIterator last)
693 {
694 size_t dist1 = distance(cbegin(), pos);
695 size_t dist2 = distance(first, last);
696 while (first != last)
697 {
698 push_back(*first);
699 first++;
700 }
701 //rotate can't be used with constIterator
702 rotate(begin() + dist1, end() - dist2, end());
703 return cbegin() + dist1;
704 }
705
706 /**
707 * erase element at iterator position
708 * @param pos iterator to element in VLVector
709 * @return iterator to object to the right
710 */
711 iterator erase(iterator pos)
712 {
713 size_t dist = distance(begin(), pos);
714 rotate(pos, pos + 1, end());
715 pop_back();
716 return begin() + dist;
717 }
718
719 /**
720 * erase element at constIterator position
721 * @param pos constIterator to element in VLVector
722 * @return constIterator to object to the right
723 */
724 constIterator erase(constIterator pos)
725 {
726 size_t dist = distance(cbegin(), pos);
727 //can't use rotate with constIterator
728 iterator it = begin() + dist;
729 rotate(it, it + 1, end());
730 pop_back();
731 return begin() + dist;
732 }
733
734 /**
735 * erase range of elements [first, last) within VLVector
736 * @tparam InputIterator generic input iterator type
737 * @param first iterator to VLVector
738 * @param last iterator to VLVector
739 * @return iterator to first position to right of erased elements
15
740 */
741 iterator erase(iterator first, iterator last)
742 {
743 size_t dist1 = distance(begin(), first);
744 size_t dist2 = distance(first, last);
745 rotate(first, last, end());
746 for (size_t i = 0; i < dist2; i++)
747 {
748 pop_back();
749 }
750 return begin() + dist1;
751 }
752
753 /**
754 * erase range of elements [first, last) within VLVector
755 * @tparam InputIterator generic input iterator type
756 * @param first iterator to VLVector
757 * @param last iterator to VLVector
758 * @return constIterator to first position to right of erased elements
759 */
760 constIterator erase(constIterator first, constIterator last)
761 {
762 size_t dist1 = distance(cbegin(), first);
763 size_t dist2 = distance(first, last);
764 //rotate can't be used with constIterator
765 rotate(begin() + dist1, begin() + dist1 + dist2, end());
766 for (size_t i = 0; i < dist2; i++)
767 {
768 pop_back();
769 }
770 return begin() + dist1;
771 }
772
773 /**
774 * get iterator to first element in the VLVector
775 * @return iterator
776 */
777 iterator begin()
778 { return _buff; }
779
780 /**
781 * get const iterator to first element in the VLVector
782 * @return iterator
783 */
784 constIterator begin() const
785 { return _buff; }
786
787 /**
788 * get const iterator unconditionally to first element in the VLVector
789 * @return iterator
790 */
791 constIterator cbegin() const
792 { return _buff; }
793
794 /**
795 * get iterator to position after last element in the VLVector
796 * @return iterator
797 */
798 iterator end()
799 { return _buff + _vectorSize; }
800
801 /**
802 * get const iterator to position after last element in the VLVector
803 * @return iterator
804 */
805 constIterator end() const
806 { return _buff + _vectorSize; }
807
16
808 /**
809 * get const iterator unconditionally to position after last element in the VLVector
810 * @return iterator
811 */
812 constIterator cend() const
813 { return _buff + _vectorSize; }
814
815 private:
816 //data members
817 size_t _vectorSize{}; //number of data entries in VLVector
818 T _staticBuff[C]; //static array to hold data
819 T *_buff; //pointer to data array
820 const size_t _staticCap{}; //static capacity of VLVector
821 size_t _currentCap{}; //current capacity of VLVector
822
823 /**
824 * down-size VLVector capacity
825 * @param size of VLVector
826 * @return pointer to data array of correct capacity
827 */
828 T *_downSize()
829 {
830 _currentCap = _staticCap;
831 //use copy with pointers
832 copy(_buff, _buff + _vectorSize, _staticBuff);
833 delete[] _buff;
834 return _staticBuff;
835 }
836
837 /**
838 * up-size VLVector capacity
839 * @param size of VLVector
840 * @return pointer to data array of correct capacity
841 */
842 T *_upSize(size_t size)
843 {
844 //update current capacity- if size is smaller than static Capacity update to static.
845 _currentCap = _getCurrentCap(size);
846 //we need to allocate memory on the heap
847 T *newBuff = new T[_currentCap];
848 copy(_buff, _buff + _vectorSize, newBuff);
849 if (_buff != _staticBuff)
850 {
851 //meaning we were already working with a dynamically allocated buffer
852 delete[] _buff;
853 }
854 return newBuff;
855 }
856
857 /**
858 * calculate the capacity of a vector given its current size
859 * @param size of vector
860 * @return capacity
861 */
862 size_t _getCurrentCap(size_t size)
863 {
864 //lambda function calculates capacity with respect to current size
865 auto f = [](size_t x) -> size_t
866 { return ((x + 1) * 3) / 2; };
867 return size >= _staticCap ? f(size) : _staticCap;
868 }
869 };
870
871 #endif //EXAM2_VLVector_HPP
17