segmented_sequences.cc 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include "segmented_sequences.h"
  2. #include <algorithm>
  3. SegmentedSequencesLeaf::SegmentedSequencesLeaf(
  4. std::shared_ptr<std::string> sequence)
  5. : sequence_(sequence) {}
  6. void SegmentedSequencesLeaf::Visit(
  7. std::function<void(SegmentedString sequence)> visitor) const {
  8. visitor(SegmentedString{sequence_});
  9. }
  10. size_t SegmentedSequencesLeaf::MinSize() const {
  11. return sequence_->size();
  12. }
  13. SegmentedSequencesAlternatives::SegmentedSequencesAlternatives(
  14. std::vector<std::shared_ptr<SegmentedSequences>> alternatives) {
  15. min_size_ = alternatives[0]->MinSize();
  16. for (const auto &alt : alternatives) {
  17. min_size_ = std::min(min_size_, alt->MinSize());
  18. }
  19. for (const auto &alt : alternatives) {
  20. if (alt->MinSize() == min_size_) {
  21. alternatives_.push_back(alt);
  22. }
  23. }
  24. }
  25. void SegmentedSequencesAlternatives::Visit(
  26. std::function<void(SegmentedString sequence)> visitor) const {
  27. for (const auto &alt : alternatives_) {
  28. alt->Visit(visitor);
  29. }
  30. }
  31. size_t SegmentedSequencesAlternatives::MinSize() const {
  32. return min_size_;
  33. }
  34. SegmentedSequencesProduct::SegmentedSequencesProduct(
  35. std::shared_ptr<SegmentedSequences> a,
  36. std::shared_ptr<SegmentedSequences> b)
  37. : a_(a), b_(b) {
  38. min_size_ = a_->MinSize() + b_->MinSize();
  39. }
  40. void SegmentedSequencesProduct::Visit(
  41. std::function<void(SegmentedString sequence)> visitor) const {
  42. a_->Visit([this, visitor](SegmentedString seq_a) {
  43. b_->Visit([this, visitor, &seq_a](SegmentedString seq_b) {
  44. SegmentedString joined_seq = seq_a;
  45. joined_seq.insert(joined_seq.end(), seq_b.begin(), seq_b.end());
  46. visitor(joined_seq);
  47. });
  48. });
  49. }
  50. size_t SegmentedSequencesProduct::MinSize() const {
  51. return min_size_;
  52. }