Skip to content

Instantly share code, notes, and snippets.

@wamsiv
Created October 18, 2019 00:27
Show Gist options
  • Save wamsiv/d5272565b36d8dfb02dc2e96e18c8d0c to your computer and use it in GitHub Desktop.
Save wamsiv/d5272565b36d8dfb02dc2e96e18c8d0c to your computer and use it in GitHub Desktop.
OptionalVectorAmalgamation
#include <optional>
#include <type_traits>
#include <vector>
template <typename T>
struct TypeMapping {
using PrimitiveType = std::conditional_t<std::is_fundamental_v<T>, T, void>;
};
// ArrowTypeWrappers
template <>
struct TypeMapping<ArrowTypeWrappers::DictEncoding8> {
using PrimitiveType = int8_t;
};
template <>
struct TypeMapping<ArrowTypeWrappers::DictEncoding16> {
using PrimitiveType = int16_t;
};
template <>
struct TypeMapping<ArrowTypeWrappers::DictEncoding32> {
using PrimitiveType = int32_t;
};
template <>
struct TypeMapping<ArrowTypeWrappers::Date32Wrapper> {
using PrimitiveType = int32_t;
};
template <>
struct TypeMapping<ArrowTypeWrappers::Date64Wrapper> {
using PrimitiveType = int64_t;
};
template <>
struct TypeMapping<ArrowTypeWrappers::TimeWrapper> {
using PrimitiveType = int32_t;
};
template <>
struct TypeMapping<ArrowTypeWrappers::TimestampWrapper> {
using PrimitiveType = int64_t;
};
template <typename T>
struct VectorDisambiguator : public std::vector<typename TypeMapping<T>::PrimitiveType> {
using MappedType = typename TypeMapping<T>::PrimitiveType;
};
template <typename T>
inline auto& disam_vector_base(VectorDisambiguator<T>& vd) {
using MappedType = typename VectorDisambiguator<T>::MappedType;
return static_cast<std::vector<MappedType>&>(vd);
}
template <typename... PRIMITIVE_TYPES>
class OptionalVecAmalgamation
: public std::optional<VectorDisambiguator<PRIMITIVE_TYPES>>... {
public:
OptionalVecAmalgamation()
: std::optional<VectorDisambiguator<PRIMITIVE_TYPES>>(std::nullopt)... {}
template <typename T>
const auto& getBaseOptVec() {
return getBaseOptVecImpl<T>();
}
template <typename T>
auto& getMutableBaseOptVec() {
return getBaseOptVecImpl<T>();
}
template <typename T>
const auto& getBaseVecUnsafe() {
return getBaseOptVecImpl<T>().value();
}
template <typename T>
auto& getMutableBaseVecUnsafe() {
return getBaseOptVecImpl<T>().value();
}
const auto empty() const { return isEmpty(); }
private:
template <typename T>
auto& getBaseOptVecImpl() {
return static_cast<std::optional<VectorDisambiguator<T>>&>(*this);
}
const auto isEmpty() const {
auto has_any_data = [&](auto& optional_base) {
if (!optional_base) {
return false;
}
return !optional_base->empty();
};
return (!has_any_data(
static_cast<std::optional<VectorDisambiguator<PRIMITIVE_TYPES>> const&>(
*this)) &&
...);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment