singlepp_loaders
Load pre-processed reference datasets for SingleR
Loading...
Searching...
No Matches
labels.hpp
Go to the documentation of this file.
1#ifndef SINGLEPP_LOADERS_LABELS_HPP
2#define SINGLEPP_LOADERS_LABELS_HPP
3
4#include "byteme/PerByte.hpp"
5#include "byteme/RawFileReader.hpp"
6#include "byteme/GzipFileReader.hpp"
7#include "byteme/ZlibBufferReader.hpp"
8
9#include "tatami/tatami.hpp"
10#include "singlepp/singlepp.hpp"
11
12#include <string>
13#include <vector>
14#include <cctype>
15#include <type_traits>
16#include <stdexcept>
17
24
32 bool parallel = false;
33
37 size_t buffer_size = 65536;
38};
39
43namespace internal {
44
45template<typename Label_, bool parallel_>
46std::vector<Label_> load_labels(byteme::Reader& reader) {
47 bool non_empty = false;
48 int current = 0;
49 std::vector<Label_> labels;
50
51 typename std::conditional<parallel_, byteme::PerByte<char>, byteme::PerByteParallel<char> >::type pb(&reader);
52 bool okay = pb.valid();
53 while (okay) {
54 char x = pb.get();
55 okay = pb.advance();
56
57 if (x == '\n') {
58 if (!non_empty) {
59 throw std::runtime_error("label index must be an integer");
60 }
61 labels.push_back(current);
62 current = 0;
63 non_empty = false;
64 } else if (std::isdigit(x)) {
65 non_empty = true;
66 current *= 10;
67 current += (x - '0');
68 } else {
69 throw std::runtime_error("label index must be an integer");
70 }
71 }
72
73 if (non_empty) {
74 labels.push_back(current);
75 }
76
77 return labels;
78}
79
80template<typename Label_>
81std::vector<Label_> load_labels(byteme::Reader& reader, bool parallel) {
82 if (parallel) {
83 return load_labels<Label_, true>(reader);
84 } else {
85 return load_labels<Label_, false>(reader);
86 }
87}
88
89}
107template<typename Label_ = singlepp::DefaultLabel>
108std::vector<Label_> load_labels_from_text_file(const char* path, const LoadLabelsOptions& options) {
109 byteme::RawFileReader reader(path, options.buffer_size);
110 return internal::load_labels<Label_>(reader, options.parallel);
111}
112
123template<typename Label_ = singlepp::DefaultLabel>
124std::vector<Label_> load_labels_from_gzip_file(const char* path, const LoadLabelsOptions& options) {
125 byteme::GzipFileReader reader(path, options.buffer_size);
126 return internal::load_labels<Label_>(reader, options.parallel);
127}
128
140template<typename Label_ = singlepp::DefaultLabel>
141std::vector<Label_> load_labels_from_zlib_buffer(const unsigned char* buffer, size_t len, const LoadLabelsOptions& options) {
142 byteme::ZlibBufferReader reader(buffer, len, 3, options.buffer_size);
143 return internal::load_labels<Label_>(reader, options.parallel);
144}
145
146}
147
148#endif
Load pre-processed single reference datasets.
Definition labels.hpp:23
std::vector< Label_ > load_labels_from_gzip_file(const char *path, const LoadLabelsOptions &options)
Definition labels.hpp:124
std::vector< Label_ > load_labels_from_text_file(const char *path, const LoadLabelsOptions &options)
Definition labels.hpp:108
std::vector< Label_ > load_labels_from_zlib_buffer(const unsigned char *buffer, size_t len, const LoadLabelsOptions &options)
Definition labels.hpp:141
Options for loading labels.
Definition labels.hpp:28
size_t buffer_size
Definition labels.hpp:37
bool parallel
Definition labels.hpp:32