shangxinli commented on code in PR #354:
URL: https://github.com/apache/iceberg-cpp/pull/354#discussion_r2570258948


##########
src/iceberg/util/partition_value_util.h:
##########
@@ -0,0 +1,218 @@
+/*
+ * 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
+
+/// \file iceberg/row/partition_values.h
+/// Wrapper classes for partition value related data structures.
+
+#include <functional>
+#include <unordered_map>
+#include <unordered_set>
+
+#include "iceberg/iceberg_export.h"
+#include "iceberg/row/partition_values.h"
+
+namespace iceberg {
+
+constexpr size_t kHashPrime = 0x9e3779b9;
+
+using PartitionKey = std::pair<int32_t, PartitionValues>;
+
+/// \brief Hash functor for PartitionValues.
+struct PartitionValuesHash {
+  std::size_t operator()(const PartitionValues& partition) const noexcept {
+    std::size_t hash = 0;
+    LiteralHash literal_hash;
+    for (const auto& literal : partition.values()) {
+      hash ^= literal_hash(literal) + kHashPrime + (hash << 6) + (hash >> 2);
+    }
+    return hash;
+  }
+};
+
+/// \brief Equality functor for PartitionValues.
+struct PartitionValuesEqual {
+  bool operator()(const PartitionValues& lhs, const PartitionValues& rhs) 
const {
+    return lhs == rhs;
+  }
+};
+
+/// \brief Hash functor for std::pair<int32_t, PartitionValues>.
+struct PartitionKeyHash {
+  std::size_t operator()(const PartitionKey& key) const noexcept {
+    std::size_t hash = std::hash<int32_t>{}(key.first);
+    hash ^= PartitionValuesHash{}(key.second) + kHashPrime + (hash << 6) + 
(hash >> 2);
+    return hash;
+  }
+};
+
+/// \brief Equality functor for std::pair<int32_t, PartitionValues>.
+struct PartitionKeyEqual {
+  bool operator()(const PartitionKey& lhs, const PartitionKey& rhs) const {
+    return lhs == rhs;
+  }
+};
+
+/// \brief A map that uses a pair of spec ID and partition tuple as keys.
+///
+/// \tparam V the type of values
+template <typename V>
+class PartitionMap {
+ public:
+  using map_type =
+      std::unordered_map<PartitionKey, V, PartitionKeyHash, PartitionKeyEqual>;
+  using iterator = typename map_type::iterator;
+  using const_iterator = typename map_type::const_iterator;
+
+  PartitionMap() = default;
+
+  /// \brief Get the number of entries in the map.
+  size_t size() const { return map_.size(); }
+
+  /// \brief Check if the map is empty.
+  bool empty() const { return map_.empty(); }
+
+  /// \brief Clear all entries from the map.
+  void clear() { map_.clear(); }
+
+  /// \brief Check if the map contains a key.
+  /// \param spec_id The partition spec ID.
+  /// \param values The partition values.
+  /// \return true if the key exists, false otherwise.
+  bool contains(int32_t spec_id, const PartitionValues& values) const {
+    return map_.contains(PartitionKey{spec_id, values});

Review Comment:
   In this case, every lookup will create a temporary PartitionKey{spec_id, 
values} which copies the entire PartitionValues vector? Consider heterogeneous 
lookup? 



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to