You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datasketches.apache.org by jm...@apache.org on 2020/06/02 21:51:04 UTC

[incubator-datasketches-cpp] 03/09: conditional back inserter

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

jmalkin pushed a commit to branch patch_for_rc4
in repository https://gitbox.apache.org/repos/asf/incubator-datasketches-cpp.git

commit ef43c5ba50b9a4e901f06159946bb55535834e8e
Author: AlexanderSaydakov <Al...@users.noreply.github.com>
AuthorDate: Fri May 29 16:28:09 2020 -0700

    conditional back inserter
---
 theta/include/conditional_back_inserter.hpp | 53 +++++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)

diff --git a/theta/include/conditional_back_inserter.hpp b/theta/include/conditional_back_inserter.hpp
new file mode 100644
index 0000000..f128a3b
--- /dev/null
+++ b/theta/include/conditional_back_inserter.hpp
@@ -0,0 +1,53 @@
+/*
+ * 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.
+ */
+
+#ifndef CONDITIONAL_BACK_INSERTER_HPP_
+#define CONDITIONAL_BACK_INSERTER_HPP_
+
+#include <iterator>
+#include <functional>
+
+namespace datasketches {
+
+template <class Container, class Predicate>
+class conditional_back_insert_iterator: public std::back_insert_iterator<Container> {
+public:
+  conditional_back_insert_iterator(Container& c, Predicate& p): std::back_insert_iterator<Container>(c), p(p) {}
+
+  conditional_back_insert_iterator& operator=(typename Container::const_reference value) {
+    if (p(value)) std::back_insert_iterator<Container>::operator=(value);
+    return *this;
+  }
+
+  conditional_back_insert_iterator& operator*() { return *this; }
+  conditional_back_insert_iterator& operator++() { return *this; }
+  conditional_back_insert_iterator& operator++(int) { return *this; }
+
+private:
+  Predicate& p;
+};
+
+template< class Container, class Predicate>
+conditional_back_insert_iterator<Container, Predicate> conditional_back_inserter(Container& c, Predicate& p) {
+  return conditional_back_insert_iterator<Container, Predicate>(c, p);
+}
+
+} /* namespace datasketches */
+
+#endif


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org
For additional commands, e-mail: commits-help@datasketches.apache.org