szaszm commented on code in PR #1703: URL: https://github.com/apache/nifi-minifi-cpp/pull/1703#discussion_r1414703188
########## extensions/standard-processors/RollingWindow.h: ########## @@ -0,0 +1,71 @@ +/** + * 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 <mutex> +#include <queue> +#include <vector> + +namespace org::apache::nifi::minifi::processors::standard::utils { + +namespace detail { +template<typename T, typename Container, typename Comparator> +struct priority_queue : std::priority_queue<T, Container, Comparator> { + using std::priority_queue<T, Container, Comparator>::priority_queue; + + // Expose the underlying container + const Container& get_container() const & { return this->c; } + Container get_container() && { return std::move(this->c); } +}; +} // namespace detail + +template<typename Timestamp, typename Value> +class RollingWindow { + public: + struct Entry { + Timestamp timestamp{}; + Value value{}; + }; + struct EntryComparator { + // greater-than, because std::priority_queue order is reversed. This way, top() is the oldest entry. + bool operator()(const Entry& lhs, const Entry& rhs) const { + return lhs.timestamp > rhs.timestamp; + } + }; + + void removeOlderThan(Timestamp timestamp) { + while (!state_.empty() && state_.top().timestamp < timestamp) { + state_.pop(); + } + } + + /** Remove the oldest entries until the size is <= size. */ + void shrinkToSize(size_t size) { + while (state_.size() > size && !state_.empty()) { Review Comment: While the AttributeRollingWindow processor never calls this with a size of 0, the interface contract of RollingWindow allows shrinking to 0. In the case of AttributeRollingWindow, the optimizer can probably prove that size is never zero, and get rid of the extra check after inlining. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
