-
Notifications
You must be signed in to change notification settings - Fork 3
Пестов Владимир Алексеевич #5
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
Open
BasedDepartment1
wants to merge
1
commit into
cppdevcourse:master
Choose a base branch
from
BasedDepartment1:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 |
|---|---|---|
| @@ -1,48 +1,193 @@ | ||
| #include "stack.hpp" | ||
|
|
||
| #include <cstddef> | ||
|
|
||
| // TODO: remove me | ||
| #define UNUSED(VAR) (void)(VAR) | ||
| #include <cstdint> | ||
| #include <limits> | ||
| #include <memory> | ||
| #include <stdexcept> | ||
| #include <unordered_map> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| namespace stack | ||
| { | ||
|
|
||
| class Node | ||
| { | ||
| public: | ||
| explicit Node(const uint8_t* data, size_t size) : size_(size) | ||
| { | ||
| data_ = std::make_unique<uint8_t[]>(size); | ||
| std::copy(data, data + size, data_.get()); | ||
| } | ||
|
|
||
| size_t Size() const | ||
| { | ||
| return size_; | ||
| } | ||
|
|
||
| uint8_t* begin() | ||
| { | ||
| return data_.get(); | ||
| } | ||
|
|
||
| uint8_t* end() | ||
| { | ||
| return data_.get() + size_; | ||
| } | ||
|
|
||
| private: | ||
| size_t size_; | ||
| std::unique_ptr<uint8_t[]> data_; | ||
| }; | ||
|
|
||
| class LIFOStack | ||
| { | ||
| public: | ||
| explicit LIFOStack() | ||
| { | ||
| } | ||
|
|
||
| Handle Create() | ||
| { | ||
| auto next_handle = NextHandle(); | ||
| mp_.insert({next_handle, std::vector<Node>()}); | ||
|
|
||
| return next_handle; | ||
| } | ||
|
|
||
| void Destroy(const Handle handle) | ||
| { | ||
| mp_.erase(handle); | ||
| free_handles_.push_back(handle); | ||
| } | ||
|
|
||
| bool IsValid(const Handle handle) const | ||
| { | ||
| return mp_.find(handle) != mp_.end(); | ||
| } | ||
|
|
||
| size_t Count(const Handle handle) const | ||
| { | ||
| if (!IsValid(handle)) | ||
| { | ||
| return 0u; | ||
| } | ||
|
|
||
| const auto& stack = GetByHandle(handle); | ||
| return stack.size(); | ||
| } | ||
|
|
||
| void Push(const Handle handle, const uint8_t* data, size_t size) | ||
| { | ||
| if (!IsValid(handle) || data == nullptr || size == 0) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| auto& stack = GetByHandle(handle); | ||
| stack.emplace_back(data, size); | ||
| } | ||
|
|
||
| size_t Pop(const Handle handle, uint8_t* data, size_t size) | ||
| { | ||
| if (!IsValid(handle) || data == nullptr || size == 0) | ||
| { | ||
| return 0u; | ||
| } | ||
|
|
||
| auto& stack = GetByHandle(handle); | ||
| if (stack.empty()) | ||
| { | ||
| return 0u; | ||
| } | ||
|
|
||
| auto& last_node = stack.back(); | ||
| if (last_node.Size() != size) | ||
| { | ||
| return 0u; | ||
| } | ||
|
|
||
| std::copy(last_node.begin(), last_node.end(), data); | ||
| stack.pop_back(); | ||
|
|
||
| return size; | ||
| } | ||
|
|
||
| private: | ||
| const std::vector<Node>& GetByHandle(const Handle handle) const | ||
| { | ||
| return mp_.at(handle); | ||
| } | ||
|
|
||
| std::vector<Node>& GetByHandle(const Handle handle) | ||
| { | ||
| return mp_.at(handle); | ||
| } | ||
|
|
||
| Handle NextHandle() | ||
| { | ||
| if (current_handle_ != std::numeric_limits<Handle>::max()) | ||
| { | ||
| return current_handle_++; | ||
| } | ||
|
|
||
| if (free_handles_.empty()) | ||
| { | ||
| throw std::runtime_error("all handle numbers have been used"); | ||
| } | ||
|
|
||
| auto free_handle = free_handles_.back(); | ||
| free_handles_.pop_back(); | ||
|
|
||
| return free_handle; | ||
| } | ||
|
|
||
| private: | ||
| std::unordered_map<Handle, std::vector<Node>> mp_{}; | ||
| Handle current_handle_{}; | ||
| std::vector<Handle> free_handles_{}; | ||
| }; | ||
|
|
||
| LIFOStack stack{}; | ||
|
|
||
| Handle create() | ||
| { | ||
| return -1; | ||
| try | ||
| { | ||
| return stack.Create(); | ||
| } | ||
| catch (...) | ||
| { | ||
| return -1; | ||
| } | ||
| } | ||
|
|
||
| void destroy(const Handle handle) | ||
| { | ||
| UNUSED(handle); | ||
| stack.Destroy(handle); | ||
| } | ||
|
|
||
| bool valid(const Handle handle) | ||
| { | ||
| UNUSED(handle); | ||
| return false; | ||
| return stack.IsValid(handle); | ||
| } | ||
|
|
||
| std::size_t count(const Handle handle) | ||
| { | ||
| UNUSED(handle); | ||
| return 0u; | ||
| return stack.Count(handle); | ||
| } | ||
|
|
||
| void push(const Handle handle, const void* const data, const std::size_t size) | ||
| { | ||
| UNUSED(handle); | ||
| UNUSED(data); | ||
| UNUSED(size); | ||
| auto data_to_insert = static_cast<const uint8_t*>(data); | ||
| stack.Push(handle, data_to_insert, size); | ||
| } | ||
|
|
||
| std::size_t pop(const Handle handle, void* const data, const std::size_t size) | ||
| { | ||
| UNUSED(handle); | ||
| UNUSED(data); | ||
| UNUSED(size); | ||
| return 0u; | ||
| auto data_to_fill = static_cast<uint8_t*>(data); | ||
| return stack.Pop(handle, data_to_fill, size); | ||
| } | ||
|
|
||
| } // namespace stack | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Если в этой задаче нельзя использовать smart pointer'ы, то могу переписать на ручной деструктор