-
Notifications
You must be signed in to change notification settings - Fork 1
Implement a file reader class. #19
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
Baryonics
wants to merge
4
commits into
YanzhaoW:master
Choose a base branch
from
Baryonics:implement_reader_class
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.
+1,115
−5
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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,5 @@ | ||
| target_sources( | ||
| core | ||
| PRIVATE binary.cpp | ||
| PUBLIC FILE_SET publicHeaders TYPE HEADERS FILES binary.hpp | ||
| ) |
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,252 @@ | ||
| #include "binary.hpp" | ||
| #include "centipede/data/entry.hpp" | ||
| #include "centipede/util/error_types.hpp" | ||
| #include "centipede/util/return_types.hpp" | ||
| #include <algorithm> | ||
| #include <cassert> | ||
| #include <cstddef> | ||
| #include <cstdint> | ||
| #include <cstring> | ||
| #include <expected> | ||
| #include <fstream> | ||
| #include <functional> | ||
| #include <ios> | ||
| #include <iterator> | ||
| #include <optional> | ||
| #include <ranges> | ||
| #include <type_traits> | ||
| #include <vector> | ||
|
|
||
| namespace centipede::reader | ||
| { | ||
| namespace srs = std::ranges; | ||
| namespace svs = std::views; | ||
| namespace | ||
| { | ||
| template <typename T> | ||
| requires(sizeof(T) == sizeof(uint32_t) and std::is_trivially_copyable_v<T>) | ||
| auto read_from_file(std::ifstream& input_file, T& data) -> EnumError<std::size_t> | ||
| { | ||
| const auto read_size = sizeof(data); | ||
| // NOLINTBEGIN (cppcoreguidelines-pro-type-reinterpret-cast) | ||
| input_file.read(reinterpret_cast<char*>(&data), static_cast<std::streamsize>(read_size)); | ||
| // NOLINTEND (cppcoreguidelines-pro-type-reinterpret-cast) | ||
| if (input_file.gcount() != static_cast<std::streamsize>(read_size)) | ||
| { | ||
| return std::unexpected{ ErrorCode::reader_file_fail_to_read }; | ||
| } | ||
| return read_size; | ||
| } | ||
|
|
||
| template <typename T> | ||
| requires(sizeof(T) == sizeof(uint32_t) and std::is_trivially_copyable_v<T>) | ||
| auto read_from_file(std::ifstream& input_file, std::vector<T>& data) -> EnumError<std::size_t> | ||
| { | ||
| assert(!data.empty()); | ||
| const auto read_size = data.size() * sizeof(T); | ||
| // NOLINTBEGIN (cppcoreguidelines-pro-type-reinterpret-cast) | ||
| input_file.read(reinterpret_cast<char*>(data.data()), static_cast<std::streamsize>(read_size)); | ||
| // NOLINTEND (cppcoreguidelines-pro-type-reinterpret-cast) | ||
| if (input_file.gcount() != static_cast<std::streamsize>(read_size)) | ||
| { | ||
| return std::unexpected{ ErrorCode::reader_file_fail_to_read }; | ||
| } | ||
| return read_size; | ||
| } | ||
|
|
||
| template <typename IterCursor, typename IterEnd> | ||
| struct ChunkPointer | ||
| { | ||
| IterCursor iter; | ||
| IterEnd end; | ||
| EntryPoint<>* entrypoint; | ||
| std::size_t& current_size; | ||
| }; | ||
|
|
||
| auto chunk_check_size_one(auto chunk_ptr) | ||
| { | ||
| assert(chunk_ptr.entrypoint != nullptr); | ||
| return (srs::size(*(chunk_ptr.iter)) == 1U) ? std::optional{ chunk_ptr } : std::nullopt; | ||
| } | ||
|
|
||
| auto chunk_not_end_and_increment(auto chunk_ptr) | ||
| { | ||
| assert(chunk_ptr.entrypoint != nullptr); | ||
| return (++(chunk_ptr.iter) != chunk_ptr.end) ? std::optional{ chunk_ptr } : std::nullopt; | ||
| } | ||
|
|
||
| auto chunk_handle_measurement(auto chunk_ptr) | ||
| { | ||
| assert(chunk_ptr.entrypoint != nullptr); | ||
| (chunk_ptr.entrypoint)->set_measurement(std::get<1>(*(*(chunk_ptr.iter)).begin())); | ||
| return chunk_ptr; | ||
| } | ||
|
|
||
| auto chunk_handle_globals(auto chunk_ptr) | ||
| { | ||
| assert(chunk_ptr.entrypoint != nullptr); | ||
| for (const auto& global : *(chunk_ptr.iter)) | ||
| { | ||
| chunk_ptr.entrypoint->add_global(std::get<0>(global), std::get<1>(global)); | ||
| } | ||
| return chunk_ptr; | ||
| } | ||
|
|
||
| auto chunk_handle_sigma(auto chunk_ptr) | ||
| { | ||
| assert(chunk_ptr.entrypoint != nullptr); | ||
| chunk_ptr.entrypoint->set_sigma(std::get<1>(*(*(chunk_ptr.iter)).begin())); | ||
| return chunk_ptr; | ||
| } | ||
|
|
||
| auto chunk_handle_locals(auto chunk_ptr) | ||
| { | ||
| assert(chunk_ptr.entrypoint != nullptr); | ||
| for (const auto& local : *(chunk_ptr.iter) | svs::values) | ||
| { | ||
| chunk_ptr.entrypoint->add_local(local); | ||
| } | ||
| ++(chunk_ptr.current_size); | ||
| return chunk_ptr; | ||
| } | ||
|
|
||
| auto chunk_end_after_increment(auto chunk_ptr) | ||
| { | ||
| assert(chunk_ptr.entrypoint != nullptr); | ||
| return (++(chunk_ptr.iter) == chunk_ptr.end) ? std::optional{ chunk_ptr } : std::nullopt; | ||
| } | ||
|
|
||
| auto parse_entry_points(const Binary::RawBufferType& input, Binary::BufferType& output) | ||
| -> EnumError<std::size_t> | ||
| { | ||
| if (input.first.at(0) != 0U) | ||
| { | ||
| return std::unexpected{ ErrorCode::reader_file_fail_to_read }; | ||
| } | ||
| constexpr auto chunk_size{ 4 }; | ||
| auto size = std::size_t{}; | ||
| auto zipped = svs::zip(input.first, input.second) | svs::drop(1) | | ||
| svs::chunk_by([](const auto& current, const auto& next) -> auto | ||
| { return std::get<0>(current) != 0U and std::get<0>(next) != 0U; }); | ||
| if (zipped.begin() == zipped.end()) | ||
| { | ||
| return std::unexpected{ ErrorCode::reader_file_fail_to_read }; | ||
| } | ||
| // TODO: Use chunk_view after libc++ supports it. | ||
| auto chunks = | ||
| svs::zip(svs::iota(0), zipped) | | ||
| svs::chunk_by([](const auto& current, const auto& next) -> auto | ||
| { return std::get<0>(current) / chunk_size == std::get<0>(next) / chunk_size; }) | | ||
| svs::transform([](auto&& chunk) -> auto { return chunk | svs::values; }); | ||
|
|
||
| auto is_ok = srs::all_of(svs::zip_transform( | ||
| [&size](auto&& chunk, auto&& entrypoint) -> bool | ||
| { | ||
| auto chunk_ptr = ChunkPointer{ .iter = chunk.begin(), | ||
| .end = chunk.end(), | ||
| .entrypoint = &entrypoint, | ||
| .current_size = size }; | ||
| using ChunkPtrType = decltype(chunk_ptr); | ||
| return chunk_check_size_one(chunk_ptr) | ||
| .transform(chunk_handle_measurement<ChunkPtrType>) | ||
| .and_then(chunk_not_end_and_increment<ChunkPtrType>) | ||
| .transform(chunk_handle_globals<ChunkPtrType>) | ||
| .and_then(chunk_not_end_and_increment<ChunkPtrType>) | ||
| .and_then(chunk_check_size_one<ChunkPtrType>) | ||
| .transform(chunk_handle_sigma<ChunkPtrType>) | ||
| .and_then(chunk_not_end_and_increment<ChunkPtrType>) | ||
| .transform(chunk_handle_locals<ChunkPtrType>) | ||
| .and_then(chunk_end_after_increment<ChunkPtrType>) | ||
| .has_value(); | ||
| }, | ||
| chunks, | ||
| output), | ||
| std::identity{}); | ||
| if (not is_ok) | ||
| { | ||
| return std::unexpected{ ErrorCode::reader_file_fail_to_read }; | ||
| } | ||
| return size; | ||
| } | ||
| } // namespace | ||
|
|
||
| auto Binary::init() -> EnumError<> | ||
| { | ||
| if (config_.in_filename.empty()) | ||
| { | ||
| return std::unexpected{ ErrorCode::reader_invalid_filename }; | ||
| } | ||
| entry_buffer_.resize(config_.max_bufferpoint_size); | ||
| raw_entry_buffer_.first.reserve(config_.max_bufferpoint_size); | ||
| raw_entry_buffer_.second.reserve(config_.max_bufferpoint_size); | ||
| input_file_.open(config_.in_filename, std::ios::binary | std::ios::in); | ||
| if (!input_file_.is_open()) | ||
| { | ||
| return std::unexpected{ ErrorCode::reader_file_fail_to_open }; | ||
| } | ||
| n_entries_ = 0Z; | ||
| end_of_file_ = false; | ||
| return {}; | ||
| } | ||
|
|
||
| auto Binary::read_one_entry() -> EnumError<std::size_t> | ||
| { | ||
| if (entry_buffer_.empty()) | ||
| { | ||
| return std::unexpected{ ErrorCode::reader_uninitialized }; | ||
| } | ||
| reset(); | ||
| auto read_size = uint32_t{}; | ||
| if (auto result = read_from_file(input_file_, read_size); !result) | ||
| { | ||
| if (input_file_.eof() and input_file_.gcount() == 0) | ||
| { | ||
| end_of_file_ = true; | ||
| return 0U; | ||
| } | ||
| return std::unexpected{ result.error() }; | ||
| } | ||
| if (auto result = read_entry_to_buffer(read_size); !result) | ||
| { | ||
| return std::unexpected{ result.error() }; | ||
| } | ||
| if (read_size > config_.max_bufferpoint_size) | ||
| { | ||
| entry_buffer_.resize(read_size); | ||
| } | ||
| auto size = parse_entry_points(raw_entry_buffer_, entry_buffer_); | ||
| if (not size) | ||
| { | ||
| return std::unexpected{ size.error() }; | ||
| } | ||
| ++n_entries_; | ||
| size_ = size.value(); | ||
| return size.value(); | ||
| } | ||
|
|
||
| void Binary::reset() | ||
| { | ||
| for (auto& entrypoint : entry_buffer_) | ||
| { | ||
| entrypoint.reset(); | ||
| } | ||
| raw_entry_buffer_.first.clear(); | ||
| raw_entry_buffer_.second.clear(); | ||
| size_ = 0U; | ||
| } | ||
|
|
||
| auto Binary::read_entry_to_buffer(uint32_t read_size) -> EnumError<> | ||
| { | ||
| raw_entry_buffer_.first.resize(read_size / 2U); | ||
| raw_entry_buffer_.second.resize(read_size / 2U); | ||
| if (auto result = read_from_file(input_file_, raw_entry_buffer_.second); !result) | ||
| { | ||
| return std::unexpected{ result.error() }; | ||
| } | ||
| if (auto result = read_from_file(input_file_, raw_entry_buffer_.first); !result) | ||
| { | ||
| return std::unexpected{ result.error() }; | ||
| } | ||
| return {}; | ||
| } | ||
| } // namespace centipede::reader | ||
Oops, something went wrong.
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.