This repository was archived by the owner on Apr 4, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +42
-41
lines changed
Expand file tree Collapse file tree 4 files changed +42
-41
lines changed Original file line number Diff line number Diff line change 3030* .exe
3131* .out
3232* .app
33+
34+ main.cpp
35+
Original file line number Diff line number Diff line change 11{
22 "project-name" : " custom-cpp-vector" ,
3- "version" : " v1.0.1 " ,
3+ "version" : " v1.1.0 " ,
44 "description" : " Making a custom vector implementation." ,
55 "author" : " Sion Pixley" ,
66 "license" : " MIT; LICENSE" ,
7- "main-file" : " main.cpp " ,
7+ "main-file" : " sion-vector.h " ,
88 "dependencies" : {
99 "c++" : " 14"
1010 },
1111 "dev-dependencies" : {
1212 "clang" : " v11.x"
13- },
14- "scripts" : {
15- "build" : " clang++ -O2 -std=c++14 main.cpp -o main" ,
16- "start" : " ./main" ,
17- "timed-start" : " time ./main"
1813 }
1914}
2015
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 44#include < iostream>
55#include < string>
66
7+
78using std::cout;
89using std::cin;
910using std::endl;
@@ -15,15 +16,15 @@ namespace sion
1516 {
1617 private:
1718 type* elements;
18- size_t max_size ;
19- size_t size ;
19+ size_t max_length ;
20+ size_t length ;
2021
2122 public:
2223 vector ()
2324 {
24- max_size = 10 ;
25- size = 0 ;
26- elements = (type*)(std::malloc (max_size * sizeof (type)));
25+ max_length = 10 ;
26+ length = 0 ;
27+ elements = (type*)(std::malloc (max_length * sizeof (type)));
2728 }
2829
2930 ~vector ()
@@ -35,18 +36,18 @@ namespace sion
3536 {
3637 try
3738 {
38- if (size == max_size )
39+ if (length == max_length )
3940 {
40- max_size *= 2 ;
41- elements = (type*)(std::realloc (elements, max_size * sizeof (type)));
42- elements[size ] = item;
43- size += 1 ;
41+ max_length *= 2 ;
42+ elements = (type*)(std::realloc (elements, max_length * sizeof (type)));
43+ elements[length ] = item;
44+ length += 1 ;
4445 return 0 ;
4546 }
4647 else
4748 {
48- elements[size ] = item;
49- size += 1 ;
49+ elements[length ] = item;
50+ length += 1 ;
5051 return 0 ;
5152 }
5253 }
@@ -56,30 +57,46 @@ namespace sion
5657 }
5758 }
5859
59- size_t length ()
60+ size_t size ()
6061 {
61- return size ;
62+ return length ;
6263 }
6364
6465 type pop_back ()
6566 {
66- if (size == 0 )
67+ if (length == 0 )
6768 {
6869 type t;
6970 return t;
7071 }
7172 else
7273 {
73- type t = elements[size - 1 ];
74- elements[size - 1 ] = NULL ;
75- size -= 1 ;
74+ type t = elements[length - 1 ];
75+ elements[length - 1 ] = NULL ;
76+ length -= 1 ;
7677 return t;
7778 }
7879 }
7980
81+ int clear ()
82+ {
83+ try
84+ {
85+ std::free (elements);
86+ max_length = 10 ;
87+ length = 0 ;
88+ elements = (type*)(std::malloc (max_length * sizeof (type)));
89+ return 0 ;
90+ }
91+ catch (...)
92+ {
93+ return 1 ;
94+ }
95+ }
96+
8097 type& operator [](size_t index)
8198 {
82- if (index >= size )
99+ if (index >= length )
83100 {
84101 cout << " Error. Out of bounds." << endl;
85102 exit (-1 );
@@ -93,3 +110,4 @@ namespace sion
93110}
94111
95112#endif // SION_VECTOR_H
113+
You can’t perform that action at this time.
0 commit comments