Skip to content

Commit

Permalink
Merge pull request #33 from GolosChain/32-partial-compare-variants
Browse files Browse the repository at this point in the history
Partial compare variants #32
  • Loading branch information
afalaleev committed May 16, 2019
2 parents 31aa538 + 9290259 commit dbc0dda
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
3 changes: 3 additions & 0 deletions include/fc/variant.hpp
Expand Up @@ -411,6 +411,9 @@ namespace fc
explicit variant( const T& val );

void clear();

bool has_value(const variant&) const;

private:

uint64_t to_uint64() const;
Expand Down
2 changes: 2 additions & 0 deletions include/fc/variant_object.hpp
Expand Up @@ -95,6 +95,8 @@ namespace fc
bool operator==(const variant_object&) const;
bool operator==(const mutable_variant_object&) const;

bool has_value(const variant_object&) const;

private:
std::shared_ptr< std::vector< entry > > _key_value;
friend class mutable_variant_object;
Expand Down
21 changes: 21 additions & 0 deletions src/variant.cpp
Expand Up @@ -563,6 +563,27 @@ const variant_object& variant::get_object() const {
FC_THROW_EXCEPTION( bad_cast_exception, "Invalid cast from type '${type}' to Object", ("type",get_type()) );
}

bool variant::has_value(const variant& v) const {
if( get_type() != v.get_type() )
return false;

if( is_double() ) return std::abs(as_double() - v.as_double()) < DOUBLE_ACCURACY;
if( is_int64() ) return as_int64() == v.as_int64();
if( is_uint64() ) return as_uint64() == v.as_uint64();
if( is_int128() ) return as_int128() == v.as_int128();
if( is_uint128() ) return as_uint128() == v.as_uint128();
if( is_time() ) return as_time_point() == v.as_time_point();
if( is_array() ) return get_array() == v.get_array();
if( is_bool() ) return as_bool() == v.as_bool();

if( is_object() ) return get_object().has_value(v.get_object());

if( get_type() == type_id::string_type ) return get_string() == get_string();
if( get_type() == type_id::blob_type ) return get_blob() == get_blob();

return false;
}

void from_variant( const variant& var, variants& vo )
{
vo = var.get_array();
Expand Down
12 changes: 12 additions & 0 deletions src/variant_object.cpp
Expand Up @@ -162,6 +162,18 @@ namespace fc
return *this;
}

bool variant_object::has_value(const variant_object& obj) const {
if (size() == 0) return true;
if (size() < obj.size()) return false;

for (auto& e: obj) {
auto itr = find(e.key());
if (end() == itr) return false;
if (itr->value() != e.value()) return false;
}
return true;
}

bool variant_object::operator==(const variant_object& obj) const {
if (size() != obj.size()) return false;

Expand Down

0 comments on commit dbc0dda

Please sign in to comment.