mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 08:42:39 +01:00 
			
		
		
		
	Implement comparison out of VariantData
				
					
				
			This commit is contained in:
		| @@ -20,12 +20,4 @@ inline typename TVisitor::result_type arrayAccept(const CollectionData *arr, | |||||||
|   else |   else | ||||||
|     return visitor.visitNull(); |     return visitor.visitNull(); | ||||||
| } | } | ||||||
|  |  | ||||||
| inline bool arrayEquals(const CollectionData *lhs, const CollectionData *rhs) { |  | ||||||
|   if (lhs == rhs) |  | ||||||
|     return true; |  | ||||||
|   if (!lhs || !rhs) |  | ||||||
|     return false; |  | ||||||
|   return lhs->equalsArray(*rhs); |  | ||||||
| } |  | ||||||
| }  // namespace ARDUINOJSON_NAMESPACE | }  // namespace ARDUINOJSON_NAMESPACE | ||||||
|   | |||||||
| @@ -83,7 +83,26 @@ class ArrayConstRef : public ArrayRefBase<const CollectionData>, | |||||||
|   FORCE_INLINE ArrayConstRef(const CollectionData* data) : base_type(data) {} |   FORCE_INLINE ArrayConstRef(const CollectionData* data) : base_type(data) {} | ||||||
|  |  | ||||||
|   FORCE_INLINE bool operator==(ArrayConstRef rhs) const { |   FORCE_INLINE bool operator==(ArrayConstRef rhs) const { | ||||||
|     return arrayEquals(_data, rhs._data); |     if (_data == rhs._data) | ||||||
|  |       return true; | ||||||
|  |     if (!_data || !rhs._data) | ||||||
|  |       return false; | ||||||
|  |  | ||||||
|  |     iterator it1 = begin(); | ||||||
|  |     iterator it2 = rhs.begin(); | ||||||
|  |  | ||||||
|  |     for (;;) { | ||||||
|  |       bool end1 = it1 == end(); | ||||||
|  |       bool end2 = it2 == rhs.end(); | ||||||
|  |       if (end1 && end2) | ||||||
|  |         return true; | ||||||
|  |       if (end1 || end2) | ||||||
|  |         return false; | ||||||
|  |       if (*it1 != *it2) | ||||||
|  |         return false; | ||||||
|  |       ++it1; | ||||||
|  |       ++it2; | ||||||
|  |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   FORCE_INLINE VariantConstRef operator[](size_t index) const { |   FORCE_INLINE VariantConstRef operator[](size_t index) const { | ||||||
| @@ -138,7 +157,7 @@ class ArrayRef : public ArrayRefBase<CollectionData>, | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   FORCE_INLINE bool operator==(ArrayRef rhs) const { |   FORCE_INLINE bool operator==(ArrayRef rhs) const { | ||||||
|     return arrayEquals(_data, rhs._data); |     return ArrayConstRef(_data) == ArrayConstRef(rhs._data); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   // Internal use |   // Internal use | ||||||
|   | |||||||
| @@ -36,8 +36,6 @@ class CollectionData { | |||||||
|  |  | ||||||
|   void removeElement(size_t index); |   void removeElement(size_t index); | ||||||
|  |  | ||||||
|   bool equalsArray(const CollectionData &other) const; |  | ||||||
|  |  | ||||||
|   // Object only |   // Object only | ||||||
|  |  | ||||||
|   template <typename TAdaptedString, typename TStoragePolicy> |   template <typename TAdaptedString, typename TStoragePolicy> | ||||||
| @@ -58,8 +56,6 @@ class CollectionData { | |||||||
|   template <typename TAdaptedString> |   template <typename TAdaptedString> | ||||||
|   bool containsKey(const TAdaptedString &key) const; |   bool containsKey(const TAdaptedString &key) const; | ||||||
|  |  | ||||||
|   bool equalsObject(const CollectionData &other) const; |  | ||||||
|  |  | ||||||
|   // Generic |   // Generic | ||||||
|  |  | ||||||
|   void clear(); |   void clear(); | ||||||
|   | |||||||
| @@ -11,10 +11,6 @@ | |||||||
|  |  | ||||||
| namespace ARDUINOJSON_NAMESPACE { | namespace ARDUINOJSON_NAMESPACE { | ||||||
|  |  | ||||||
| inline bool variantEquals(const VariantData* a, const VariantData* b) { |  | ||||||
|   return variantCompare(a, b) == COMPARE_RESULT_EQUAL; |  | ||||||
| } |  | ||||||
|  |  | ||||||
| inline VariantSlot* CollectionData::addSlot(MemoryPool* pool) { | inline VariantSlot* CollectionData::addSlot(MemoryPool* pool) { | ||||||
|   VariantSlot* slot = pool->allocVariant(); |   VariantSlot* slot = pool->allocVariant(); | ||||||
|   if (!slot) |   if (!slot) | ||||||
| @@ -77,33 +73,6 @@ inline bool CollectionData::copyFrom(const CollectionData& src, | |||||||
|   return true; |   return true; | ||||||
| } | } | ||||||
|  |  | ||||||
| inline bool CollectionData::equalsObject(const CollectionData& other) const { |  | ||||||
|   size_t count = 0; |  | ||||||
|   for (VariantSlot* slot = _head; slot; slot = slot->next()) { |  | ||||||
|     VariantData* v1 = slot->data(); |  | ||||||
|     VariantData* v2 = other.getMember(adaptString(slot->key())); |  | ||||||
|     if (!variantEquals(v1, v2)) |  | ||||||
|       return false; |  | ||||||
|     count++; |  | ||||||
|   } |  | ||||||
|   return count == other.size(); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| inline bool CollectionData::equalsArray(const CollectionData& other) const { |  | ||||||
|   VariantSlot* s1 = _head; |  | ||||||
|   VariantSlot* s2 = other._head; |  | ||||||
|   for (;;) { |  | ||||||
|     if (s1 == s2) |  | ||||||
|       return true; |  | ||||||
|     if (!s1 || !s2) |  | ||||||
|       return false; |  | ||||||
|     if (!variantEquals(s1->data(), s2->data())) |  | ||||||
|       return false; |  | ||||||
|     s1 = s1->next(); |  | ||||||
|     s2 = s2->next(); |  | ||||||
|   } |  | ||||||
| } |  | ||||||
|  |  | ||||||
| template <typename TAdaptedString> | template <typename TAdaptedString> | ||||||
| inline VariantSlot* CollectionData::getSlot(TAdaptedString key) const { | inline VariantSlot* CollectionData::getSlot(TAdaptedString key) const { | ||||||
|   if (key.isNull()) |   if (key.isNull()) | ||||||
|   | |||||||
| @@ -17,14 +17,6 @@ typename TVisitor::result_type objectAccept(const CollectionData *obj, | |||||||
|     return visitor.visitNull(); |     return visitor.visitNull(); | ||||||
| } | } | ||||||
|  |  | ||||||
| inline bool objectEquals(const CollectionData *lhs, const CollectionData *rhs) { |  | ||||||
|   if (lhs == rhs) |  | ||||||
|     return true; |  | ||||||
|   if (!lhs || !rhs) |  | ||||||
|     return false; |  | ||||||
|   return lhs->equalsObject(*rhs); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| template <typename TAdaptedString> | template <typename TAdaptedString> | ||||||
| inline VariantData *objectGetMember(const CollectionData *obj, | inline VariantData *objectGetMember(const CollectionData *obj, | ||||||
|                                     TAdaptedString key) { |                                     TAdaptedString key) { | ||||||
|   | |||||||
| @@ -127,7 +127,19 @@ class ObjectConstRef : public ObjectRefBase<const CollectionData>, | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   FORCE_INLINE bool operator==(ObjectConstRef rhs) const { |   FORCE_INLINE bool operator==(ObjectConstRef rhs) const { | ||||||
|     return objectEquals(_data, rhs._data); |     if (_data == rhs._data) | ||||||
|  |       return true; | ||||||
|  |  | ||||||
|  |     if (!_data || !rhs._data) | ||||||
|  |       return false; | ||||||
|  |  | ||||||
|  |     size_t count = 0; | ||||||
|  |     for (iterator it = begin(); it != end(); ++it) { | ||||||
|  |       if (it->value() != rhs[it->key()]) | ||||||
|  |         return false; | ||||||
|  |       count++; | ||||||
|  |     } | ||||||
|  |     return count == rhs.size(); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  private: |  private: | ||||||
| @@ -215,7 +227,7 @@ class ObjectRef : public ObjectRefBase<CollectionData>, | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   FORCE_INLINE bool operator==(ObjectRef rhs) const { |   FORCE_INLINE bool operator==(ObjectRef rhs) const { | ||||||
|     return objectEquals(_data, rhs._data); |     return ObjectConstRef(_data) == ObjectConstRef(rhs._data); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   FORCE_INLINE void remove(iterator it) const { |   FORCE_INLINE void remove(iterator it) const { | ||||||
|   | |||||||
| @@ -89,7 +89,7 @@ struct ArrayComparer : ComparerBase { | |||||||
|   explicit ArrayComparer(const CollectionData &rhs) : _rhs(&rhs) {} |   explicit ArrayComparer(const CollectionData &rhs) : _rhs(&rhs) {} | ||||||
|  |  | ||||||
|   CompareResult visitArray(const CollectionData &lhs) { |   CompareResult visitArray(const CollectionData &lhs) { | ||||||
|     if (lhs.equalsArray(*_rhs)) |     if (ArrayConstRef(&lhs) == ArrayConstRef(_rhs)) | ||||||
|       return COMPARE_RESULT_EQUAL; |       return COMPARE_RESULT_EQUAL; | ||||||
|     else |     else | ||||||
|       return COMPARE_RESULT_DIFFER; |       return COMPARE_RESULT_DIFFER; | ||||||
| @@ -102,7 +102,7 @@ struct ObjectComparer : ComparerBase { | |||||||
|   explicit ObjectComparer(const CollectionData &rhs) : _rhs(&rhs) {} |   explicit ObjectComparer(const CollectionData &rhs) : _rhs(&rhs) {} | ||||||
|  |  | ||||||
|   CompareResult visitObject(const CollectionData &lhs) { |   CompareResult visitObject(const CollectionData &lhs) { | ||||||
|     if (lhs.equalsObject(*_rhs)) |     if (ObjectConstRef(&lhs) == ObjectConstRef(_rhs)) | ||||||
|       return COMPARE_RESULT_EQUAL; |       return COMPARE_RESULT_EQUAL; | ||||||
|     else |     else | ||||||
|       return COMPARE_RESULT_DIFFER; |       return COMPARE_RESULT_DIFFER; | ||||||
| @@ -201,8 +201,4 @@ CompareResult compare(const T1 &lhs, const T2 &rhs) { | |||||||
|   return lhs.accept(comparer); |   return lhs.accept(comparer); | ||||||
| } | } | ||||||
|  |  | ||||||
| inline int variantCompare(const VariantData *a, const VariantData *b) { |  | ||||||
|   return compare(VariantConstRef(a), VariantConstRef(b)); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| }  // namespace ARDUINOJSON_NAMESPACE | }  // namespace ARDUINOJSON_NAMESPACE | ||||||
|   | |||||||
| @@ -43,8 +43,6 @@ inline bool variantCopyFrom(VariantData *dst, const VariantData *src, | |||||||
|   return dst->copyFrom(*src, pool); |   return dst->copyFrom(*src, pool); | ||||||
| } | } | ||||||
|  |  | ||||||
| inline int variantCompare(const VariantData *a, const VariantData *b); |  | ||||||
|  |  | ||||||
| inline void variantSetNull(VariantData *var) { | inline void variantSetNull(VariantData *var) { | ||||||
|   if (!var) |   if (!var) | ||||||
|     return; |     return; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user