susumu.yata
null+****@clear*****
Tue Oct 7 13:45:48 JST 2014
susumu.yata 2014-10-07 13:45:48 +0900 (Tue, 07 Oct 2014) New Revision: 58c852f07cf6e0b088aec9b126832d748cc72af4 https://github.com/groonga/grnxx/commit/58c852f07cf6e0b088aec9b126832d748cc72af4 Message: Specialize Column<Float>. Copied files: lib/grnxx/impl/column/column_float.cpp (from lib/grnxx/impl/column/column.cpp) Modified files: lib/grnxx/impl/column/Makefile.am lib/grnxx/impl/column/column.cpp lib/grnxx/impl/column/column_float.hpp lib/grnxx/impl/column/column_vector_float.cpp Modified: lib/grnxx/impl/column/Makefile.am (+1 -0) =================================================================== --- lib/grnxx/impl/column/Makefile.am 2014-10-07 13:33:14 +0900 (f97bf8f) +++ lib/grnxx/impl/column/Makefile.am 2014-10-07 13:45:48 +0900 (fb38d8f) @@ -11,6 +11,7 @@ libgrnxx_impl_column_la_LDFLAGS = @AM_LTLDFLAGS@ libgrnxx_impl_column_la_SOURCES = \ column.cpp \ column_base.cpp \ + column_float.cpp \ column_int.cpp \ column_text.cpp \ column_vector_int.cpp \ Modified: lib/grnxx/impl/column/column.cpp (+0 -4) =================================================================== --- lib/grnxx/impl/column/column.cpp 2014-10-07 13:33:14 +0900 (993d55b) +++ lib/grnxx/impl/column/column.cpp 2014-10-07 13:45:48 +0900 (ecafca0) @@ -5,8 +5,6 @@ #include "grnxx/impl/table.hpp" #include "grnxx/index.hpp" -#include <set> - namespace grnxx { namespace impl { @@ -23,7 +21,6 @@ bool Column<T>::set(Error *error, Int row_id, const Datum &datum) { T old_value = get(row_id); T new_value; datum.force(&new_value); - // TODO: Note that NaN != NaN. if (new_value != old_value) { for (Int i = 0; i < num_indexes(); ++i) { if (!indexes_[i]->insert(error, row_id, datum)) { @@ -106,7 +103,6 @@ template <typename T> Column<T>::Column() : ColumnBase(), values_() {} template class Column<Bool>; -template class Column<Float>; template class Column<GeoPoint>; template class Column<Vector<Bool>>; Copied: lib/grnxx/impl/column/column_float.cpp (+22 -35) 54% =================================================================== --- lib/grnxx/impl/column/column.cpp 2014-10-07 13:33:14 +0900 (993d55b) +++ lib/grnxx/impl/column/column_float.cpp 2014-10-07 13:45:48 +0900 (53beb89) @@ -5,26 +5,23 @@ #include "grnxx/impl/table.hpp" #include "grnxx/index.hpp" -#include <set> +#include <cmath> namespace grnxx { namespace impl { -template <typename T> -bool Column<T>::set(Error *error, Int row_id, const Datum &datum) { - if (datum.type() != TypeTraits<T>::data_type()) { +bool Column<Float>::set(Error *error, Int row_id, const Datum &datum) { + if (datum.type() != TypeTraits<Float>::data_type()) { GRNXX_ERROR_SET(error, INVALID_ARGUMENT, "Wrong data type"); return false; } if (!table_->test_row(error, row_id)) { return false; } - // Note that a Bool object does not have its own address. - T old_value = get(row_id); - T new_value; - datum.force(&new_value); - // TODO: Note that NaN != NaN. - if (new_value != old_value) { + Float old_value = get(row_id); + Float new_value = datum.force_float(); + if ((std::isnan(new_value) && std::isnan(old_value)) || + (new_value != old_value)) { for (Int i = 0; i < num_indexes(); ++i) { if (!indexes_[i]->insert(error, row_id, datum)) { for (Int j = 0; j < i; ++i) { @@ -41,8 +38,7 @@ bool Column<T>::set(Error *error, Int row_id, const Datum &datum) { return true; } -template <typename T> -bool Column<T>::get(Error *error, Int row_id, Datum *datum) const { +bool Column<Float>::get(Error *error, Int row_id, Datum *datum) const { if (!table_->test_row(error, row_id)) { return false; } @@ -50,38 +46,36 @@ bool Column<T>::get(Error *error, Int row_id, Datum *datum) const { return true; } -template <typename T> -unique_ptr<Column<T>> Column<T>::create(Error *error, - Table *table, - const StringCRef &name, - const ColumnOptions &options) { +unique_ptr<Column<Float>> Column<Float>::create(Error *error, + Table *table, + const StringCRef &name, + const ColumnOptions &options) { unique_ptr<Column> column(new (nothrow) Column); if (!column) { GRNXX_ERROR_SET(error, NO_MEMORY, "Memory allocation failed"); return nullptr; } if (!column->initialize_base(error, table, name, - TypeTraits<T>::data_type(), options)) { + TypeTraits<Float>::data_type(), options)) { return nullptr; } if (!column->values_.resize(error, table->max_row_id() + 1, - TypeTraits<T>::default_value())) { + TypeTraits<Float>::default_value())) { return nullptr; } return column; } -template <typename T> -Column<T>::~Column() {} +Column<Float>::~Column() {} -template <typename T> -bool Column<T>::set_default_value(Error *error, Int row_id) { +bool Column<Float>::set_default_value(Error *error, Int row_id) { if (row_id >= values_.size()) { - if (!values_.resize(error, row_id + 1, TypeTraits<T>::default_value())) { + if (!values_.resize(error, row_id + 1, + TypeTraits<Float>::default_value())) { return false; } } - T value = TypeTraits<T>::default_value(); + Float value = TypeTraits<Float>::default_value(); for (Int i = 0; i < num_indexes(); ++i) { if (!indexes_[i]->insert(error, row_id, value)) { for (Int j = 0; j < i; ++j) { @@ -94,21 +88,14 @@ bool Column<T>::set_default_value(Error *error, Int row_id) { return true; } -template <typename T> -void Column<T>::unset(Int row_id) { +void Column<Float>::unset(Int row_id) { for (Int i = 0; i < num_indexes(); ++i) { indexes_[i]->remove(nullptr, row_id, get(row_id)); } - values_.set(row_id, TypeTraits<T>::default_value()); + values_.set(row_id, TypeTraits<Float>::default_value()); } -template <typename T> -Column<T>::Column() : ColumnBase(), values_() {} - -template class Column<Bool>; -template class Column<Float>; -template class Column<GeoPoint>; -template class Column<Vector<Bool>>; +Column<Float>::Column() : ColumnBase(), values_() {} } // namespace impl } // namespace grnxx Modified: lib/grnxx/impl/column/column_float.hpp (+46 -1) =================================================================== --- lib/grnxx/impl/column/column_float.hpp 2014-10-07 13:33:14 +0900 (9fb732c) +++ lib/grnxx/impl/column/column_float.hpp 2014-10-07 13:45:48 +0900 (efc444d) @@ -6,7 +6,52 @@ namespace grnxx { namespace impl { -// TODO +template <> +class Column<Float> : public ColumnBase { + public: + // -- Public API (grnxx/column.hpp) -- + + bool set(Error *error, Int row_id, const Datum &datum); + bool get(Error *error, Int row_id, Datum *datum) const; + + // -- Internal API (grnxx/impl/column/column_base.hpp) -- + + ~Column(); + + bool set_default_value(Error *error, Int row_id); + void unset(Int row_id); + + // -- Internal API -- + + // Create a new column. + // + // Returns a pointer to the column on success. + // On failure, returns nullptr and stores error information into "*error" if + // "error" != nullptr. + static unique_ptr<Column> create(Error *error, + Table *table, + const StringCRef &name, + const ColumnOptions &options); + + // Return a value identified by "row_id". + // + // Assumes that "row_id" is valid. Otherwise, the result is undefined. + Float get(Int row_id) const { + return values_[row_id]; + } + + // Read values. + void read(ArrayCRef<Record> records, ArrayRef<Float> values) const { + for (Int i = 0; i < records.size(); ++i) { + values.set(i, get(records.get_row_id(i))); + } + } + + private: + Array<Float> values_; + + Column(); +}; } // namespace impl } // namespace grnxx Modified: lib/grnxx/impl/column/column_vector_float.cpp (+0 -1) =================================================================== --- lib/grnxx/impl/column/column_vector_float.cpp 2014-10-07 13:33:14 +0900 (ca6b184) +++ lib/grnxx/impl/column/column_vector_float.cpp 2014-10-07 13:45:48 +0900 (0802911) @@ -7,7 +7,6 @@ namespace grnxx { namespace impl { - bool Column<Vector<Float>>::set(Error *error, Int row_id, const Datum &datum) { if (datum.type() != FLOAT_VECTOR_DATA) { GRNXX_ERROR_SET(error, INVALID_ARGUMENT, "Wrong data type"); -------------- next part -------------- HTML����������������������������...Download