Max OS 0.3
Loading...
Searching...
No Matches
MaxOS::common::Vector< Type > Class Template Reference

Dynamically stores an array of elements. More...

#include <vector.h>

Public Types

typedef Typeiterator
 The iterator type for the Vector.
 

Public Member Functions

 Vector ()
 ______________________________________Implementation__________________________________________________
 
 Vector (int size, Type element)
 Constructor for Vector.
 
 Vector (const Vector< Type > &other)
 Copy constructor for Vector.
 
 Vector (Vector< Type > &&other)
 Move constructor for Vector.
 
 ~Vector ()
 Destructor for Vector, de-allocates the array.
 
Typeoperator[] (uint32_t index) const
 Overloads the [] operator to return the element at the index.
 
Vector< Type > & operator= (const Vector< Type > &other)
 Assignment by copy, data is copied into a new buffer stored in this vector.
 
Vector< Type > & operator= (Vector< Type > &&other)
 Assignment by move, data is moved into the buffer stored in this vector and the other vector is cleared.
 
bool empty () const
 Checks if the Vector is empty.
 
uint32_t size () const
 Returns the number of elements in the Vector.
 
iterator begin () const
 Returns the first element of the Vector.
 
iterator end () const
 Returns the last element of the Vector.
 
iterator find (Type) const
 Finds an element in the Vector and returns the iterator of the element.
 
iterator push_back (Type)
 Adds an element to the end of the vector and returns the iterator of the element.
 
Type pop_back ()
 Removes the last element from the Vector.
 
iterator push_front (Type)
 Adds an element to the front of the Vector and returns the iterator of the element.
 
Type pop_front ()
 Removes the m_first_memory_chunk element from the Vector.
 
void erase (Type)
 Removes all elements from the Vector that are equal to the element.
 
void erase (typename Vector< Type >::iterator position)
 Removes the element at the m_position.
 
void clear ()
 Removes all elements from the Vector.
 
void reserve (size_t amount)
 Reserves space in the Vector for a certain amount of elements.
 
void increase_size ()
 Increases the size of the Vector by doubling the capacity.
 
void iterate (VectorIterationHandler< Type > *)
 Iterates over the Vector and calls the OnRead function of the handler for each element.
 
void iterate (void callback(Type &))
 Iterates over the Vector and calls the callback function for each element.
 

Protected Attributes

Typem_elements
 The array of elements.
 
uint32_t m_size { 0 }
 How many elements are currently stored.
 
uint32_t m_capacity { 1 }
 How many elements can be stored without resizing.
 

Detailed Description

template<class Type>
class MaxOS::common::Vector< Type >

Dynamically stores an array of elements.

Template Parameters
TypeType of the Vector

Definition at line 39 of file vector.h.

Member Typedef Documentation

◆ iterator

template<class Type >
typedef Type* MaxOS::common::Vector< Type >::iterator

The iterator type for the Vector.

Definition at line 46 of file vector.h.

Constructor & Destructor Documentation

◆ Vector() [1/4]

template<class Type >
MaxOS::common::Vector< Type >::Vector ( )

______________________________________Implementation__________________________________________________

Constructor for Vector

Template Parameters
TypeType of the Vector

Definition at line 88 of file vector.h.

88 {
89
90 // Allocate space for the array
91 m_elements = new Type[m_capacity];
92
93 }
uint32_t m_capacity
How many elements can be stored without resizing.
Definition vector.h:43
Type * m_elements
The array of elements.
Definition vector.h:41

◆ Vector() [2/4]

template<class Type >
MaxOS::common::Vector< Type >::Vector ( int  size,
Type  element 
)

Constructor for Vector.

Template Parameters
TypeType of the Vector
Parameters
sizeSize of the Vector
elementElement to fill the Vector with

Definition at line 102 of file vector.h.

102 {
103
104 // Allocate space for the array
105 m_elements = new Type[size];
106 m_capacity = size > 0 ? size : 1;
107 m_size = 0;
108
109 // Push all the elements to the Vector
110 for(int i = 0; i < size; ++i)
111 push_back(element);
112 }
uint32_t size() const
Returns the number of elements in the Vector.
Definition vector.h:273
uint32_t m_size
How many elements are currently stored.
Definition vector.h:42
iterator push_back(Type)
Adds an element to the end of the vector and returns the iterator of the element.
Definition vector.h:333

◆ Vector() [3/4]

template<class Type >
MaxOS::common::Vector< Type >::Vector ( const Vector< Type > &  other)

Copy constructor for Vector.

Template Parameters
TypeThe type of data to be stored
Parameters
otherThe vector to copy from

Definition at line 120 of file vector.h.

121 : m_size(other.m_size),
122 m_capacity(other.m_capacity) {
123 // Copy each element into a new array
124 m_elements = new Type[m_capacity];
125 for(uint32_t i = 0; i < m_size; ++i)
126 m_elements[i] = other.m_elements[i];
127 }

References MaxOS::common::Vector< Type >::m_capacity, MaxOS::common::Vector< Type >::m_elements, and MaxOS::common::Vector< Type >::m_size.

◆ Vector() [4/4]

template<class Type >
MaxOS::common::Vector< Type >::Vector ( Vector< Type > &&  other)

Move constructor for Vector.

Template Parameters
TypeThe type of data to be stored
Parameters
otherThe vector to copy from

Definition at line 135 of file vector.h.

136 : m_elements(other.m_elements),
137 m_size(other.m_size),
138 m_capacity(other.m_capacity) {
139
140 // Clear the other Vector
141 other.m_elements = nullptr;
142 other.m_size = 0;
143 other.m_capacity = 0;
144
145 }

◆ ~Vector()

Destructor for Vector, de-allocates the array.

Template Parameters
TypeType data stored in the Vector

Definition at line 152 of file vector.h.

152 {
153
154 // De-allocate the array
155 delete[] m_elements;
156
157 }

Member Function Documentation

◆ begin()

template<class Type >
Vector< Type >::iterator MaxOS::common::Vector< Type >::begin ( ) const

Returns the first element of the Vector.

Template Parameters
TypeType of the Vector
Returns
The first element of the Vector

Definition at line 283 of file vector.h.

283 {
284 return &m_elements[0];
285 }

Referenced by MaxOS::common::Map< Key, Value >::begin().

◆ clear()

template<class Type >
void MaxOS::common::Vector< Type >::clear ( )

Removes all elements from the Vector.

Template Parameters
TypeType of the Vector

Definition at line 461 of file vector.h.

461 {
462 m_size = 0;
463 }

Referenced by MaxOS::common::Map< Key, Value >::clear(), and MaxOS::filesystem::format::ext2::Ext2Directory::read_from_disk().

◆ empty()

template<class Type >
bool MaxOS::common::Vector< Type >::empty ( ) const

Checks if the Vector is empty.

Template Parameters
TypeType of the Vector
Returns
True if the Vector is empty, false otherwise

Definition at line 321 of file vector.h.

321 {
322 return begin() == end();
323 }
iterator end() const
Returns the last element of the Vector.
Definition vector.h:293
iterator begin() const
Returns the first element of the Vector.
Definition vector.h:283

Referenced by MaxOS::common::Map< Key, Value >::empty(), and MaxOS::common::BlockingLock::release().

◆ end()

template<class Type >
Vector< Type >::iterator MaxOS::common::Vector< Type >::end ( ) const

Returns the last element of the Vector.

Template Parameters
TypeType of the Vector
Returns
The last element of the Vector

Definition at line 293 of file vector.h.

293 {
294 return &m_elements[0] + m_size;
295 }

Referenced by MaxOS::common::Map< Key, Value >::end().

◆ erase() [1/2]

template<class Type >
void MaxOS::common::Vector< Type >::erase ( Type  element)

Removes all elements from the Vector that are equal to the element.

Template Parameters
TypeType of the Vector
Parameters
elementThe element to remove

Definition at line 415 of file vector.h.

415 {
416
417 // Search for the element
418 int hits = 0;
419 for(iterator i = begin(); i != end(); ++i) {
420 // If it is the element we are looking for
421 if(*i == element) {
422 ++hits;
423 } else {
424
425 // If there are hits move the element to the left
426 if(hits > 0)
427 *(i - hits) = *i;
428 }
429
430 }
431
432 // Decrease the size of the Vector
433 m_size -= hits;
434 }
Type * iterator
The iterator type for the Vector.
Definition vector.h:46

References MaxOS::common::Rectangle< Type >::Rectangle().

Referenced by MaxOS::common::Map< Key, Value >::erase(), and MaxOS::common::Map< Key, Value >::erase().

◆ erase() [2/2]

template<class Type >
void MaxOS::common::Vector< Type >::erase ( typename Vector< Type >::iterator  position)

Removes the element at the m_position.

Template Parameters
TypeThe type of the Vector
Parameters
positionThe m_position of the element to remove

Definition at line 442 of file vector.h.

442 {
443
444 // If the m_position is not in the Vector
445 if(position < begin() || position >= end())
446 return;
447
448 // Move all elements one index to the left
449 for(++position; position != end(); ++position)
450 *(position - 1) = *position;
451
452 // Decrease the size of the Vector
453 --m_size;
454 }

◆ find()

template<class Type >
Vector< Type >::iterator MaxOS::common::Vector< Type >::find ( Type  element) const

Finds an element in the Vector and returns the iterator of the element.

Template Parameters
TypeType of the Vector
Parameters
elementThe element to find
Returns
The iterator of the element or the end of the Vector if the element is not found

Definition at line 304 of file vector.h.

304 {
305
306 // Find the element
307 for(iterator i = begin(); i != end(); ++i)
308 if(*i == element)
309 return i;
310
311 // The element must not be in the Vector
312 return end();
313 }

◆ increase_size()

template<class Type >
void MaxOS::common::Vector< Type >::increase_size ( )

Increases the size of the Vector by doubling the capacity.

Template Parameters
TypeType of the Vector

Definition at line 164 of file vector.h.

164 {
165 reserve(m_capacity * 2);
166 }
void reserve(size_t amount)
Reserves space in the Vector for a certain amount of elements.
Definition vector.h:174

Referenced by MaxOS::common::Map< Key, Value >::increase_size().

◆ iterate() [1/2]

template<class Type >
void MaxOS::common::Vector< Type >::iterate ( VectorIterationHandler< Type > *  vector_iteration_handler)

Iterates over the Vector and calls the OnRead function of the handler for each element.

Template Parameters
TypeType of the Vector
Parameters
vector_iteration_handlerThe handler

Definition at line 471 of file vector.h.

471 {
472
473 // Call the OnRead function of the handler for each element
474 for(auto& element : m_elements)
475 vector_iteration_handler->on_read(element);
476
477 // Call the OnEndOfStream function of the handler
478 vector_iteration_handler->on_end_of_stream();
479 }

◆ iterate() [2/2]

template<class Type >
void MaxOS::common::Vector< Type >::iterate ( void   callbackType &)

Iterates over the Vector and calls the callback function for each element.

Template Parameters
TypeType of the Vector
Parameters
callbackThe callback function

Definition at line 488 of file vector.h.

488 {
489
490 // Call the callback function for each element
491 for(auto& element : m_elements)
492 callback(element);
493 }

References MaxOS::common::Rectangle< Type >::Rectangle().

◆ operator=() [1/2]

template<class Type >
Vector< Type > & MaxOS::common::Vector< Type >::operator= ( const Vector< Type > &  other)

Assignment by copy, data is copied into a new buffer stored in this vector.

Template Parameters
TypeType of the Vector
Parameters
otherThe vector to copy from
Returns
This vector, with the copied elements

Definition at line 221 of file vector.h.

221 {
222
223 // Setting to itself?
224 if(this == &other)
225 return *this;
226
227 // Create a new buffer to store the elements
228 delete[] m_elements;
229 m_elements = new Type[other.m_capacity];
230
231 // Copy data
232 m_size = other.m_size;
233 m_capacity = other.m_capacity;
234 for(uint32_t i = 0; i < m_size; ++i)
235 m_elements[i] = other.m_elements[i];
236
237 return *this;
238 }

◆ operator=() [2/2]

template<class Type >
Vector< Type > & MaxOS::common::Vector< Type >::operator= ( Vector< Type > &&  other)

Assignment by move, data is moved into the buffer stored in this vector and the other vector is cleared.

Template Parameters
TypeType of the Vector
Parameters
otherThe vector to copy from
Returns
This vector, with the copied elements

Definition at line 247 of file vector.h.

247 {
248
249 // Moving to itself?
250 if(this == &other)
251 return *this;
252
253 // Move into this vector
254 delete[] m_elements;
255 m_elements = other.m_elements;
256 m_size = other.m_size;
257 m_capacity = other.m_capacity;
258
259 // Remove from other vector
260 other.m_elements = nullptr;
261 other.m_size = 0;
262 other.m_capacity = 0;
263
264 return *this;
265 }

◆ operator[]()

template<class Type >
Type & MaxOS::common::Vector< Type >::operator[] ( uint32_t  index) const

Overloads the [] operator to return the element at the index.

Template Parameters
TypeType of the Vector
Parameters
indexThe index of the element
Returns
the element at the index or the end of the Vector if the index is out of bounds

Definition at line 203 of file vector.h.

203 {
204
205 // If the index is in the Vector
206 if(index < m_size)
207 return m_elements[index];
208
209 // Return the last element of the Vector
210 return m_elements[m_size - 1];
211
212 }

◆ pop_back()

template<class Type >
Type MaxOS::common::Vector< Type >::pop_back ( )

Removes the last element from the Vector.

Template Parameters
TypeType of the Vector
Returns
The element that was removed

Definition at line 350 of file vector.h.

350 {
351
352 // Remove the last element from the Vector
353 if(m_size > 0)
354 --m_size;
355
356 return m_elements[m_size];
357 }

Referenced by MaxOS::common::Map< Key, Value >::pop_back().

◆ pop_front()

template<class Type >
Type MaxOS::common::Vector< Type >::pop_front ( )

Removes the m_first_memory_chunk element from the Vector.

Template Parameters
TypeType of the Vector
Returns
The element that was removed, or a default constructed element if the Vector is empty

Definition at line 390 of file vector.h.

390 {
391
392 // Make sure the Vector is not empty
393 if(m_size == 0)
394 return Type();
395
396 // Store the element to return
397
398 Type element = m_elements[0];
399
400 // Move all elements one index to the left
401 for(uint32_t i = 0; i < m_size - 1; ++i)
402 m_elements[i] = m_elements[i + 1];
403
404 // Decrease the size of the Vector
405 --m_size;
406 return element;
407 }

References MaxOS::common::Rectangle< Type >::Rectangle().

Referenced by MaxOS::common::Map< Key, Value >::pop_front(), and MaxOS::common::BlockingLock::release().

◆ push_back()

template<class Type >
Vector< Type >::iterator MaxOS::common::Vector< Type >::push_back ( Type  element)

Adds an element to the end of the vector and returns the iterator of the element.

Template Parameters
TypeType of the Vector
Parameters
elementThe element to add
Returns
The iterator of the element, if the Vector is full it returns the end of the Vector

Definition at line 333 of file vector.h.

333 {
334
335 // Check if we need to allocate more space for the array
336 if(m_size == m_capacity)
338
339 // Add the element to the Vector and return the iterator of the element
340 m_elements[m_size++] = element;
341 return end() - 1;
342 }
void increase_size()
Increases the size of the Vector by doubling the capacity.
Definition vector.h:164

References MaxOS::common::Rectangle< Type >::Rectangle().

Referenced by MaxOS::common::BlockingLock::acquire(), MaxOS::filesystem::format::ext2::InodeHandler::InodeHandler(), MaxOS::common::Map< Key, Value >::insert(), MaxOS::common::Map< Key, Value >::push_back(), and MaxOS::common::EventManager< EventType >::raise_event().

◆ push_front()

template<class Type >
Vector< Type >::iterator MaxOS::common::Vector< Type >::push_front ( Type  element)

Adds an element to the front of the Vector and returns the iterator of the element.

Template Parameters
TypeType of the Vector
Parameters
elementThe element to add
Returns
The iterator of the element, if the Vector is full it returns the end of the Vector

Definition at line 366 of file vector.h.

366 {
367
368 // Check if we need to allocate more space for the array
369 if(m_size == m_capacity)
371
372 // Move all elements one index to the right
373 for(iterator i = end(); i > begin(); --i)
374 *i = *(i - 1);
375
376 // Add the element to the front of the Vector
377 m_elements[0] = element;
378 ++m_size;
379
380 // Return the iterator of the element
381 return begin();
382 }

References MaxOS::common::Rectangle< Type >::Rectangle().

Referenced by MaxOS::common::Map< Key, Value >::push_front().

◆ reserve()

template<class Type >
void MaxOS::common::Vector< Type >::reserve ( size_t  amount)

Reserves space in the Vector for a certain amount of elements.

Template Parameters
TypeType of the Vector
Parameters
amountThe amount of elements to reserve space for

Definition at line 174 of file vector.h.

174 {
175
176
177 // Increase the capacity of the Vector
178 if(m_capacity < amount)
179 m_capacity = amount;
180
181 // Allocate more space for the array
182 Type* new_elements = new Type[amount];
183
184 // Copy the elements to the new array
185 for(uint32_t i = 0; i < m_size; ++i)
186 new_elements[i] = m_elements[i];
187
188 // De-allocate the old array
189 delete[] m_elements;
190
191 // Set the new array
192 m_elements = new_elements;
193 }

References MaxOS::common::Rectangle< Type >::Rectangle().

Referenced by MaxOS::common::Map< Key, Value >::reserve().

◆ size()

template<class Type >
uint32_t MaxOS::common::Vector< Type >::size ( ) const

Returns the number of elements in the Vector.

Template Parameters
TypeType of the Vector
Returns
The size of the Vector

Definition at line 273 of file vector.h.

273 {
274 return m_size;
275 }

Referenced by MaxOS::common::Map< Key, Value >::size().

Member Data Documentation

◆ m_capacity

template<class Type >
uint32_t MaxOS::common::Vector< Type >::m_capacity { 1 }
protected

How many elements can be stored without resizing.

Definition at line 43 of file vector.h.

43{ 1 };

Referenced by MaxOS::common::Vector< Type >::Vector().

◆ m_elements

template<class Type >
Type* MaxOS::common::Vector< Type >::m_elements
protected

The array of elements.

Definition at line 41 of file vector.h.

Referenced by MaxOS::common::Vector< Type >::Vector().

◆ m_size

template<class Type >
uint32_t MaxOS::common::Vector< Type >::m_size { 0 }
protected

How many elements are currently stored.

Definition at line 42 of file vector.h.

42{ 0 };

Referenced by MaxOS::common::Vector< Type >::Vector().


The documentation for this class was generated from the following file: