PLSSVM - Parallel Least Squares Support Vector Machine
2.0.0
A Least Squares Support Vector Machine implementation using different backends.
|
The plssvm::detail::file_reader class is responsible for reading a file and splitting it into its lines. More...
#include <file_reader.hpp>
Public Member Functions | |
file_reader ()=default | |
Creates a new file_reader without associating it to a file. | |
file_reader (const char *filename) | |
Create a new file_reader and associate it to the filename by opening it (possibly memory mapping it). More... | |
file_reader (const std::string &filename) | |
Create a new file_reader and associate it to the filename by opening it (possibly memory mapping it). More... | |
file_reader (const std::filesystem::path &filename) | |
Create a new file_reader and associate it to the filename by opening it (possibly memory mapping it). More... | |
~file_reader () | |
Closes the associated file. More... | |
file_reader (const file_reader &)=delete | |
Delete the copy-constructor since file_reader is move-only. | |
file_reader (file_reader &&other) noexcept | |
Implement the move-constructor since file_reader is move-only. More... | |
file_reader & | operator= (const file_reader &)=delete |
Delete the copy-assignment operator since file_reader is move-only. More... | |
file_reader & | operator= (file_reader &&other) noexcept |
Implement the move-assignment operator since file_reader is move-only. More... | |
void | open (const char *filename) |
Associates the current file_reader with the file denoted by filename , i.e., opens the file filename (possible memory mapping it). More... | |
void | open (const std::string &filename) |
Associates the current file_reader with the file denoted by filename , i.e., opens the file filename (possible memory mapping it). More... | |
void | open (const std::filesystem::path &filename) |
Associates the current file_reader with the file denoted by filename , i.e., opens the file filename (possible memory mapping it). More... | |
bool | is_open () const noexcept |
Checks whether this file_reader is currently associated with a file. More... | |
void | close () |
Closes the associated file. More... | |
void | swap (file_reader &other) |
Element-wise swap all contents of *this with other . More... | |
const std::vector< std::string_view > & | read_lines (std::string_view comment={ "\n" }) |
Read the content of the associated file and split it into lines, ignoring empty lines and lines starting with the comment . More... | |
const std::vector< std::string_view > & | read_lines (char comment) |
Read the content of the associated file and split it into lines, ignoring empty lines and lines starting with the comment . More... | |
std::vector< std::string_view >::size_type | num_lines () const noexcept |
Return the number of parsed lines (where all empty lines or lines starting with a comment are ignored). More... | |
std::string_view | line (typename std::vector< std::string_view >::size_type pos) const |
Return the pos line of the parsed file. More... | |
const std::vector< std::string_view > & | lines () const noexcept |
Return all lines present after the preprocessing. More... | |
const char * | buffer () const noexcept |
Return the underlying file content as one large string. More... | |
Private Member Functions | |
void | open_memory_mapped_file_unix (const char *filename) |
Try to open the file filename and "read" its content using memory mapped IO on UNIX systems. More... | |
void | open_memory_mapped_file_windows (const char *filename) |
Try to open the file filename and "read" its content using memory mapped IO on Windows systems. More... | |
void | open_file (const char *filename) |
Read open the file and read its content in one buffer using a normal std::ifstream. More... | |
Private Attributes | |
char * | file_content_ { nullptr } |
The content of the file. Pointer to the memory mapped area or to a separately allocated memory area holding the file's content. If the file is empty, corresponds to a nullptr ! | |
std::streamsize | num_bytes_ { 0 } |
The number of bytes stored in file_content_. | |
std::vector< std::string_view > | lines_ {} |
The parsed content of file_content_: a vector of all lines that are not empty and do not start with the provided comment. | |
bool | is_open_ { false } |
true if a file is currently associated wih this file_reader, false otherwise. | |
The plssvm::detail::file_reader class is responsible for reading a file and splitting it into its lines.
If the necessary headers are present, the class tries to memory map the given file. If this fails or if the headers are not present, the file is read as one blob using std::ifstream::read
.
|
explicit |
Create a new file_reader and associate it to the filename
by opening it (possibly memory mapping it).
[in] | filename | the file to open |
plssvm::file_not_found_exception | if the filename couldn't be found |
|
explicit |
Create a new file_reader and associate it to the filename
by opening it (possibly memory mapping it).
[in] | filename | the file to open |
plssvm::file_not_found_exception | if the filename couldn't be found |
|
explicit |
Create a new file_reader and associate it to the filename
by opening it (possibly memory mapping it).
[in] | filename | the file to open |
plssvm::file_not_found_exception | if the filename couldn't be found |
plssvm::detail::io::file_reader::~file_reader | ( | ) |
Closes the associated file.
If memory mapped IO has been used, unmap the file and close the file descriptor, and delete the allocated buffer.
|
noexcept |
Implement the move-constructor since file_reader is move-only.
[in,out] | other | the file_reader to move the values from |
|
delete |
Delete the copy-assignment operator since file_reader is move-only.
|
noexcept |
Implement the move-assignment operator since file_reader is move-only.
[in,out] | other | the file_reader to move the values from |
*this
void plssvm::detail::io::file_reader::open | ( | const char * | filename | ) |
Associates the current file_reader with the file denoted by filename
, i.e., opens the file filename
(possible memory mapping it).
This function is called by the constructor of file_reader accepting a std::string and is not usually invoked directly.
[in] | filename | the file to open |
plssvm::file_reader_exception | if the file_reader has already opened another file |
plssvm::file_not_found_exception | if the filename couldn't be found |
void plssvm::detail::io::file_reader::open | ( | const std::string & | filename | ) |
Associates the current file_reader with the file denoted by filename
, i.e., opens the file filename
(possible memory mapping it).
This function is called by the constructor of file_reader accepting a std::string and is not usually invoked directly.
[in] | filename | the file to open |
plssvm::file_reader_exception | if the file_reader has already opened another file |
plssvm::file_not_found_exception | if the filename couldn't be found |
void plssvm::detail::io::file_reader::open | ( | const std::filesystem::path & | filename | ) |
Associates the current file_reader with the file denoted by filename
, i.e., opens the file filename
(possible memory mapping it).
This function is called by the constructor of file_reader accepting a std::string and is not usually invoked directly.
[in] | filename | the file to open |
plssvm::file_reader_exception | if the file_reader has already opened another file |
plssvm::file_not_found_exception | if the filename couldn't be found |
|
noexcept |
Checks whether this file_reader is currently associated with a file.
true
if a file is currently open, false
otherwise ([[nodiscard]]
) void plssvm::detail::io::file_reader::close | ( | ) |
Closes the associated file.
If memory mapped IO has been used, unmap the file and close the file descriptor, and delete the allocated buffer. This function is called by the destructor of file_reader when the object goes out of scope and is not usually invoked directly.
void plssvm::detail::io::file_reader::swap | ( | file_reader & | other | ) |
Element-wise swap all contents of *this
with other
.
[in,out] | other | the other file_reader to swap the contents with |
const std::vector<std::string_view>& plssvm::detail::io::file_reader::read_lines | ( | std::string_view | comment = { "\n" } | ) |
Read the content of the associated file and split it into lines, ignoring empty lines and lines starting with the comment
.
[in] | comment | a character (sequence) at the beginning of a line that causes this line to be ignored (used to filter comments) |
plssvm::file_reader_exception | if no file is currently associated to this file_reader |
comment
const std::vector<std::string_view>& plssvm::detail::io::file_reader::read_lines | ( | char | comment | ) |
Read the content of the associated file and split it into lines, ignoring empty lines and lines starting with the comment
.
[in] | comment | a character (sequence) at the beginning of a line that causes this line to be ignored (used to filter comments) |
plssvm::file_reader_exception | if no file is currently associated to this file_reader |
comment
|
noexcept |
Return the number of parsed lines (where all empty lines or lines starting with a comment are ignored).
Returns 0
if no file is currently associated with this file_reader or the read_lines() function has not been called yet.
[[nodiscard]]
) std::string_view plssvm::detail::io::file_reader::line | ( | typename std::vector< std::string_view >::size_type | pos | ) | const |
Return the pos
line of the parsed file.
Returns 0
if no file is currently associated with this file_reader or the read_lines() function has not been called yet.
[in] | pos | the line to return |
[[nodiscard]]
)
|
noexcept |
Return all lines present after the preprocessing.
Returns 0
if no file is currently associated with this file_reader or the read_lines() function has not been called yet.
[[nodiscard]]
)
|
noexcept |
Return the underlying file content as one large string.
[[nodiscard]]
)
|
private |
Try to open the file filename
and "read" its content using memory mapped IO on UNIX systems.
If the file could not be memory mapped, automatically falls back to open_file().
[in] | filename | the file to open |
plssvm::file_not_found_exception | if the filename couldn't be found |
|
private |
Try to open the file filename
and "read" its content using memory mapped IO on Windows systems.
If the file could not be memory mapped, automatically falls back to open_file().
[in] | filename | the file to open |
plssvm::file_not_found_exception | if the filename couldn't be found |
|
private |
Read open the file and read its content in one buffer using a normal std::ifstream.
[in] | filename | the file to open |
plssvm::file_not_found_exception | if the filename couldn't be found |
plssvm::invalid_file_format_exception | if an error occurs while reading the contents of the filename |