This is an automated email from the ASF dual-hosted git repository.

haonan pushed a commit to branch iotdb
in repository https://gitbox.apache.org/repos/asf/tsfile.git

commit 1e86410150626aea5050a1436d48ac7df20d1cbe
Author: Hongzhi Gao <[email protected]>
AuthorDate: Thu Jun 27 11:41:54 2024 +0800

    [CPP] Fix syntax and logic errors in files under the 'filter' directory 
(#135)
    
    * bugfix: bitmap clear method
    
    * fix filter
    
    * fix filter
    
    ---------
    
    Co-authored-by: hongzhigao <[email protected]>
---
 cpp/src/reader/filter/between.h   |   2 +
 cpp/src/reader/filter/eq.h        |  21 ++---
 cpp/src/reader/filter/gt.h        |  23 ++---
 cpp/src/reader/filter/gt_eq.h     |  23 ++---
 cpp/src/reader/filter/in.h        |   8 +-
 cpp/src/reader/filter/lt.h        |  28 +++---
 cpp/src/reader/filter/lt_eq.h     |  25 +++---
 cpp/src/reader/filter/not_eq.h    |  27 +++---
 cpp/src/reader/filter/object.h    | 182 +++++++++++++++++++++++++++++++++++++-
 cpp/src/reader/filter/or_filter.h |  53 +++++++----
 10 files changed, 299 insertions(+), 93 deletions(-)

diff --git a/cpp/src/reader/filter/between.h b/cpp/src/reader/filter/between.h
index 655c7aee..1928a36f 100644
--- a/cpp/src/reader/filter/between.h
+++ b/cpp/src/reader/filter/between.h
@@ -19,7 +19,9 @@
 #ifndef READER_FILTER_OPERATOR_BETWEEN_H
 #define READER_FILTER_OPERATOR_BETWEEN_H
 
+#include "common/statistic.h"
 #include "reader/filter/binary_filter.h"
+#include "reader/filter/object.h"
 
 namespace storage {
 template <typename T>
diff --git a/cpp/src/reader/filter/eq.h b/cpp/src/reader/filter/eq.h
index eb577840..13e917b9 100644
--- a/cpp/src/reader/filter/eq.h
+++ b/cpp/src/reader/filter/eq.h
@@ -19,6 +19,7 @@
 #ifndef READER_FILTER_OPERATOR_EQ_H
 #define READER_FILTER_OPERATOR_EQ_H
 
+#include "reader/filter/object.h"
 #include "reader/filter/unary_filter.h"
 
 namespace storage {
@@ -26,14 +27,14 @@ template <typename T>
 class Eq : public UnaryFilter<T> {
    public:
     Eq() : UnaryFilter<T>() {}
-    Eq(T value, FilterType type) { UnaryFilter<T>(value, type); }
+    Eq(T value, FilterType type) : UnaryFilter<T>(value, type) {}
 
     virtual ~Eq() {}
 
     bool satisfy(Statistic *statistic) {
-        if (type_ == TIME_FILTER) {
-            return value_ >= statistic->start_time_ &&
-                   value_ <= statistic->end_time_;
+        if (this->type_ == TIME_FILTER) {
+            return this->value_ >= statistic->start_time_ &&
+                   this->value_ <= statistic->end_time_;
         } else {
             if (statistic->get_type() == common::TEXT ||
                 statistic->get_type() == common::BOOLEAN) {
@@ -46,21 +47,21 @@ class Eq : public UnaryFilter<T> {
     }
 
     bool satisfy(long time, Object value) {
-        Object v = (type_ == TIME_FILTER ? time : value);
-        return value_.equals(v);
+        Object v = (this->type_ == TIME_FILTER ? time : value);
+        return this->value_.equals(v);
     }
 
     bool satisfy_start_end_time(long start_time, long end_time) {
-        if (type_ == TIME_FILTER) {
-            return value_ <= end_time && value_ >= start_time;
+        if (this->type_ == TIME_FILTER) {
+            return this->value_ <= end_time && this->value_ >= start_time;
         } else {
             return true;
         }
     }
 
     bool contain_start_end_time(long start_time, long end_time) {
-        if (type_ == TIME_FILTER) {
-            return value_ == start_time && time == end_time;
+        if (this->type_ == TIME_FILTER) {
+            return this->value_ == start_time && this->value_ == end_time;
         } else {
             return true;
         }
diff --git a/cpp/src/reader/filter/gt.h b/cpp/src/reader/filter/gt.h
index e3cde134..5db7b970 100644
--- a/cpp/src/reader/filter/gt.h
+++ b/cpp/src/reader/filter/gt.h
@@ -19,19 +19,20 @@
 #ifndef READER_FILTER_OPERATOR_GT_H
 #define READER_FILTER_OPERATOR_GT_H
 
-#include "storage/read/filter/unary_filter.h"
+#include "reader/filter/object.h"
+#include "reader/filter/unary_filter.h"
 
 namespace storage {
 template <typename T>
 class Gt : public UnaryFilter<T> {
    public:
-    Gt() : UnaryFilter() {}
-    Gt(T value, FilterType type) { UnaryFilter(value, type); }
+    Gt() : UnaryFilter<T>() {}
+    Gt(T value, FilterType type) : UnaryFilter<T>(value, type) {}
     virtual ~Gt() {}
 
     bool satisfy(Statistic *statistic) {
-        if (type_ == TIME_FILTER) {
-            return value_ < statistic.end_time_;
+        if (this->type_ == TIME_FILTER) {
+            return this->value_ < statistic->end_time_;
         } else {
             if (statistic->get_type() == common::TEXT ||
                 statistic->get_type() == common::BOOLEAN) {
@@ -44,21 +45,21 @@ class Gt : public UnaryFilter<T> {
     }
 
     bool satisfy(long time, Object value) {
-        Object v = (type_ == TIME_FILTER ? time : value);
-        return value_ < v;
+        Object v = (this->type_ == TIME_FILTER ? time : value);
+        return this->value_ < v;
     }
 
     bool satisfy_start_end_time(long start_time, long end_time) {
-        if (type_ == TIME_FILTER) {
-            return value_ < end_time;
+        if (this->type_ == TIME_FILTER) {
+            return this->value_ < end_time;
         } else {
             return true;
         }
     }
 
     bool contain_start_end_time(long start_time, long end_time) {
-        if (type_ == TIME_FILTER) {
-            return value_ < start_time;
+        if (this->type_ == TIME_FILTER) {
+            return this->value_ < start_time;
         } else {
             return true;
         }
diff --git a/cpp/src/reader/filter/gt_eq.h b/cpp/src/reader/filter/gt_eq.h
index 4f08a28c..067c5608 100644
--- a/cpp/src/reader/filter/gt_eq.h
+++ b/cpp/src/reader/filter/gt_eq.h
@@ -19,20 +19,21 @@
 #ifndef READER_FILTER_OPERATOR_GT_EQ_H
 #define READER_FILTER_OPERATOR_GT_EQ_H
 
-#include "filter/unary_filter.h"
+#include "reader/filter/object.h"
+#include "reader/filter/unary_filter.h"
 
 namespace storage {
 template <typename T>
 class GtEq : public UnaryFilter<T> {
    public:
-    GtEq() : UnaryFilter() {}
-    GtEq(T value, FilterType type) { UnaryFilter(value, type); }
+    GtEq() : UnaryFilter<T>(){};
+    GtEq(T value, FilterType type) : UnaryFilter<T>(value, type) {}
 
     virtual ~GtEq() {}
 
     bool satisfy(Statistic *statistic) {
-        if (type_ == TIME_FILTER) {
-            return value_ <= statistic->end_time_;
+        if (this->type_ == TIME_FILTER) {
+            return this->value_ <= statistic->end_time_;
         } else {
             if (statistic->get_type() == common::TEXT ||
                 statistic->get_type() == common::BOOLEAN) {
@@ -45,21 +46,21 @@ class GtEq : public UnaryFilter<T> {
     }
 
     bool satisfy(long time, Object value) {
-        Object v = (type_ == TIME_FILTER ? time : value);
-        return value_ <= v;
+        Object v = (this->type_ == TIME_FILTER ? time : value);
+        return this->value_ <= v;
     }
 
     bool satisfy_start_end_time(long start_time, long end_time) {
-        if (type_ == TIME_FILTER) {
-            return value_ <= end_time;
+        if (this->type_ == TIME_FILTER) {
+            return this->value_ <= end_time;
         } else {
             return true;
         }
     }
 
     bool contain_start_end_time(long start_time, long end_time) {
-        if (type_ == TIME_FILTER) {
-            return value_ <= start_time;
+        if (this->type_ == TIME_FILTER) {
+            return this->value_ <= start_time;
         } else {
             return true;
         }
diff --git a/cpp/src/reader/filter/in.h b/cpp/src/reader/filter/in.h
index 4e4494d0..0c95de95 100644
--- a/cpp/src/reader/filter/in.h
+++ b/cpp/src/reader/filter/in.h
@@ -19,9 +19,11 @@
 #ifndef READER_FILTER_OPERATOR_IN_H
 #define READER_FILTER_OPERATOR_IN_H
 
+#include <algorithm>
 #include <vector>
 
-#include "filter/binary_filter.h"
+#include "reader/filter/binary_filter.h"
+#include "reader/filter/object.h"
 
 namespace storage {
 template <typename T>
@@ -35,8 +37,8 @@ class In : public Filter {
     bool satisfy(Statistic *statistic) { return true; }
 
     bool satisfy(long time, Object value) {
-        Object v = (filterType == TIME_FILTER ? time : value);
-        std::vector<T>::iterator it = find(values_.begin(), values_.end(), v);
+        Object v = (type_ == TIME_FILTER ? time : value);
+        auto it = std::find(values_.begin(), values_.end(), v);
         bool result = (it != values_.end() ? true : false);
         return result != not_;
     }
diff --git a/cpp/src/reader/filter/lt.h b/cpp/src/reader/filter/lt.h
index 85c3e9c7..062fb3af 100644
--- a/cpp/src/reader/filter/lt.h
+++ b/cpp/src/reader/filter/lt.h
@@ -19,22 +19,22 @@
 #ifndef READER_FILTER_OPERATOR_LT_H
 #define READER_FILTER_OPERATOR_LT_H
 
-#include "storage/read/filter/unary_filter.h"
-
+#include "reader/filter/object.h"
+#include "reader/filter/unary_filter.h"
 namespace storage {
 template <typename T>
 class Lt : public UnaryFilter<T> {
    public:
-    Lt() {}
-    Lt(T value, FilterType type) { UnaryFilter(value, type); }
+    Lt() : UnaryFilter<T>() {}
+    Lt(T value, FilterType type) : UnaryFilter<T>(value, type) {}
     virtual ~Lt() {}
 
     bool satisfy(Statistic *statistic) {
-        if (type_ == TIME_FILTER) {
-            return value_ > statistic->start_time_;
+        if (this->type_ == TIME_FILTER) {
+            return this->value_ > statistic->start_time_;
         } else {
-            if (statistic.get_type() == common::TEXT ||
-                statistic.get_type() == common::BOOLEAN) {
+            if (statistic->get_type() == common::TEXT ||
+                statistic->get_type() == common::BOOLEAN) {
                 return true;
             } else {
                 // todo value filter
@@ -44,21 +44,21 @@ class Lt : public UnaryFilter<T> {
     }
 
     bool satisfy(long time, Object value) {
-        Object v = (type_ == TIME_FILTER ? time : value);
-        return value_ > v;
+        Object v = (this->type_ == TIME_FILTER ? time : value);
+        return this->value_ > v;
     }
 
     bool satisfy_start_end_time(long start_time, long end_time) {
-        if (type_ == TIME_FILTER) {
-            return value_ > start_time;
+        if (this->type_ == TIME_FILTER) {
+            return this->value_ > start_time;
         } else {
             return true;
         }
     }
 
     bool contain_start_end_time(long start_time, long end_time) {
-        if (type_ == TIME_FILTER) {
-            return value_ > end_time;
+        if (this->type_ == TIME_FILTER) {
+            return this->value_ > end_time;
         } else {
             return true;
         }
diff --git a/cpp/src/reader/filter/lt_eq.h b/cpp/src/reader/filter/lt_eq.h
index 78f5633b..aa866057 100644
--- a/cpp/src/reader/filter/lt_eq.h
+++ b/cpp/src/reader/filter/lt_eq.h
@@ -19,19 +19,22 @@
 #ifndef READER_FILTER_OPERATOR_LT_EQ_H
 #define READER_FILTER_OPERATOR_LT_EQ_H
 
-#include "storage/read/filter/unary_filter.h"
+#include <gtest/gtest.h>
+
+#include "common/statistic.h"
+#include "reader/filter/gt.h"
 
 namespace storage {
 template <typename T>
 class LtEq : public UnaryFilter<T> {
    public:
-    LtEq() {}
-    LtEq(T value, FilterType type) { UnaryFilter(value, type); }
+    LtEq() : UnaryFilter<T>() {}
+    LtEq(T value, FilterType type) : UnaryFilter<T>(value, type) {}
     virtual ~LtEq() {}
 
     bool satisfy(Statistic *statistic) {
-        if (type_ == TIME_FILTER) {
-            return value_ >= statistic->start_time_;
+        if (this->type_ == TIME_FILTER) {
+            return this->value_ >= statistic->start_time_;
         } else {
             if (statistic->get_type() == common::TEXT ||
                 statistic->get_type() == common::BOOLEAN) {
@@ -44,21 +47,21 @@ class LtEq : public UnaryFilter<T> {
     }
 
     bool satisfy(long time, Object value) {
-        Object v = (type_ == TIME_FILTER ? time : value);
-        return value_ >= v;
+        Object v = (this->type_ == TIME_FILTER ? time : value);
+        return this->value_ >= v;
     }
 
     bool satisfy_start_end_time(long start_time, long end_time) {
-        if (type_ == TIME_FILTER) {
-            return value_ >= start_time;
+        if (this->type_ == TIME_FILTER) {
+            return this->value_ >= start_time;
         } else {
             return true;
         }
     }
 
     bool contain_start_end_time(long start_time, long end_time) {
-        if (type_ == TIME_FILTER) {
-            return value_ >= end_time;
+        if (this->type_ == TIME_FILTER) {
+            return this->value_ >= end_time;
         } else {
             return true;
         }
diff --git a/cpp/src/reader/filter/not_eq.h b/cpp/src/reader/filter/not_eq.h
index 0f4ac3af..d9d54359 100644
--- a/cpp/src/reader/filter/not_eq.h
+++ b/cpp/src/reader/filter/not_eq.h
@@ -19,23 +19,24 @@
 #ifndef READER_FILTER_OPERATOR_NOT_EQ_H
 #define READER_FILTER_OPERATOR_NOT_EQ_H
 
-#include "filter/unary_filter.h"
+#include "reader/filter/object.h"
+#include "reader/filter/unary_filter.h"
 
 namespace storage {
 template <typename T>
 class NotEq : public UnaryFilter<T> {
    public:
     NotEq() {}
-    NotEq(T value, FilterType type) { UnaryFilter(value, type); }
+    NotEq(T value, FilterType type) : UnaryFilter<T>(value, type) {}
     virtual ~NotEq() {}
 
     bool satisfy(Statistic *statistic) {
-        if (type_ == TIME_FILTER) {
-            return !(value_ == statistic->start_time_ &&
-                     value_ == statistic->end_time_);
+        if (this->type_ == TIME_FILTER) {
+            return !(this->value_ == statistic->start_time_ &&
+                     this->value_ == statistic->end_time_);
         } else {
-            if (statistic.get_type() == common::TEXT ||
-                statistic.get_type() == common::BOOLEAN) {
+            if (statistic->get_type() == common::TEXT ||
+                statistic->get_type() == common::BOOLEAN) {
                 return true;
             } else {
                 // todo value filter
@@ -45,21 +46,21 @@ class NotEq : public UnaryFilter<T> {
     }
 
     bool satisfy(long time, Object value) {
-        Object v = (type_ == TIME_FILTER ? time : value);
-        return !value_.equals(v);
+        Object v = (this->type_ == TIME_FILTER ? time : value);
+        return !this->value_.equals(v);
     }
 
     bool satisfy_start_end_time(long start_time, long end_time) {
-        if (filterType == TIME_FILTER) {
-            return value_ != end_time && value_ != start_time;
+        if (this->type_ == TIME_FILTER) {
+            return this->value_ != end_time && this->value_ != start_time;
         } else {
             return true;
         }
     }
 
     bool contain_start_end_time(long start_time, long end_time) {
-        if (filterType == TIME_FILTER) {
-            return value_ < start_time || value_ > end_time;
+        if (this->type_ == TIME_FILTER) {
+            return this->value_ < start_time || this->value_ > end_time;
         } else {
             return true;
         }
diff --git a/cpp/src/reader/filter/object.h b/cpp/src/reader/filter/object.h
index 5709fadd..2fe2fd59 100644
--- a/cpp/src/reader/filter/object.h
+++ b/cpp/src/reader/filter/object.h
@@ -20,6 +20,7 @@
 #define READER_FILTER_BASIC_OBJECT_H
 
 #include "common/db_common.h"
+#include "string.h"
 
 namespace storage {
 
@@ -72,6 +73,186 @@ class Object {
         values_.sval_ = const_cast<char *>(val.c_str());
     }
 
+    bool equals(const Object &object) {
+        if (object.get_type() != type_) {
+            return false;
+        }
+        switch (object.get_type()) {
+            case common::BOOLEAN:
+                return values_.bval_ == object.values_.bval_;
+            case common::INT32:
+                return values_.ival_ == object.values_.ival_;
+            case common::INT64:
+                return values_.lval_ == object.values_.lval_;
+            case common::FLOAT:
+                return values_.fval_ == object.values_.fval_;
+            case common::DOUBLE:
+                return values_.dval_ == object.values_.dval_;
+            case common::TEXT:
+                return !strcmp(values_.sval_, object.values_.sval_);
+            default:
+                return false;
+        }
+    }
+
+    bool operator==(const Object &object) const {
+        if (object.get_type() != type_) {
+            return false;
+        }
+        switch (object.get_type()) {
+            case common::BOOLEAN:
+                return values_.bval_ == object.values_.bval_;
+            case common::INT32:
+                return values_.ival_ == object.values_.ival_;
+            case common::INT64:
+                return values_.lval_ == object.values_.lval_;
+            case common::FLOAT:
+                return values_.fval_ == object.values_.fval_;
+            case common::DOUBLE:
+                return values_.dval_ == object.values_.dval_;
+            case common::TEXT:
+                return !strcmp(values_.sval_, object.values_.sval_);
+            default:
+                return false;
+        }
+    }
+
+    bool operator!=(const Object &object) const {
+        if (object.get_type() != type_) {
+            return false;
+        }
+        switch (object.get_type()) {
+            case common::BOOLEAN:
+                return values_.bval_ != object.values_.bval_;
+            case common::INT32:
+                return values_.ival_ != object.values_.ival_;
+            case common::INT64:
+                return values_.lval_ != object.values_.lval_;
+            case common::FLOAT:
+                return values_.fval_ != object.values_.fval_;
+            case common::DOUBLE:
+                return values_.dval_ != object.values_.dval_;
+            case common::TEXT:
+                return strcmp(values_.sval_, object.values_.sval_);
+            default:
+                return false;
+        }
+    }
+
+    bool operator<(const Object &object) const {
+        if (object.get_type() != type_) {
+            return false;
+        }
+        switch (object.get_type()) {
+            case common::BOOLEAN:
+                return values_.bval_ < object.values_.bval_;
+            case common::INT32:
+                return values_.ival_ < object.values_.ival_;
+            case common::INT64:
+                return values_.lval_ < object.values_.lval_;
+            case common::FLOAT:
+                return values_.fval_ < object.values_.fval_;
+            case common::DOUBLE:
+                return values_.dval_ < object.values_.dval_;
+            case common::TEXT:
+                return strcmp(values_.sval_, object.values_.sval_) < 0;
+            default:
+                return false;
+        }
+    }
+
+    bool operator<=(const Object &object) const {
+        if (object.get_type() != type_) {
+            return false;
+        }
+        switch (object.get_type()) {
+            case common::BOOLEAN:
+                return values_.bval_ <= object.values_.bval_;
+            case common::INT32:
+                return values_.ival_ <= object.values_.ival_;
+            case common::INT64:
+                return values_.lval_ <= object.values_.lval_;
+            case common::FLOAT:
+                return values_.fval_ <= object.values_.fval_;
+            case common::DOUBLE:
+                return values_.dval_ <= object.values_.dval_;
+            case common::TEXT:
+                return strcmp(values_.sval_, object.values_.sval_) <= 0;
+            default:
+                return false;
+        }
+    }
+
+    bool operator>(const Object &object) const {
+        if (object.get_type() != type_) {
+            return false;
+        }
+        switch (object.get_type()) {
+            case common::BOOLEAN:
+                return values_.bval_ > object.values_.bval_;
+            case common::INT32:
+                return values_.ival_ > object.values_.ival_;
+            case common::INT64:
+                return values_.lval_ > object.values_.lval_;
+            case common::FLOAT:
+                return values_.fval_ > object.values_.fval_;
+            case common::DOUBLE:
+                return values_.dval_ > object.values_.dval_;
+            case common::TEXT:
+                return strcmp(values_.sval_, object.values_.sval_) > 0;
+            default:
+                return false;
+        }
+    }
+
+    bool operator>=(const Object &object) const {
+        if (object.get_type() != type_) {
+            return false;
+        }
+        switch (object.get_type()) {
+            case common::BOOLEAN:
+                return values_.bval_ >= object.values_.bval_;
+            case common::INT32:
+                return values_.ival_ >= object.values_.ival_;
+            case common::INT64:
+                return values_.lval_ >= object.values_.lval_;
+            case common::FLOAT:
+                return values_.fval_ >= object.values_.fval_;
+            case common::DOUBLE:
+                return values_.dval_ >= object.values_.dval_;
+            case common::TEXT:
+                return strcmp(values_.sval_, object.values_.sval_) >= 0;
+            default:
+                return false;
+        }
+    }
+
+    friend bool operator>=(const Object &object1, const Object &objec2) {
+        return object1.operator>=(objec2);
+    }
+    friend bool operator>(const Object &object1, const Object &objec2) {
+        return object1.operator>(objec2);
+    }
+    friend bool operator==(const Object &object1, const Object &objec2) {
+        return object1.operator==(objec2);
+    }
+    friend bool operator<=(const Object &object1, const Object &objec2) {
+        return object1.operator<=(objec2);
+    }
+    friend bool operator<(const Object &object1, const Object &objec2) {
+        return object1.operator<(objec2);
+    }
+    friend bool operator!=(const Object &object1, const Object &objec2) {
+        return object1.operator!=(objec2);
+    }
+
+    bool operator>=(const int64_t &time) const {}
+    bool operator<=(const int64_t &time) const {}
+    bool operator==(const int64_t &time) const {}
+    bool operator>(const int64_t &time) const {}
+    bool operator<(const int64_t &time) const {}
+    bool operator!=(const int64_t &time) const {}
+
     FORCE_INLINE const common::TSDataType get_type() const { return type_; }
 
    private:
@@ -79,6 +260,5 @@ class Object {
 };
 
 }  // namespace storage
-}  // namespace timecho
 
 #endif  // READER_FILTER_BASIC_OBJECT_H
diff --git a/cpp/src/reader/filter/or_filter.h 
b/cpp/src/reader/filter/or_filter.h
index 49a6d50b..f0ca1aed 100644
--- a/cpp/src/reader/filter/or_filter.h
+++ b/cpp/src/reader/filter/or_filter.h
@@ -27,7 +27,7 @@ namespace storage {
 class OrFilter : public BinaryFilter {
    public:
     OrFilter() {}
-    OrFilter(Filter *left, Filter *right) { BinaryFilter(left, right); }
+    OrFilter(Filter *left, Filter *right) : BinaryFilter(left, right) {}
     ~OrFilter() {}
 
     FORCE_INLINE bool satisfy(Statistic *statistic) {
@@ -56,32 +56,47 @@ class OrFilter : public BinaryFilter {
         int left_index = 0, right_index = 0;
         int left_size = left_time_ranges->size();
         int right_size = right_time_ranges->size();
-
+        TimeRange *range = choose_next_range(
+            left_time_ranges, right_time_ranges, left_index, right_index);
         while (left_index < left_size || right_index < right_size) {
-            TimeRange *left_range = left_time_ranges->at(left_index);
-            TimeRange *right_range = right_time_ranges->at(right_index);
-
-            if (left_range->end_time_ < right_range->start_time_) {
-                left_index++;
-            } else if (right_range->end_time_ < left_range->start_time_) {
-                right_index++;
+            TimeRange *choosen_range = choose_next_range(
+                left_time_ranges, right_time_ranges, left_index, right_index);
+            if (choosen_range->start_time_ > range->end_time_) {
+                result->push_back(
+                    new TimeRange(range->start_time_, range->end_time_));
+                range = choosen_range;
             } else {
-                TimeRange *intersection = new TimeRange(
-                    std::max(left_range->start_time_, 
right_range->start_time_),
-                    std::min(left_range->end_time_, right_range->end_time_));
-                result->push_back(intersection);
-                if (left_range->end_time_ <= intersection->end_time_) {
-                    left_index++;
-                }
-                if (right_range->end_time_ <= intersection->end_time_) {
-                    right_index++;
-                }
+                range->end_time_ =
+                    std::max(range->end_time_, choosen_range->end_time_);
             }
         }
+        result->push_back(new TimeRange(range->start_time_, range->end_time_));
         return result;
     }
 
    private:
+    TimeRange *choose_next_range(std::vector<TimeRange *> *left_time_ranges,
+                                 std::vector<TimeRange *> *right_time_ranges,
+                                 int &left_index, int &right_index) {
+        int left_size = left_time_ranges->size();
+        int right_size = right_time_ranges->size();
+        if (left_index < left_size && right_index < right_size) {
+            TimeRange *left_range = left_time_ranges->at(left_index);
+            TimeRange *right_range = right_time_ranges->at(right_index);
+            // Choose the range with the smaller minimum start time
+            if (left_range->start_time_ <= right_range->start_time_) {
+                left_index++;
+                return left_range;
+            } else {
+                right_index++;
+                return right_range;
+            }
+        } else if (left_index < left_size) {
+            return left_time_ranges->at(left_index++);
+        } else {
+            return right_time_ranges->at(right_index++);
+        }
+    }
 };
 
 }  // namespace storage

Reply via email to