int ensureCapacityForNextChild(int childId, int parentId, int childrenCount) {
      if (myCurrentParent >= 0) {
        if (childrenCount == myExpectedChildrenCount - 1) {
          myCurrentParent = -1;
        }
        else if (parentId != myCurrentParent) {
          myCurrentParent = -1;
          return switchChildrenToJoinedList(parentId, childrenCount, myExpectedChildrenCount - childrenCount);
        }
      }

      int childrenStart = getChildrenStart(parentId);
      ChildrenStorage storage = getChildrenStorage(childrenStart);
      if (storage == ChildrenStorage.inPlainList) {
        if (childrenCount == 0) {
          assert parentId == childId - 1;
        }
        else if (areChildrenNonAdjacent(childId, parentId)) {
          return switchChildrenToJoinedList(parentId, childrenCount, 0);
        }
      }
      else if (storage == ChildrenStorage.inJoinedList && !canAddToJoinedList(childrenStart + childrenCount)) {
        switchChildrenToTempMap(parentId);
        return IN_TEMP_MAP;
      }
      return childrenStart;
    }
int ensureCapacityForNextChild(int childId, int parentId, int childrenCount)