From 5e841f7a5786700a97e0e4fedb62538d7b1b7cc7 Mon Sep 17 00:00:00 2001 From: Wes McKinney Date: Tue, 22 Mar 2016 20:09:54 -0500 Subject: [PATCH] Create explicit PrimitiveArray subclasses to avoid unwanted template instantiation --- cpp/src/arrow/array-test.cc | 4 +- cpp/src/arrow/array.cc | 8 +-- cpp/src/arrow/array.h | 4 +- cpp/src/arrow/types/boolean.h | 2 +- cpp/src/arrow/types/list.cc | 4 +- cpp/src/arrow/types/list.h | 2 +- cpp/src/arrow/types/primitive-test.cc | 4 +- cpp/src/arrow/types/primitive.cc | 4 +- cpp/src/arrow/types/primitive.h | 87 +++++++++++---------------- 9 files changed, 46 insertions(+), 73 deletions(-) diff --git a/cpp/src/arrow/array-test.cc b/cpp/src/arrow/array-test.cc index f7bdd0ce09b40..eded5941e892e 100644 --- a/cpp/src/arrow/array-test.cc +++ b/cpp/src/arrow/array-test.cc @@ -31,8 +31,6 @@ namespace arrow { -static TypePtr int32 = TypePtr(new Int32Type()); - class TestArray : public ::testing::Test { public: void SetUp() { @@ -76,7 +74,7 @@ TEST_F(TestArray, TestIsNull) { std::shared_ptr null_buf = test::bytes_to_null_buffer(nulls.data(), nulls.size()); std::unique_ptr arr; - arr.reset(new Array(int32, nulls.size(), null_count, null_buf)); + arr.reset(new Int32Array(nulls.size(), nullptr, null_count, null_buf)); ASSERT_EQ(null_count, arr->null_count()); ASSERT_EQ(5, null_buf->size()); diff --git a/cpp/src/arrow/array.cc b/cpp/src/arrow/array.cc index d6a2381a379b6..b3d4d21178827 100644 --- a/cpp/src/arrow/array.cc +++ b/cpp/src/arrow/array.cc @@ -37,7 +37,7 @@ Array::Array(const TypePtr& type, int32_t length, int32_t null_count, } } -bool Array::Equals(const Array& other) const { +bool Array::EqualsExact(const Array& other) const { if (this == &other) return true; if (length_ != other.length_ || null_count_ != other.null_count_ || type_enum() != other.type_enum()) { @@ -50,10 +50,4 @@ bool Array::Equals(const Array& other) const { } } -bool Array::Equals(const std::shared_ptr& arr) const { - if (arr.get() == nullptr) return false; - if (this == arr.get()) return true; - return Equals(*static_cast(arr.get())); -} - } // namespace arrow diff --git a/cpp/src/arrow/array.h b/cpp/src/arrow/array.h index 6304e9fe0a425..70e0f48bf4552 100644 --- a/cpp/src/arrow/array.h +++ b/cpp/src/arrow/array.h @@ -60,8 +60,8 @@ class Array { return nulls_; } - bool Equals(const Array& arr) const; - virtual bool Equals(const std::shared_ptr& arr) const; + bool EqualsExact(const Array& arr) const; + virtual bool Equals(const std::shared_ptr& arr) const = 0; protected: TypePtr type_; diff --git a/cpp/src/arrow/types/boolean.h b/cpp/src/arrow/types/boolean.h index a5023d7b368d2..1cb91f9ba4966 100644 --- a/cpp/src/arrow/types/boolean.h +++ b/cpp/src/arrow/types/boolean.h @@ -22,7 +22,7 @@ namespace arrow { -typedef PrimitiveArrayImpl BooleanArray; +// typedef PrimitiveArrayImpl BooleanArray; class BooleanBuilder : public ArrayBuilder { }; diff --git a/cpp/src/arrow/types/list.cc b/cpp/src/arrow/types/list.cc index c84c6f9cac5c2..670ee4da11675 100644 --- a/cpp/src/arrow/types/list.cc +++ b/cpp/src/arrow/types/list.cc @@ -19,7 +19,7 @@ namespace arrow { -bool ListArray::Equals(const ListArray& other) const { +bool ListArray::EqualsExact(const ListArray& other) const { if (this == &other) return true; if (null_count_ != other.null_count_) { return false; @@ -45,7 +45,7 @@ bool ListArray::Equals(const std::shared_ptr& arr) const { if (this->type_enum() != arr->type_enum()) { return false; } - return Equals(*static_cast(arr.get())); + return EqualsExact(*static_cast(arr.get())); } } // namespace arrow diff --git a/cpp/src/arrow/types/list.h b/cpp/src/arrow/types/list.h index 0448c8d4068e3..141f762458b3b 100644 --- a/cpp/src/arrow/types/list.h +++ b/cpp/src/arrow/types/list.h @@ -65,7 +65,7 @@ class ListArray : public Array { int32_t value_offset(int i) { return offsets_[i];} int32_t value_length(int i) { return offsets_[i + 1] - offsets_[i];} - bool Equals(const ListArray& other) const; + bool EqualsExact(const ListArray& other) const; bool Equals(const std::shared_ptr& arr) const override; protected: diff --git a/cpp/src/arrow/types/primitive-test.cc b/cpp/src/arrow/types/primitive-test.cc index 2fd3cc35bb372..7eae8cda8c488 100644 --- a/cpp/src/arrow/types/primitive-test.cc +++ b/cpp/src/arrow/types/primitive-test.cc @@ -123,7 +123,7 @@ class TestPrimitiveBuilder : public TestBuilder { ASSERT_EQ(0, builder_->null_count()); ASSERT_EQ(nullptr, builder_->buffer()); - ASSERT_TRUE(result->Equals(*expected.get())); + ASSERT_TRUE(result->EqualsExact(*expected.get())); ASSERT_EQ(ex_null_count, result->null_count()); } @@ -143,7 +143,7 @@ class TestPrimitiveBuilder : public TestBuilder { ASSERT_EQ(0, builder_nn_->capacity()); ASSERT_EQ(nullptr, builder_nn_->buffer()); - ASSERT_TRUE(result->Equals(*expected.get())); + ASSERT_TRUE(result->EqualsExact(*expected.get())); ASSERT_EQ(0, result->null_count()); } diff --git a/cpp/src/arrow/types/primitive.cc b/cpp/src/arrow/types/primitive.cc index cda50d0edae28..32b8bfa7f1bd4 100644 --- a/cpp/src/arrow/types/primitive.cc +++ b/cpp/src/arrow/types/primitive.cc @@ -35,7 +35,7 @@ PrimitiveArray::PrimitiveArray(const TypePtr& type, int32_t length, raw_data_ = data == nullptr? nullptr : data_->data(); } -bool PrimitiveArray::Equals(const PrimitiveArray& other) const { +bool PrimitiveArray::EqualsExact(const PrimitiveArray& other) const { if (this == &other) return true; if (null_count_ != other.null_count_) { return false; @@ -55,7 +55,7 @@ bool PrimitiveArray::Equals(const std::shared_ptr& arr) const { if (this->type_enum() != arr->type_enum()) { return false; } - return Equals(*static_cast(arr.get())); + return EqualsExact(*static_cast(arr.get())); } } // namespace arrow diff --git a/cpp/src/arrow/types/primitive.h b/cpp/src/arrow/types/primitive.h index 43ff88980af66..e01027cf55c39 100644 --- a/cpp/src/arrow/types/primitive.h +++ b/cpp/src/arrow/types/primitive.h @@ -45,7 +45,7 @@ class PrimitiveArray : public Array { const std::shared_ptr& data() const { return data_;} - bool Equals(const PrimitiveArray& other) const; + bool EqualsExact(const PrimitiveArray& other) const; bool Equals(const std::shared_ptr& arr) const override; protected: @@ -53,43 +53,41 @@ class PrimitiveArray : public Array { const uint8_t* raw_data_; }; - -template -class PrimitiveArrayImpl : public PrimitiveArray { - public: - typedef typename TypeClass::c_type value_type; - - PrimitiveArrayImpl(const TypePtr& type, int32_t length, - const std::shared_ptr& data, - int32_t null_count = 0, - const std::shared_ptr& nulls = nullptr) : - PrimitiveArray(type, length, data, null_count, nulls) {} - - PrimitiveArrayImpl(int32_t length, const std::shared_ptr& data, - int32_t null_count = 0, - const std::shared_ptr& nulls = nullptr) : - PrimitiveArray(std::make_shared(), length, data, - null_count, nulls) {} - - virtual ~PrimitiveArrayImpl() {} - - bool Equals(const PrimitiveArrayImpl& other) const { - return PrimitiveArray::Equals(*static_cast(&other)); - } - - const value_type* raw_data() const { - return reinterpret_cast(raw_data_); - } - - value_type Value(int i) const { - return raw_data()[i]; - } - - TypeClass* exact_type() const { - return static_cast(type_); - } +#define NUMERIC_ARRAY_DECL(NAME, TypeClass, T) \ +class NAME : public PrimitiveArray { \ + public: \ + using value_type = T; \ + using PrimitiveArray::PrimitiveArray; \ + NAME(int32_t length, const std::shared_ptr& data, \ + int32_t null_count = 0, \ + const std::shared_ptr& nulls = nullptr) : \ + PrimitiveArray(std::make_shared(), length, data, \ + null_count, nulls) {} \ + \ + bool EqualsExact(const NAME& other) const { \ + return PrimitiveArray::EqualsExact( \ + *static_cast(&other)); \ + } \ + \ + const T* raw_data() const { \ + return reinterpret_cast(raw_data_); \ + } \ + \ + T Value(int i) const { \ + return raw_data()[i]; \ + } \ }; +NUMERIC_ARRAY_DECL(UInt8Array, UInt8Type, uint8_t); +NUMERIC_ARRAY_DECL(Int8Array, Int8Type, int8_t); +NUMERIC_ARRAY_DECL(UInt16Array, UInt16Type, uint16_t); +NUMERIC_ARRAY_DECL(Int16Array, Int16Type, int16_t); +NUMERIC_ARRAY_DECL(UInt32Array, UInt32Type, uint32_t); +NUMERIC_ARRAY_DECL(Int32Array, Int32Type, int32_t); +NUMERIC_ARRAY_DECL(UInt64Array, UInt64Type, uint64_t); +NUMERIC_ARRAY_DECL(Int64Array, Int64Type, int64_t); +NUMERIC_ARRAY_DECL(FloatArray, FloatType, float); +NUMERIC_ARRAY_DECL(DoubleArray, DoubleType, double); template class PrimitiveBuilder : public ArrayBuilder { @@ -217,23 +215,6 @@ class PrimitiveBuilder : public ArrayBuilder { int elsize_; }; -// Array containers - -typedef PrimitiveArrayImpl UInt8Array; -typedef PrimitiveArrayImpl Int8Array; - -typedef PrimitiveArrayImpl UInt16Array; -typedef PrimitiveArrayImpl Int16Array; - -typedef PrimitiveArrayImpl UInt32Array; -typedef PrimitiveArrayImpl Int32Array; - -typedef PrimitiveArrayImpl UInt64Array; -typedef PrimitiveArrayImpl Int64Array; - -typedef PrimitiveArrayImpl FloatArray; -typedef PrimitiveArrayImpl DoubleArray; - // Builders typedef PrimitiveBuilder UInt8Builder;