Git Product home page Git Product logo

Comments (10)

pah avatar pah commented on May 18, 2024

Sounds very useful, 👍 from my side.

Some remarks from my side:

  • In order to avoid surprises (compared to std::vector and the other C++ containers), the implementation should preserve the order of the remaining values in the array.
  • A similar iterator-based Erase function should be added for MemberIterators (again, preferably order-preserving).

Last, but not least, adding an operator== to GenericValue enables the C++ erase-remove idiom, std::remove_if works already:

  // remove all 'null' values
  bool IsNull( const Value& v ) { return v.IsNull(); }
  d.Erase(std::remove_if(d.Begin(), d.End(), IsNull), d.End());

The RemoveMember function currently shuffles the order (for performance reasons), which would be inconsistent. In practice, moving all elements should be quite fast as the memory is contiguous. But we could of course also add a flag (similar to kParseFlag) for these operations. What do you think?

from rapidjson.

miloyip avatar miloyip commented on May 18, 2024

I added operator==() in 71c8402.

For RemoveMember(), I think it is OK to change the order as JSON object can be thought as an unordered map. But it is also possible to add a function like EraseMember() which preserve order as JSON array.

from rapidjson.

pah avatar pah commented on May 18, 2024

In #92, I've added an Erase overload, taking MemberIterators. Would you prefer to call it EraseMember? The distinction based on the iterator type should be sufficient, isn't it?

from rapidjson.

miloyip avatar miloyip commented on May 18, 2024

I think that will be easier to identify which functions are for object and which are for array.

from rapidjson.

pah avatar pah commented on May 18, 2024

Ok, added to #92 in fc6d7a5.

from rapidjson.

miloyip avatar miloyip commented on May 18, 2024

I will amend the documentation before merging this branch.

from rapidjson.

pah avatar pah commented on May 18, 2024

Sure, thanks.

from rapidjson.

miloyip avatar miloyip commented on May 18, 2024

I try to add erase-remove in unit tests at b52e078. It passed gcc but not VC2013:

1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\algorithm(1735): error C2248: 'rapidjson::GenericValue<rapidjson::UTF8<char>,rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator>>::GenericValue' : cannot access private member declared in class 'rapidjson::GenericValue<rapidjson::UTF8<char>,rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator>>'
1>          ..\..\include\rapidjson/document.h(357) : see declaration of 'rapidjson::GenericValue<rapidjson::UTF8<char>,rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator>>::GenericValue'
1>          C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\algorithm(1748) : see reference to function template instantiation '_FwdIt std::_Remove<_Iter,_Ty>(_FwdIt,_FwdIt,const _Ty &)' being compiled
1>          with
1>          [
1>              _FwdIt=rapidjson::GenericValue<rapidjson::UTF8<char>,rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator>> *
1>  ,            _Iter=rapidjson::GenericValue<rapidjson::UTF8<char>,rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator>> *
1>  ,            _Ty=rapidjson::GenericValue<rapidjson::UTF8<char>,rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator>>
1>          ]
1>          ..\..\test\unittest\valuetest.cpp(688) : see reference to function template instantiation '_FwdIt std::remove<rapidjson::GenericValue<rapidjson::UTF8<char>,rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator>>*,rapidjson::GenericValue<rapidjson::UTF8<char>,rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator>>>(_FwdIt,_FwdIt,const _Ty &)' being compiled
1>          with
1>          [
1>              _FwdIt=rapidjson::GenericValue<rapidjson::UTF8<char>,rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator>> *
1>  ,            _Ty=rapidjson::GenericValue<rapidjson::UTF8<char>,rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator>>
1>          ]

I think it is because VC2013 force to use C++11 and remove() requires MoveAssignable.

from rapidjson.

pah avatar pah commented on May 18, 2024

Mmh, it's probably due to GenericValue's requirement to take a non-const reference to its public assignment operator and in C++11, an explicit std::move is performed before the assignment. It fails with a similar error on GCC 4.9 with -std=c++11.

We might need to add both a MoveConstructor and and MoveAssignment operator in C++11 mode. A very simple approach (hidden behind a macro RAPIDJSON_CXX11) is available in pah/rapidjson-upstream@8005b55a.

With this (and the signedness fix in my issue91removeelement branch, the "erase-remove" test works both on GCC 4.9 in C++11 mode and VS 2013 (when RAPIDJSON_CXX11 is manually defined).

from rapidjson.

pah avatar pah commented on May 18, 2024

Side note: With the move constructor, the Move function is no longer needed in C++11 code.

from rapidjson.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.