-
Notifications
You must be signed in to change notification settings - Fork 3
Фролов Кирилл #1
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
base: master
Are you sure you want to change the base?
Changes from all commits
da4765b
4707b9c
a330fa5
284fb72
909e3f8
a93e8ef
224c1d4
304c39e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,48 +1,160 @@ | ||
| #include "stack.hpp" | ||
|
|
||
| #include <cstddef> | ||
|
|
||
| // TODO: remove me | ||
| #define UNUSED(VAR) (void)(VAR) | ||
| #include <unordered_map> | ||
| #include <vector> | ||
| #include <stdexcept> | ||
| #include <queue> | ||
| #include <limits> | ||
|
|
||
| namespace stack | ||
| { | ||
|
|
||
| Handle create() | ||
| { | ||
| return -1; | ||
| } | ||
| struct Node { | ||
| std::vector<char> data; | ||
| Node* next; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Раз завернули данные в |
||
|
|
||
| void destroy(const Handle handle) | ||
| { | ||
| UNUSED(handle); | ||
| } | ||
| Node(const void* data_ptr, std::size_t data_size) | ||
| : data(static_cast<const char*>(data_ptr), | ||
| static_cast<const char*>(data_ptr) + data_size), | ||
| next(nullptr) { | ||
| } | ||
| }; | ||
|
|
||
| bool valid(const Handle handle) | ||
| { | ||
| UNUSED(handle); | ||
| return false; | ||
| } | ||
| struct Stack { | ||
| Node* top; | ||
| std::size_t count; | ||
|
|
||
| Stack() : top(nullptr), count(0) { | ||
| } | ||
| }; | ||
|
|
||
| std::size_t count(const Handle handle) | ||
| { | ||
| UNUSED(handle); | ||
| return 0u; | ||
| } | ||
| std::unordered_map<Handle, Stack> stacks; | ||
| std::queue<Handle> available_handles; | ||
| Handle next_new_handle = 0; | ||
|
|
||
| void push(const Handle handle, const void* const data, const std::size_t size) | ||
| { | ||
| UNUSED(handle); | ||
| UNUSED(data); | ||
| UNUSED(size); | ||
| } | ||
| Handle allocate_handle() { | ||
| if (!available_handles.empty()) { | ||
| Handle handle = available_handles.front(); | ||
| available_handles.pop(); | ||
| return handle; | ||
| } | ||
|
|
||
| if (next_new_handle < std::numeric_limits<Handle>::max()) { | ||
| return next_new_handle++; | ||
| } | ||
|
|
||
| return -1; | ||
| } | ||
|
|
||
| void deallocate_handle(Handle handle) { | ||
| if (handle != -1) { | ||
| available_handles.push(handle); | ||
| } | ||
| } | ||
|
|
||
| std::size_t pop(const Handle handle, void* const data, const std::size_t size) | ||
| { | ||
| UNUSED(handle); | ||
| UNUSED(data); | ||
| UNUSED(size); | ||
| return 0u; | ||
| } | ||
| Handle create() | ||
| { | ||
| try { | ||
| Handle handle = allocate_handle(); | ||
| if (handle == -1) { | ||
| return -1; | ||
| } | ||
|
|
||
| stacks.emplace(handle, Stack()); | ||
| return handle; | ||
| } catch (...) { | ||
| return -1; | ||
| } | ||
| } | ||
|
|
||
| void destroy(const Handle handle) | ||
| { | ||
| auto it = stacks.find(handle); | ||
| if (it == stacks.end()) { | ||
| return; | ||
| } | ||
|
|
||
| Stack& stack = it->second; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Посмотрите срабатывания стат. анализатора (шаг SAST в Workflow). Он вам там подсказывает, что тут можно можно сделать ссылку константной. |
||
| Node* current = stack.top; | ||
| while (current != nullptr) { | ||
| Node* next = current->next; | ||
| delete current; | ||
| current = next; | ||
| } | ||
|
|
||
| stacks.erase(it); | ||
| deallocate_handle(handle); | ||
| } | ||
|
|
||
| bool valid(const Handle handle) | ||
| { | ||
| return stacks.find(handle) != stacks.end(); | ||
| } | ||
|
|
||
| std::size_t count(const Handle handle) | ||
| { | ||
| auto it = stacks.find(handle); | ||
| if (it == stacks.end()) { | ||
| return 0; | ||
| } | ||
| return it->second.count; | ||
| } | ||
|
|
||
| void push(const Handle handle, const void* const data, const std::size_t size) | ||
| { | ||
| if (data == nullptr || size == 0) { | ||
| return ; | ||
| } | ||
|
|
||
| auto it = stacks.find(handle); | ||
| if (it == stacks.end()) { | ||
| return; | ||
| } | ||
|
|
||
| Stack& stack = it->second; | ||
|
|
||
| try { | ||
| Node* new_node = new Node(data, size); | ||
|
|
||
| new_node->next = stack.top; | ||
| stack.top = new_node; | ||
| stack.count++; | ||
|
|
||
| } catch (...) { | ||
| } | ||
| } | ||
|
|
||
| std::size_t pop(const Handle handle, void* const data, const std::size_t size) | ||
| { | ||
| if (data == nullptr || size == 0) { | ||
| return 0; | ||
| } | ||
|
|
||
| auto it = stacks.find(handle); | ||
| if (it == stacks.end() || it->second.top == nullptr) { | ||
| return 0; | ||
| } | ||
|
|
||
| Stack& stack = it->second; | ||
| Node* top_node = stack.top; | ||
|
|
||
| if (size < top_node->data.size()) { | ||
| return 0; | ||
| } | ||
|
|
||
| std::size_t copy_size = std::min(size, top_node->data.size()); | ||
| const char* source_data = top_node->data.data(); | ||
| char* dest_data = static_cast<char*>(data); | ||
|
|
||
| for (std::size_t i = 0; i < copy_size; ++i) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Можно использовать |
||
| dest_data[i] = source_data[i]; | ||
| } | ||
|
|
||
| stack.top = top_node->next; | ||
| delete top_node; | ||
| stack.count--; | ||
|
|
||
| return copy_size; | ||
| } | ||
| } // namespace stack | ||
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.
Кажется, заголовок не используется. Вы везде ловите исключения через
...