Просмотр исходного кода

Store current and next slot id in `CollectionIterator`

Benoit Blanchon 2 лет назад
Родитель
Сommit
d4af8cffa7

+ 4 - 3
src/ArduinoJson/Collection/CollectionData.hpp

@@ -18,7 +18,7 @@ class CollectionIterator {
   friend class CollectionData;
 
  public:
-  CollectionIterator() : slot_(nullptr) {}
+  CollectionIterator() : slot_(nullptr), currentId_(NULL_SLOT) {}
 
   void next(const ResourceManager* resources);
 
@@ -64,9 +64,10 @@ class CollectionIterator {
   }
 
  private:
-  CollectionIterator(VariantSlot* slot) : slot_(slot) {}
+  CollectionIterator(VariantSlot* slot, SlotId slotId);
 
   VariantSlot* slot_;
+  SlotId currentId_, nextId_;
 };
 
 class CollectionData {
@@ -84,7 +85,7 @@ class CollectionData {
   using iterator = CollectionIterator;
 
   iterator createIterator(const ResourceManager* resources) const {
-    return iterator(resources->getSlot(head_));
+    return iterator(resources->getSlot(head_), head_);
   }
 
   size_t memoryUsage(const ResourceManager*) const;

+ 12 - 8
src/ArduinoJson/Collection/CollectionImpl.hpp

@@ -12,6 +12,11 @@
 
 ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
 
+inline CollectionIterator::CollectionIterator(VariantSlot* slot, SlotId slotId)
+    : slot_(slot), currentId_(slotId) {
+  nextId_ = slot_ ? slot_->next() : NULL_SLOT;
+}
+
 inline const char* CollectionIterator::key() const {
   ARDUINOJSON_ASSERT(slot_ != nullptr);
   return slot_->key();
@@ -35,19 +40,18 @@ inline bool CollectionIterator::ownsKey() const {
 }
 
 inline void CollectionIterator::next(const ResourceManager* resources) {
-  ARDUINOJSON_ASSERT(slot_ != nullptr);
-  auto nextId = slot_->next();
-  if (nextId != NULL_SLOT)
-    slot_ = resources->getSlot(nextId);
-  else
-    slot_ = nullptr;
+  ARDUINOJSON_ASSERT(currentId_ != NULL_SLOT);
+  slot_ = resources->getSlot(nextId_);
+  currentId_ = nextId_;
+  if (slot_)
+    nextId_ = slot_->next();
 }
 
 inline CollectionData::iterator CollectionData::addSlot(
     ResourceManager* resources) {
   auto slot = resources->allocSlot();
   if (!slot)
-    return nullptr;
+    return {};
   if (tail_ != NULL_SLOT) {
     auto tail = resources->getSlot(tail_);
     tail->setNext(slot.id());
@@ -56,7 +60,7 @@ inline CollectionData::iterator CollectionData::addSlot(
     head_ = slot.id();
     tail_ = slot.id();
   }
-  return iterator(slot);
+  return iterator(slot, slot.id());
 }
 
 inline void CollectionData::clear(ResourceManager* resources) {