| 1 |
#ifndef PBCOPPER_POA_VECTORL_H |
| 2 |
#define PBCOPPER_POA_VECTORL_H |
| 3 |
|
| 4 |
#include <pbcopper/PbcopperConfig.h> |
| 5 |
|
| 6 |
#include <algorithm> |
| 7 |
#include <vector> |
| 8 |
|
| 9 |
#include <cassert> |
| 10 |
#include <cstddef> |
| 11 |
|
| 12 |
namespace PacBio { |
| 13 |
namespace Poa { |
| 14 |
namespace detail { |
| 15 |
|
| 16 |
// This is necessary because of dumb C++ linker rules. |
| 17 |
// see: http://isocpp.org/wiki/faq/templates#template-friends |
| 18 |
// and: http://yosefk.com/c++fqa/templates.html#fqa-35.16 (for hilarious |
| 19 |
// rejoinder) |
| 20 |
template <typename T> |
| 21 |
class VectorL; // fwd |
| 22 |
template <typename T> |
| 23 |
T Max(const VectorL<T>& v); |
| 24 |
template <typename T> |
| 25 |
size_t ArgMax(const VectorL<T>& v); |
| 26 |
|
| 27 |
// |
| 28 |
// Vector class that stores only a subsequence of the rows |
| 29 |
// (SparseVector has a lot of weird functionality built-in, so we can't use it |
| 30 |
// without |
| 31 |
// cleaning it up/refactoring it quite a bit) |
| 32 |
// |
| 33 |
template <typename T> |
| 34 |
class VectorL |
| 35 |
{ |
| 36 |
private: |
| 37 |
std::vector<T> storage_; |
| 38 |
size_t beginRow_; |
| 39 |
size_t endRow_; |
| 40 |
|
| 41 |
public: |
| 42 |
VectorL(int beginRow, int endRow, T defaultVal = T()) |
| 43 |
: storage_(endRow - beginRow, defaultVal), beginRow_(beginRow), endRow_(endRow) |
| 44 |
{} |
| 45 |
|
| 46 |
VectorL(VectorL&&) noexcept = default; |
| 47 |
VectorL& operator=(VectorL&&) noexcept = default; |
| 48 |
|
| 49 |
T& operator[](size_t pos) noexcept |
| 50 |
{ |
| 51 |
assert(beginRow_ <= pos && pos < endRow_); |
| 52 |
return storage_[pos - beginRow_]; |
| 53 |
} |
| 54 |
|
| 55 |
const T& operator[](size_t pos) const noexcept |
| 56 |
{ |
| 57 |
assert(beginRow_ <= pos && pos < endRow_); |
| 58 |
return storage_[pos - beginRow_]; |
| 59 |
} |
| 60 |
|
| 61 |
size_t BeginRow() const { return beginRow_; } |
| 62 |
size_t EndRow() const { return endRow_; } |
| 63 |
friend T Max<>(const VectorL<T>& v); |
| 64 |
friend size_t ArgMax<>(const VectorL<T>& v); |
| 65 |
}; |
| 66 |
|
| 67 |
template <typename T> |
| 68 |
T Max(const VectorL<T>& v) |
| 69 |
{ |
| 70 |
return *std::max_element(v.storage_.begin(), v.storage_.end()); |
| 71 |
} |
| 72 |
|
| 73 |
template <typename T> |
| 74 |
size_t ArgMax(const VectorL<T>& v) |
| 75 |
{ |
| 76 |
return v.beginRow_ + std::distance(v.storage_.begin(), |
| 77 |
std::max_element(v.storage_.begin(), v.storage_.end())); |
| 78 |
} |
| 79 |
|
| 80 |
} // namespace detail |
| 81 |
} // namespace Poa |
| 82 |
} // namespace PacBio |
| 83 |
|
| 84 |
#endif // PBCOPPER_POA_VECTORL_H |