项目作者: childsish

项目描述 :
A header only C++ interval and interval set
高级语言: C++
项目地址: git://github.com/childsish/interval.git
创建时间: 2017-06-25T19:22:12Z
项目社区:https://github.com/childsish/interval

开源协议:

下载


interval

A C++ header library implementing an interval and interval set.

Interval

The library implements an Interval class and an IntervalSet class. Intervals provide basic set and arithmetic functions,
functions for converting position relative and absolute to the interval and a function to get the subsequence covered
by the interval. The subsequence functionpermits any type that implements operator+ between the iterator and the
start/stop types.

Intervals have been templated to allow the start and stop types to be any type that behaves like a
number. If using a class, then comparator, arithmetic and shift operators must be implemented. The shift operators must
return integers.

IntervalSet

The IntervalSet has been implemented to provide fast lookup of intervals using interval binning. The get function will
return all intervals in the set that overlap the query.

Usage

Overlapping intervals.

  1. Interval<int> A(0, 2);
  2. Interval<int> B(1, 3);
  3. Interval<int> C(2, 4);
  4. A.overlaps(B); // true
  5. A.overlaps(C); // false
  6. B.overlaps(C); // true

Custom position types.

  1. class GenomicPosition {
  2. public:
  3. const std::string chromosome;
  4. const int position;
  5. bool operator<(const GenomicPosition &that) const;
  6. bool operator==(const GenomicPosition &that) const;
  7. GenomicPosition operator+(const GenomicPosition &that) const;
  8. GenomicPosition operator-(const GenomicPosition &that) const;
  9. GenomicPosition operator*(const GenomicPosition &that) const;
  10. GenomicPosition operator/(const GenomicPosition &that) const;
  11. int operator<<(int amount) const;
  12. int operator>>(int amount) const;
  13. }
  14. Interval<GenomicPosition> A{{"chr1", 0}, {"chr1", 100000}};
  15. Interval<GenomicPosition> B{{"chr1", 50000}, {"chr1", 150000}};
  16. A.overlaps(B); // true

The get_subsequence function.

  1. Interval<int> interval{3, 6};
  2. interval.get_subsequence<std::string>("abcdefghij"); // "def"
  3. interval.get_subsequence<std::vector<int>>({0, 1, 2, 3, 4, 5, 6, 7, 8, 9}); // {3, 4, 5}

The interval set.

  1. IntervalSet<int> set;
  2. set.add(Interval<int>{0, 10});
  3. set.add(Interval<int>{2, 12});
  4. set.add(Interval<int>{4, 14});
  5. set.add(Interval<int>{6, 16});
  6. set.add(Interval<int>{8, 18});
  7. set.add(Interval<int>{10, 20});
  8. std::vector<Interval<int>> intervals = set.get(8, 11); // get all intervals
  9. std::vector<Interval<int>> intervals = set.get(14, 18); // get intervals [6, 16), [8, 18), [10, 20)