susumu.yata
null+****@clear*****
Wed Aug 27 14:56:44 JST 2014
susumu.yata 2014-08-27 14:56:44 +0900 (Wed, 27 Aug 2014) New Revision: c6c4298f720595b82a32860ba9716b54c400ac68 https://github.com/groonga/grnxx/commit/c6c4298f720595b82a32860ba9716b54c400ac68 Message: Add Vector<Bool> which can store at most 58 Bool values. Modified files: include/grnxx/datum.hpp include/grnxx/types.hpp lib/grnxx/column.cpp Modified: include/grnxx/datum.hpp (+10 -0) =================================================================== --- include/grnxx/datum.hpp 2014-08-27 10:43:26 +0900 (b3a1bf5) +++ include/grnxx/datum.hpp 2014-08-27 14:56:44 +0900 (10e2d2f) @@ -25,6 +25,9 @@ class Datum { Datum(String value) : type_(TEXT_DATA), text_(value) {} + Datum(Vector<Bool> value) + : type_(VECTOR_BOOL_DATA), + vector_bool_(value) {} // Return the data type. DataType type() const { @@ -47,6 +50,9 @@ class Datum { Text force_text() const { return text_; } + Vector<Bool> force_vector_bool() const { + return vector_bool_; + } // Force the specified interpretation. void force(Bool *value) const { @@ -64,6 +70,9 @@ class Datum { void force(Text *value) const { *value = text_; } + void force(Vector<Bool> *value) const { + *value = vector_bool_; + } private: DataType type_; @@ -73,6 +82,7 @@ class Datum { Float float_; GeoPoint geo_point_; String text_; + Vector<Bool> vector_bool_; }; }; Modified: include/grnxx/types.hpp (+81 -25) =================================================================== --- include/grnxx/types.hpp 2014-08-27 10:43:26 +0900 (05f956e) +++ include/grnxx/types.hpp 2014-08-27 14:56:44 +0900 (3866034) @@ -3,6 +3,7 @@ #include <cinttypes> #include <cstring> +#include <initializer_list> #include <limits> #include <memory> @@ -82,12 +83,12 @@ enum DataType { // Type: Vector. // Value: Vector of above data types. // Default: {}. - BOOL_VECTOR_DATA, - INT_VECTOR_DATA, - FLOAT_VECTOR_DATA, - GEO_POINT_VECTOR_DATA, - TEXT_VECTOR_DATA, - ROW_REF_VECTOR_DATA + VECTOR_BOOL_DATA, + VECTOR_INT_DATA, + VECTOR_FLOAT_DATA, + VECTOR_GEO_POINT_DATA, + VECTOR_TEXT_DATA, + VECTOR_ROW_REF_DATA }; using Bool = bool; @@ -210,37 +211,84 @@ inline bool operator>=(String lhs, String rhs) { using Text = String; -class RowRef { +template <typename T> class Vector; + +// A Vector<Bool> contains at most 58 Bool values. +template <> +class Vector<Bool> { public: - // The default constructor does nothing. - RowRef() = default; + Vector() = default; + Vector(std::initializer_list<Bool> bits) : data_(0) { + uint64_t size = static_cast<uint64_t>(bits.size()); + if (size > 58) { + size = 58; + } + uint64_t i = 0; + for (auto it = bits.begin(); it != bits.end(); ++it) { + if (*it) { + data_ |= uint64_t(1) << i; + } + ++i; + } + data_ |= size << 58; + } + Vector(uint64_t bits, Int size) + : data_((bits & mask(size)) | + (static_cast<uint64_t>(std::min(size, Int(58))) << 58)) {} + Vector(const Vector &) = default; - // Set the table and the row ID. - RowRef(Table *table, Int row_id) : table_(table), row_id_(row_id) {} + Vector &operator=(const Vector &) = default; - // Set the table. - void set_table(Table *table) { - table_ = table; + // Return the number of Bool values. + Int size() const { + return static_cast<Int>(data_ >> 58); } - // Set the row ID. - void set_row_id(Int row_id) { - row_id_ = row_id; + // Return the "i"-th Bool value. + // + // If "i" is invalid, the result is undefined. + Bool get(Int i) const { + return (data_ & (uint64_t(1) << i)) != 0; + } + // Set the "i"-th Bool value. + // + // If "i" is invalid, the result is undefined. + void set(Int i, Bool value) { + if (value) { + data_ |= uint64_t(1) << i; + } else { + data_ &= ~(uint64_t(1) << i); + } } - // Return the table. - Table *table() const { - return table_; + // Return the "i"-th Bool value. + // + // If "i" is invalid, the result is undefined. + Bool operator[](Int i) const { + return get(i); } - // Return the row ID. - Int row_id() const { - return row_id_; + + // Return the set of Bool values. + uint64_t bits() const { + return data_ & mask(58); } private: - Table *table_; // Target table. - Int row_id_; // Row ID number. + uint64_t data_; + + static uint64_t mask(Int size) { + return (uint64_t(1) << size) - 1; + } }; +inline Bool operator==(Vector<Bool> lhs, Vector<Bool> rhs) { + return (lhs.size() == rhs.size()) && + ((lhs.bits() ^ rhs.bits()) == 0); +} +inline Bool operator!=(Vector<Bool> lhs, Vector<Bool> rhs) { + return (lhs.size() == rhs.size()) || + ((lhs.bits() ^ rhs.bits()) != 0); +} + // Type information. template <typename T> struct TypeTraits; template <> struct TypeTraits <Bool> { @@ -283,6 +331,14 @@ template <> struct TypeTraits <Text> { return Text("", 0); } }; +template <> struct TypeTraits <Vector<Bool>> { + static DataType data_type() { + return VECTOR_BOOL_DATA; + } + static Vector<Bool> default_value() { + return Vector<Bool>(0, 0); + } +}; // Zero is reserved for representing a null reference. constexpr Int NULL_ROW_ID = 0; Modified: lib/grnxx/column.cpp (+3 -0) =================================================================== --- lib/grnxx/column.cpp 2014-08-27 10:43:26 +0900 (0b67ad7) +++ lib/grnxx/column.cpp 2014-08-27 14:56:44 +0900 (fd3c573) @@ -88,6 +88,9 @@ unique_ptr<Column> Column::create(Error *error, case TEXT_DATA: { return ColumnImpl<Text>::create(error, table, name, options); } + case VECTOR_BOOL_DATA: { + return ColumnImpl<Vector<Bool>>::create(error, table, name, options); + } default: { // TODO: Other data types are not supported yet. GRNXX_ERROR_SET(error, NOT_SUPPORTED_YET, "Not suported yet"); -------------- next part -------------- HTML����������������������������...Download