You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by GitBox <gi...@apache.org> on 2021/12/11 11:02:00 UTC

[GitHub] [nifi-minifi-cpp] martinzink commented on a change in pull request #1038: MINIFICPP-1525 - Support flow file swapping in Connection

martinzink commented on a change in pull request #1038:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1038#discussion_r767141667



##########
File path: libminifi/include/utils/MinMaxHeap.h
##########
@@ -0,0 +1,320 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <functional>
+#include <vector>
+#include <cmath>
+#include <utility>
+
+struct MinMaxHeapTestAccessor;
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace utils {
+
+template<typename T, typename Comparator = std::less<T>>
+class MinMaxHeap {
+ public:
+  void clear() {
+    data_.clear();
+  }
+
+  const T& min() const {
+    return data_[0];
+  }
+
+  const T& max() const {
+    // the element at index 0, 1 or 2
+    if (data_.size() == 1) {
+      return data_[0];
+    }
+    if (data_.size() == 2) {
+      return data_[1];
+    }
+    if (less_(data_[2], data_[1])) {
+      return data_[1];
+    }
+    return data_[2];
+  }
+
+  size_t size() const {
+    return data_.size();
+  }
+
+  bool empty() const {
+    return data_.empty();
+  }
+
+  void push(T item) {
+    data_.push_back(std::move(item));
+    pushUp(data_.size() - 1);
+  }
+
+  T popMin() {
+    std::swap(data_[0], data_[data_.size() - 1]);
+    T item = std::move(data_.back());
+    data_.pop_back();
+    pushDown(0);
+    return item;
+  }
+
+  T popMax() {
+    if (data_.size() <= 2) {
+      T item = std::move(data_.back());
+      data_.pop_back();
+      return item;
+    }
+    if (less_(data_[2], data_[1])) {
+      std::swap(data_[1], data_[data_.size() - 1]);
+      T item = std::move(data_.back());
+      data_.pop_back();
+      pushDown(1);
+      return item;
+    } else {
+      std::swap(data_[2], data_[data_.size() - 1]);
+      T item = std::move(data_.back());
+      data_.pop_back();
+      pushDown(2);
+      return item;
+    }
+  }
+
+ private:
+  friend struct ::MinMaxHeapTestAccessor;
+
+  static size_t getLevel(size_t index) {
+    // more performant solutions are possible
+    // investigate if this turns out to be a bottleneck
+    size_t level = 0;
+    ++index;
+    while (index >>= 1) {
+      ++level;
+    }
+    return level;
+  }
+
+  static bool isOnMinLevel(size_t index) {
+    return getLevel(index) % 2 == 0;
+  }
+
+  static size_t getParent(size_t index) {
+    return (index - 1) / 2;
+  }
+
+  /**
+   * WARNING! must only be called when index is on a min-level.

Review comment:
       I think we should throw if this is violated.

##########
File path: libminifi/include/utils/MinMaxHeap.h
##########
@@ -0,0 +1,320 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <functional>
+#include <vector>
+#include <cmath>
+#include <utility>
+
+struct MinMaxHeapTestAccessor;
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace utils {
+
+template<typename T, typename Comparator = std::less<T>>
+class MinMaxHeap {
+ public:
+  void clear() {
+    data_.clear();
+  }
+
+  const T& min() const {
+    return data_[0];
+  }
+
+  const T& max() const {
+    // the element at index 0, 1 or 2
+    if (data_.size() == 1) {
+      return data_[0];
+    }
+    if (data_.size() == 2) {
+      return data_[1];
+    }
+    if (less_(data_[2], data_[1])) {
+      return data_[1];
+    }
+    return data_[2];
+  }
+
+  size_t size() const {
+    return data_.size();
+  }
+
+  bool empty() const {
+    return data_.empty();
+  }
+
+  void push(T item) {
+    data_.push_back(std::move(item));
+    pushUp(data_.size() - 1);
+  }
+
+  T popMin() {
+    std::swap(data_[0], data_[data_.size() - 1]);
+    T item = std::move(data_.back());
+    data_.pop_back();
+    pushDown(0);
+    return item;
+  }
+
+  T popMax() {
+    if (data_.size() <= 2) {
+      T item = std::move(data_.back());
+      data_.pop_back();
+      return item;
+    }
+    if (less_(data_[2], data_[1])) {
+      std::swap(data_[1], data_[data_.size() - 1]);

Review comment:
       I think if we were to extract the max() logic into a getMaxIndex() then this and max() could be simplified.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org