forked from open-telemetry/opentelemetry-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
List and pair #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
62b0c88
added nostd::list and nostd::pair
satac2 9c39536
ran auto formatter
satac2 821bfb4
added tests to build file
satac2 25813c8
Added tests, removed unnecessary code and simplified things
satac2 ec12e39
added assignment tests for pair and comparison test for pair
satac2 7ac58e2
moved some delete statements
satac2 cf403b7
fixed memory leak in list and pair
satac2 09b0d50
fixed list comparison error
satac2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,254 @@ | ||
| #pragma once | ||
|
|
||
| #include <algorithm> | ||
| #include <array> | ||
| #include <cstring> | ||
| #include <iostream> | ||
| #include <iterator> | ||
| #include <memory> | ||
|
|
||
| #include "opentelemetry/nostd/utility.h" | ||
| #include "opentelemetry/version.h" | ||
|
|
||
| OPENTELEMETRY_BEGIN_NAMESPACE | ||
| namespace nostd | ||
| { | ||
| // Non-standard implementation of the list data structure. | ||
| template <class T> | ||
| class list | ||
| { | ||
| public: | ||
| typedef T value_type; | ||
|
|
||
| // Custom iterator class | ||
| class iterator | ||
| { | ||
| public: | ||
| iterator operator++() | ||
| { | ||
| ptr_++; | ||
| return ptr_; | ||
| } | ||
|
|
||
| T &operator*() { return *ptr_; } | ||
|
|
||
| T *operator->() { return ptr_; } | ||
|
|
||
| bool operator==(const iterator &other) { return ptr_ == other.ptr_; } | ||
|
|
||
| bool operator!=(const iterator &other) { return ptr_ != other.ptr_; } | ||
|
|
||
| private: | ||
| friend class list; | ||
|
|
||
| iterator(T *ptr) : ptr_(ptr) {} | ||
|
|
||
| T *ptr_; | ||
| }; | ||
|
|
||
| list() | ||
| { | ||
| size_ = 0; | ||
| data_ = nullptr; | ||
| }; | ||
|
|
||
| // Constructs class with passed in element. | ||
| list(T element) | ||
| { | ||
| size_ = 1; | ||
|
|
||
| data_ = new T[size_]; | ||
|
|
||
| std::memcpy(data_, &element, sizeof(T)); | ||
| }; | ||
|
|
||
| // Constructs class with the passed in initializer_list. | ||
| list(std::initializer_list<T> init_list) | ||
| { | ||
| size_ = init_list.size(); | ||
|
|
||
| data_ = new T[size_]; | ||
|
|
||
| std::memcpy(data_, std::begin(init_list), init_list.size() * sizeof(T)); | ||
| } | ||
|
|
||
| // Returns the element at the front of the list. | ||
| T &front() { return data_[0]; } | ||
|
|
||
| // Returns the element at the back of the list. | ||
| T &back() | ||
| { | ||
| if (size_ == 0) | ||
| { | ||
| return data_[0]; | ||
| } | ||
|
|
||
| return data_[size_ - 1]; | ||
| } | ||
|
|
||
| // Returns an iterator pointing at the front of the list. | ||
| iterator begin() { return iterator(data_); } | ||
|
|
||
| // Returns an iterator pointing at the back of the list. | ||
| iterator end() { return iterator(data_ + size_); } | ||
|
|
||
| // Inserts the passed in element at the passed in | ||
| // iterator position. | ||
| iterator insert(iterator position, const T &element) | ||
| { | ||
| int previous_size = size_; | ||
| size_++; | ||
|
|
||
| T *temp = new T[size_]; | ||
|
|
||
| // Counter tracks the index of the passed in iterator position. | ||
| int counter = position.ptr_ - data_; | ||
|
|
||
| int insert_index = counter; | ||
| int next_index = counter + 1; | ||
| int remaining_elements = previous_size - counter; | ||
|
|
||
| // Copy the original data that comes before the insertion. | ||
| std::memcpy(temp, data_, insert_index * sizeof(T)); | ||
| // Copy the inserted data. | ||
| std::memcpy(temp + insert_index, &element, sizeof(T)); | ||
| // Copy the original data that comes after the insertion. | ||
| std::memcpy(temp + next_index, data_ + counter, remaining_elements * sizeof(T)); | ||
|
|
||
| delete[] data_; | ||
|
|
||
| data_ = temp; | ||
|
|
||
| // Return an iterator at the inserted element. | ||
| return iterator(data_ + counter); | ||
| } | ||
|
|
||
| // Insert an element at the end of the list. | ||
| void push_back(T element) | ||
| { | ||
|
|
||
| int previous_size = size_; | ||
| size_++; | ||
|
|
||
| T *temp = new T[size_]; | ||
|
|
||
| // Copy the original data into the new array. | ||
| for (int i = 0; i < previous_size; i++) | ||
| { | ||
| temp[i] = data_[i]; | ||
| } | ||
|
|
||
| // Deallocate memory used by previous array. | ||
| delete[] data_; | ||
|
|
||
| data_ = temp; | ||
|
|
||
| data_[previous_size] = element; | ||
| }; | ||
|
|
||
| // Insert an element at the front of the list. | ||
| void push_front(T element) | ||
| { | ||
| size_++; | ||
|
|
||
| T *temp = new T[size_]; | ||
|
|
||
| // Copy the original data into the new array leaving space | ||
| // for the new element at the front. | ||
| for (unsigned int i = 1; i < size_; i++) | ||
| { | ||
| temp[i] = data_[i - 1]; | ||
| } | ||
|
|
||
| // Deallocate memory used by previous array. | ||
| delete[] data_; | ||
|
|
||
| data_ = temp; | ||
| data_[0] = element; | ||
| }; | ||
|
|
||
| // Returns the last element and removes it from the list. | ||
| T pop_back() | ||
|
0x4b marked this conversation as resolved.
|
||
| { | ||
| T back_elem = data_[size_ - 1]; | ||
| size_--; | ||
|
|
||
| T *temp = new T[size_]; | ||
|
|
||
| // Copy the original data. | ||
| std::memcpy(temp, data_, size_ * sizeof(T)); | ||
|
|
||
| // Deallocate memory used by previous array. | ||
| delete[] data_; | ||
|
|
||
| data_ = temp; | ||
|
|
||
| return back_elem; | ||
| } | ||
|
|
||
| // Returns the first element and removes it from the list. | ||
| T pop_front() | ||
| { | ||
| T front_elem = front(); | ||
| size_--; | ||
|
|
||
| T *temp = new T[size_]; | ||
|
|
||
| // Copy the original data. | ||
| std::memcpy(temp, data_ + 1, size_ * sizeof(T)); | ||
|
|
||
| // Deallocate memory used by previous array. | ||
| delete[] data_; | ||
|
|
||
| data_ = temp; | ||
|
|
||
| return front_elem; | ||
| } | ||
|
|
||
| // Returns the number of elements in the list. // | ||
| int size() { return size_; } | ||
|
|
||
| list &operator=(const list &other_list) | ||
| { | ||
| delete[] data_; | ||
|
|
||
| data_ = new T[other_list.size_]; | ||
|
|
||
| memcpy(data_, other_list.data_, other_list.size_ * sizeof(T)); | ||
|
|
||
| size_ = other_list.size_; | ||
|
|
||
| return *this; | ||
| } | ||
|
|
||
| list(const list &other_list) | ||
| { | ||
| size_ = other_list.size_; | ||
|
|
||
| data_ = new T[size_]; | ||
|
|
||
| memcpy(data_, other_list.data_, other_list.size_ * sizeof(T)); | ||
| } | ||
|
|
||
| // Returns true if the list contents are the same, false if they are not | ||
| bool operator==(const list &other_list) | ||
| { | ||
| if (size_ != other_list.size_) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| return (memcmp(data_, other_list.data_, size_*sizeof(T)) == 0); | ||
| } | ||
|
|
||
| T &operator[](int index) { return data_[index]; } | ||
|
|
||
| ~list() { delete[] data_; } | ||
|
|
||
| private: | ||
| size_t size_; | ||
|
|
||
| T *data_; | ||
| }; | ||
| } // namespace nostd | ||
| OPENTELEMETRY_END_NAMESPACE | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| #pragma once | ||
|
|
||
| #include "opentelemetry/nostd/utility.h" | ||
| #include "opentelemetry/version.h" | ||
|
|
||
| OPENTELEMETRY_BEGIN_NAMESPACE | ||
| namespace nostd | ||
| { | ||
|
|
||
| // The nostd::pair class provides a data structure capable of hold | ||
| // two values of different or the same types, and provides access to each. | ||
|
|
||
| template <class T1, class T2> | ||
| class pair | ||
| { | ||
| public: | ||
| pair() | ||
| { | ||
| first_ = T1(); | ||
| second_ = T2(); | ||
| } | ||
|
|
||
| // Constructs a pair object with the passed in first and second values. | ||
| pair(T1 first, T2 second) | ||
| { | ||
| first_ = first; | ||
| second_ = second; | ||
| } | ||
|
|
||
| void operator=(const pair &other_pair) | ||
| { | ||
| first_ = other_pair.first_; | ||
| second_ = other_pair.second_; | ||
| } | ||
|
|
||
| bool operator==(const pair &other_pair) | ||
| { | ||
| return (first_ == other_pair.first_ && second_ == other_pair.second_); | ||
| } | ||
|
|
||
| // Returns the first element. | ||
| T1 &first() { return first_; } | ||
|
|
||
| // Returns the second element. | ||
| T2 &second() { return second_; } | ||
|
|
||
| private: | ||
| T1 first_; | ||
| T2 second_; | ||
| }; | ||
| } // namespace nostd | ||
| OPENTELEMETRY_END_NAMESPACE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.