1 #ifndef IMAGE_IO_BASE_DATA_RANGE_H_ // NOLINT 2 #define IMAGE_IO_BASE_DATA_RANGE_H_ // NOLINT 3 4 #include <algorithm> 5 6 namespace photos_editing_formats { 7 namespace image_io { 8 9 /// A class to specify a range of bytes in some sort of array. The range is 10 /// defined like others in STL to include the begin value and exclude the end 11 /// value: [begin,end). Invalid ranges where end <= begin are ok - no exceptions 12 /// are ever thrown - but the IsValid() function will return false, and other 13 /// functions will behave in an appropriate fashion. 14 class DataRange { 15 public: 16 /// The main constructor to define a range. 17 /// @param begin The begin location of the range. 18 /// @param end The end location of the range. DataRange(size_t begin,size_t end)19 DataRange(size_t begin, size_t end) : begin_(begin), end_(end) {} 20 21 /// The default construtor defines an invalid range in which both begin and 22 /// end are set to 0. DataRange()23 DataRange() : begin_(0), end_(0) {} 24 25 DataRange(const DataRange& data_range) = default; 26 DataRange& operator=(const DataRange& data_range) = default; 27 28 /// @return The begin value of the range. GetBegin()29 size_t GetBegin() const { return begin_; } 30 31 /// @return The end value of the rangel. GetEnd()32 size_t GetEnd() const { return end_; } 33 34 /// @return Whether the range is valid. IsValid()35 bool IsValid() const { return begin_ < end_; } 36 37 /// @return The length of the range, or 0 if the range is invalid. GetLength()38 size_t GetLength() const { return IsValid() ? end_ - begin_ : 0; } 39 40 /// Determines if the location is in this range or not. 41 /// @param location The location being considered for this test. 42 /// @return True if the location is in the range, else false. Contains(size_t location)43 bool Contains(size_t location) const { 44 return location >= begin_ && location < end_; 45 } 46 47 /// Determines if another DataRange is a subrange of this range or not. 48 /// @param data_range The DataRange being considered for this test. 49 /// @return True if data_range is subrange of this range, else not. Contains(const DataRange & data_range)50 bool Contains(const DataRange& data_range) const { 51 return IsValid() && data_range.IsValid() && data_range.begin_ >= begin_ && 52 data_range.end_ <= end_; 53 } 54 55 /// Computes the DataRange that is the intersection of another range with this 56 /// one. If there is no intersection, the resulting range will be invalid. 57 /// @param data_range The DataRange to use compute the intersection with this 58 /// one. 59 /// @return The DataRange that represents the intersection, or one that is 60 /// is invalid if the ranges do not overlap at all. GetIntersection(const DataRange & data_range)61 DataRange GetIntersection(const DataRange& data_range) const { 62 return DataRange(std::max(data_range.begin_, begin_), 63 std::min(data_range.end_, end_)); 64 } 65 66 /// @param rhs A DataRange to compare with this one. 67 /// @return True if the two ranges are equal (even if invalid), else false. 68 bool operator==(const DataRange& rhs) const { 69 return begin_ == rhs.begin_ && end_ == rhs.end_; 70 } 71 72 /// @param rhs A DataRange to compare with this one. 73 /// @return True if the two ranges not equal (even if invalid), else false. 74 bool operator!=(const DataRange& rhs) const { 75 return begin_ != rhs.begin_ || end_ != rhs.end_; 76 } 77 78 private: 79 /// The begin value of the range. 80 size_t begin_; 81 82 /// The end value of the range. 83 size_t end_; 84 }; 85 86 } // namespace image_io 87 } // namespace photos_editing_formats 88 89 #endif // IMAGE_IO_BASE_DATA_RANGE_H_ // NOLINT 90