MCPcopy Create free account
hub / github.com/bytedance/sonic-cpp / addMemberImpl

Method addMemberImpl

include/sonic/dom/dynamicnode.h:672–706  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

670 }
671
672 MemberIterator addMemberImpl(StringView key, DNode& value, Allocator& alloc,
673 bool copyKey) {
674 constexpr size_t k_default_obj_cap = 16;
675 size_t count = this->Size();
676 if (count >= this->Capacity()) {
677 if (this->Capacity() == 0) {
678 setChildren(containerMalloc<MemberNode>(k_default_obj_cap, alloc));
679 } else {
680 size_t cap = this->Capacity();
681 cap += (cap + 1) / 2; // grow by factor 1.5
682 void* old_ptr = children();
683 setChildren(containerRealloc<MemberNode>(old_ptr, this->Capacity(), cap,
684 alloc));
685 }
686 }
687
688 // add member to the last pos
689 DNode name;
690 if (copyKey) {
691 name.SetString(key, alloc);
692 } else {
693 name.SetString(key);
694 }
695 DNode* last = this->getObjChildrenFirst() + count * 2;
696 last->rawAssign(name); // MemberEnd()->name
697 (last + 1)->rawAssign(value); // MemberEnd()->value
698 this->addLength(1);
699
700 // maintain map
701 if (nullptr != getMap()) {
702 // If key exists, it will be still keeped in vector but replaced in map.
703 getMap()->emplace(std::make_pair(last->GetStringView(), count));
704 }
705 return (MemberIterator)last;
706 }
707
708 sonic_force_inline bool removeMemberImpl(StringView key) {
709 MemberIterator m;

Callers 1

AddMemberFunction · 0.80

Calls 4

getObjChildrenFirstMethod · 0.95
rawAssignMethod · 0.80
SizeMethod · 0.45
CapacityMethod · 0.45

Tested by

no test coverage detected