pitrou commented on a change in pull request #10915:
URL: https://github.com/apache/arrow/pull/10915#discussion_r689433858



##########
File path: cpp/src/arrow/util/small_vector.h
##########
@@ -0,0 +1,509 @@
+// 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 <algorithm>
+#include <cassert>
+#include <cstddef>
+#include <initializer_list>
+#include <limits>
+#include <new>
+#include <type_traits>
+#include <utility>
+
+#include "arrow/util/macros.h"
+
+namespace arrow {
+namespace internal {
+
+#if __cplusplus >= 201703L
+using std::launder;
+#else
+// TODO factor out from result.h
+
+// template <class T>
+// constexpr T* launder(T* p) noexcept {
+//   return p;
+// }
+#endif
+
+template <typename ValueType, typename PointerType, typename ReferenceType>
+class VectorIterator {
+ public:
+  using value_type = ValueType;
+  using pointer = PointerType;
+  using reference = ReferenceType;
+  using difference_type = ptrdiff_t;
+  using iterator_category = std::random_access_iterator_tag;
+
+  // Some algorithms need to default-construct an iterator
+  constexpr VectorIterator() noexcept = default;
+
+  constexpr explicit VectorIterator(pointer ptr) noexcept : ptr_(ptr) {}
+
+  // Value access
+  constexpr reference operator*() const { return *ptr_; }
+
+  constexpr reference operator[](difference_type n) const { return ptr_[n]; }
+
+  // Forward / backward
+  VectorIterator& operator++() {
+    ++ptr_;
+    return *this;
+  }
+  VectorIterator& operator--() {
+    --ptr_;
+    return *this;
+  }
+  VectorIterator operator++(int) {
+    VectorIterator tmp(*this);
+    ++ptr_;
+    return tmp;
+  }
+  VectorIterator operator--(int) {
+    VectorIterator tmp(*this);
+    --ptr_;
+    return tmp;
+  }
+
+  // Arithmetic
+  constexpr difference_type operator-(const VectorIterator& other) const {
+    return ptr_ - other.ptr_;
+  }
+  constexpr VectorIterator operator+(difference_type n) const {
+    return VectorIterator(ptr_ + n);
+  }
+  constexpr VectorIterator operator-(difference_type n) const {
+    return VectorIterator(ptr_ - n);
+  }
+  VectorIterator& operator+=(difference_type n) {
+    ptr_ += n;
+    return *this;
+  }
+  VectorIterator& operator-=(difference_type n) {
+    ptr_ -= n;
+    return *this;
+  }
+
+  // Comparisons
+  constexpr bool operator==(const VectorIterator& other) const {
+    return ptr_ == other.ptr_;
+  }
+  constexpr bool operator!=(const VectorIterator& other) const {
+    return ptr_ != other.ptr_;
+  }
+  constexpr bool operator<(const VectorIterator& other) const {
+    return ptr_ < other.ptr_;
+  }
+  constexpr bool operator>(const VectorIterator& other) const {
+    return ptr_ > other.ptr_;
+  }
+  constexpr bool operator<=(const VectorIterator& other) const {
+    return ptr_ <= other.ptr_;
+  }
+  constexpr bool operator>=(const VectorIterator& other) const {
+    return ptr_ >= other.ptr_;
+  }
+
+ private:
+  pointer ptr_ = NULLPTR;
+};
+
+template <typename T>
+class StaticVectorMixin {
+ protected:
+  // properly aligned uninitialized storage for N T's
+  using storage_type = typename std::aligned_storage<sizeof(T), 
alignof(T)>::type;
+
+  static T* ptr_at(storage_type* p, size_t i) {
+    return launder(reinterpret_cast<T*>(&p[i]));
+  }
+
+  static const T* ptr_at(const storage_type* p, size_t i) {
+    return launder(reinterpret_cast<const T*>(&p[i]));
+  }
+
+  static void move_storage(storage_type* src, storage_type* dest, size_t n) {
+    for (size_t i = 0; i < n; ++i) {
+      T* src_item = ptr_at(src, i);
+      T* dest_item = ptr_at(dest, i);
+      new (dest_item) T(std::move(*src_item));
+      src_item->~T();
+    }
+  }
+
+  static void destroy_storage(storage_type* p, size_t n) {
+    for (size_t i = 0; i < n; ++i) {
+      ptr_at(p, i)->~T();
+    }
+  }
+};
+
+template <typename T, size_t N, bool NonTrivialDestructor>
+class StaticVectorBaseStorage : public StaticVectorMixin<T> {

Review comment:
       Note one question is how to share the definition of most vector methods 
between SmallVector and StaticVector. Your sketch doesn't show that.




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


Reply via email to