mirror of
https://github.com/open-source-parsers/jsoncpp.git
synced 2024-12-27 11:21:02 +08:00
Ran clang-format again, this time hitting .inl files too.
clang-format -i $(find . -name '*.h' -or -name '*.cpp' -or -name '*.inl')
This commit is contained in:
parent
57dde78308
commit
ba330893d7
@ -9,6 +9,8 @@
|
||||
#define JSONCPP_VERSION_MINOR 7
|
||||
#define JSONCPP_VERSION_PATCH 0
|
||||
#define JSONCPP_VERSION_QUALIFIER
|
||||
# define JSONCPP_VERSION_HEXA ((JSONCPP_VERSION_MAJOR << 24) | (JSONCPP_VERSION_MINOR << 16) | (JSONCPP_VERSION_PATCH << 8))
|
||||
#define JSONCPP_VERSION_HEXA \
|
||||
((JSONCPP_VERSION_MAJOR << 24) | (JSONCPP_VERSION_MINOR << 16) | \
|
||||
(JSONCPP_VERSION_PATCH << 8))
|
||||
|
||||
#endif // JSON_VERSION_H_INCLUDED
|
||||
|
@ -15,40 +15,28 @@ namespace Json {
|
||||
// //////////////////////////////////////////////////////////////////
|
||||
// //////////////////////////////////////////////////////////////////
|
||||
|
||||
ValueArrayAllocator::~ValueArrayAllocator()
|
||||
{
|
||||
}
|
||||
ValueArrayAllocator::~ValueArrayAllocator() {}
|
||||
|
||||
// //////////////////////////////////////////////////////////////////
|
||||
// class DefaultValueArrayAllocator
|
||||
// //////////////////////////////////////////////////////////////////
|
||||
#ifdef JSON_USE_SIMPLE_INTERNAL_ALLOCATOR
|
||||
class DefaultValueArrayAllocator : public ValueArrayAllocator
|
||||
{
|
||||
class DefaultValueArrayAllocator : public ValueArrayAllocator {
|
||||
public: // overridden from ValueArrayAllocator
|
||||
virtual ~DefaultValueArrayAllocator()
|
||||
{
|
||||
}
|
||||
virtual ~DefaultValueArrayAllocator() {}
|
||||
|
||||
virtual ValueInternalArray *newArray()
|
||||
{
|
||||
return new ValueInternalArray();
|
||||
}
|
||||
virtual ValueInternalArray* newArray() { return new ValueInternalArray(); }
|
||||
|
||||
virtual ValueInternalArray *newArrayCopy( const ValueInternalArray &other )
|
||||
{
|
||||
virtual ValueInternalArray* newArrayCopy(const ValueInternalArray& other) {
|
||||
return new ValueInternalArray(other);
|
||||
}
|
||||
|
||||
virtual void destructArray( ValueInternalArray *array )
|
||||
{
|
||||
delete array;
|
||||
}
|
||||
virtual void destructArray(ValueInternalArray* array) { delete array; }
|
||||
|
||||
virtual void reallocateArrayPageIndex( Value **&indexes,
|
||||
virtual void
|
||||
reallocateArrayPageIndex(Value**& indexes,
|
||||
ValueInternalArray::PageIndex& indexCount,
|
||||
ValueInternalArray::PageIndex minNewIndexCount )
|
||||
{
|
||||
ValueInternalArray::PageIndex minNewIndexCount) {
|
||||
ValueInternalArray::PageIndex newIndexCount = (indexCount * 3) / 2 + 1;
|
||||
if (minNewIndexCount > newIndexCount)
|
||||
newIndexCount = minNewIndexCount;
|
||||
@ -58,19 +46,17 @@ public: // overridden from ValueArrayAllocator
|
||||
indexes = static_cast<Value**>(newIndexes);
|
||||
}
|
||||
virtual void releaseArrayPageIndex(Value** indexes,
|
||||
ValueInternalArray::PageIndex indexCount )
|
||||
{
|
||||
ValueInternalArray::PageIndex indexCount) {
|
||||
if (indexes)
|
||||
free(indexes);
|
||||
}
|
||||
|
||||
virtual Value *allocateArrayPage()
|
||||
{
|
||||
return static_cast<Value *>( malloc( sizeof(Value) * ValueInternalArray::itemsPerPage ) );
|
||||
virtual Value* allocateArrayPage() {
|
||||
return static_cast<Value*>(
|
||||
malloc(sizeof(Value) * ValueInternalArray::itemsPerPage));
|
||||
}
|
||||
|
||||
virtual void releaseArrayPage( Value *value )
|
||||
{
|
||||
virtual void releaseArrayPage(Value* value) {
|
||||
if (value)
|
||||
free(value);
|
||||
}
|
||||
@ -78,40 +64,33 @@ public: // overridden from ValueArrayAllocator
|
||||
|
||||
#else // #ifdef JSON_USE_SIMPLE_INTERNAL_ALLOCATOR
|
||||
/// @todo make this thread-safe (lock when accessign batch allocator)
|
||||
class DefaultValueArrayAllocator : public ValueArrayAllocator
|
||||
{
|
||||
class DefaultValueArrayAllocator : public ValueArrayAllocator {
|
||||
public: // overridden from ValueArrayAllocator
|
||||
virtual ~DefaultValueArrayAllocator()
|
||||
{
|
||||
}
|
||||
virtual ~DefaultValueArrayAllocator() {}
|
||||
|
||||
virtual ValueInternalArray *newArray()
|
||||
{
|
||||
virtual ValueInternalArray* newArray() {
|
||||
ValueInternalArray* array = arraysAllocator_.allocate();
|
||||
new (array) ValueInternalArray(); // placement new
|
||||
return array;
|
||||
}
|
||||
|
||||
virtual ValueInternalArray *newArrayCopy( const ValueInternalArray &other )
|
||||
{
|
||||
virtual ValueInternalArray* newArrayCopy(const ValueInternalArray& other) {
|
||||
ValueInternalArray* array = arraysAllocator_.allocate();
|
||||
new (array) ValueInternalArray(other); // placement new
|
||||
return array;
|
||||
}
|
||||
|
||||
virtual void destructArray( ValueInternalArray *array )
|
||||
{
|
||||
if ( array )
|
||||
{
|
||||
virtual void destructArray(ValueInternalArray* array) {
|
||||
if (array) {
|
||||
array->~ValueInternalArray();
|
||||
arraysAllocator_.release(array);
|
||||
}
|
||||
}
|
||||
|
||||
virtual void reallocateArrayPageIndex( Value **&indexes,
|
||||
virtual void
|
||||
reallocateArrayPageIndex(Value**& indexes,
|
||||
ValueInternalArray::PageIndex& indexCount,
|
||||
ValueInternalArray::PageIndex minNewIndexCount )
|
||||
{
|
||||
ValueInternalArray::PageIndex minNewIndexCount) {
|
||||
ValueInternalArray::PageIndex newIndexCount = (indexCount * 3) / 2 + 1;
|
||||
if (minNewIndexCount > newIndexCount)
|
||||
newIndexCount = minNewIndexCount;
|
||||
@ -121,144 +100,109 @@ public: // overridden from ValueArrayAllocator
|
||||
indexes = static_cast<Value**>(newIndexes);
|
||||
}
|
||||
virtual void releaseArrayPageIndex(Value** indexes,
|
||||
ValueInternalArray::PageIndex indexCount )
|
||||
{
|
||||
ValueInternalArray::PageIndex indexCount) {
|
||||
if (indexes)
|
||||
free(indexes);
|
||||
}
|
||||
|
||||
virtual Value *allocateArrayPage()
|
||||
{
|
||||
virtual Value* allocateArrayPage() {
|
||||
return static_cast<Value*>(pagesAllocator_.allocate());
|
||||
}
|
||||
|
||||
virtual void releaseArrayPage( Value *value )
|
||||
{
|
||||
virtual void releaseArrayPage(Value* value) {
|
||||
if (value)
|
||||
pagesAllocator_.release(value);
|
||||
}
|
||||
|
||||
private:
|
||||
BatchAllocator<ValueInternalArray, 1> arraysAllocator_;
|
||||
BatchAllocator<Value, ValueInternalArray::itemsPerPage> pagesAllocator_;
|
||||
};
|
||||
#endif // #ifdef JSON_USE_SIMPLE_INTERNAL_ALLOCATOR
|
||||
|
||||
static ValueArrayAllocator *&arrayAllocator()
|
||||
{
|
||||
static ValueArrayAllocator*& arrayAllocator() {
|
||||
static DefaultValueArrayAllocator defaultAllocator;
|
||||
static ValueArrayAllocator* arrayAllocator = &defaultAllocator;
|
||||
return arrayAllocator;
|
||||
}
|
||||
|
||||
static struct DummyArrayAllocatorInitializer {
|
||||
DummyArrayAllocatorInitializer()
|
||||
{
|
||||
arrayAllocator(); // ensure arrayAllocator() statics are initialized before main().
|
||||
DummyArrayAllocatorInitializer() {
|
||||
arrayAllocator(); // ensure arrayAllocator() statics are initialized before
|
||||
// main().
|
||||
}
|
||||
} dummyArrayAllocatorInitializer;
|
||||
|
||||
// //////////////////////////////////////////////////////////////////
|
||||
// class ValueInternalArray
|
||||
// //////////////////////////////////////////////////////////////////
|
||||
bool
|
||||
ValueInternalArray::equals( const IteratorState &x,
|
||||
const IteratorState &other )
|
||||
{
|
||||
return x.array_ == other.array_
|
||||
&& x.currentItemIndex_ == other.currentItemIndex_
|
||||
&& x.currentPageIndex_ == other.currentPageIndex_;
|
||||
bool ValueInternalArray::equals(const IteratorState& x,
|
||||
const IteratorState& other) {
|
||||
return x.array_ == other.array_ &&
|
||||
x.currentItemIndex_ == other.currentItemIndex_ &&
|
||||
x.currentPageIndex_ == other.currentPageIndex_;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ValueInternalArray::increment( IteratorState &it )
|
||||
{
|
||||
JSON_ASSERT_MESSAGE( it.array_ &&
|
||||
(it.currentPageIndex_ - it.array_->pages_)*itemsPerPage + it.currentItemIndex_
|
||||
!= it.array_->size_,
|
||||
void ValueInternalArray::increment(IteratorState& it) {
|
||||
JSON_ASSERT_MESSAGE(
|
||||
it.array_ && (it.currentPageIndex_ - it.array_->pages_) * itemsPerPage +
|
||||
it.currentItemIndex_ !=
|
||||
it.array_->size_,
|
||||
"ValueInternalArray::increment(): moving iterator beyond end");
|
||||
++(it.currentItemIndex_);
|
||||
if ( it.currentItemIndex_ == itemsPerPage )
|
||||
{
|
||||
if (it.currentItemIndex_ == itemsPerPage) {
|
||||
it.currentItemIndex_ = 0;
|
||||
++(it.currentPageIndex_);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ValueInternalArray::decrement( IteratorState &it )
|
||||
{
|
||||
JSON_ASSERT_MESSAGE( it.array_ && it.currentPageIndex_ == it.array_->pages_
|
||||
&& it.currentItemIndex_ == 0,
|
||||
void ValueInternalArray::decrement(IteratorState& it) {
|
||||
JSON_ASSERT_MESSAGE(
|
||||
it.array_ && it.currentPageIndex_ == it.array_->pages_ &&
|
||||
it.currentItemIndex_ == 0,
|
||||
"ValueInternalArray::decrement(): moving iterator beyond end");
|
||||
if ( it.currentItemIndex_ == 0 )
|
||||
{
|
||||
if (it.currentItemIndex_ == 0) {
|
||||
it.currentItemIndex_ = itemsPerPage - 1;
|
||||
--(it.currentPageIndex_);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
--(it.currentItemIndex_);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Value &
|
||||
ValueInternalArray::unsafeDereference( const IteratorState &it )
|
||||
{
|
||||
Value& ValueInternalArray::unsafeDereference(const IteratorState& it) {
|
||||
return (*(it.currentPageIndex_))[it.currentItemIndex_];
|
||||
}
|
||||
|
||||
|
||||
Value &
|
||||
ValueInternalArray::dereference( const IteratorState &it )
|
||||
{
|
||||
JSON_ASSERT_MESSAGE( it.array_ &&
|
||||
(it.currentPageIndex_ - it.array_->pages_)*itemsPerPage + it.currentItemIndex_
|
||||
< it.array_->size_,
|
||||
Value& ValueInternalArray::dereference(const IteratorState& it) {
|
||||
JSON_ASSERT_MESSAGE(
|
||||
it.array_ && (it.currentPageIndex_ - it.array_->pages_) * itemsPerPage +
|
||||
it.currentItemIndex_ <
|
||||
it.array_->size_,
|
||||
"ValueInternalArray::dereference(): dereferencing invalid iterator");
|
||||
return unsafeDereference(it);
|
||||
}
|
||||
|
||||
void
|
||||
ValueInternalArray::makeBeginIterator( IteratorState &it ) const
|
||||
{
|
||||
void ValueInternalArray::makeBeginIterator(IteratorState& it) const {
|
||||
it.array_ = const_cast<ValueInternalArray*>(this);
|
||||
it.currentItemIndex_ = 0;
|
||||
it.currentPageIndex_ = pages_;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ValueInternalArray::makeIterator( IteratorState &it, ArrayIndex index ) const
|
||||
{
|
||||
void ValueInternalArray::makeIterator(IteratorState& it,
|
||||
ArrayIndex index) const {
|
||||
it.array_ = const_cast<ValueInternalArray*>(this);
|
||||
it.currentItemIndex_ = index % itemsPerPage;
|
||||
it.currentPageIndex_ = pages_ + index / itemsPerPage;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ValueInternalArray::makeEndIterator( IteratorState &it ) const
|
||||
{
|
||||
void ValueInternalArray::makeEndIterator(IteratorState& it) const {
|
||||
makeIterator(it, size_);
|
||||
}
|
||||
|
||||
|
||||
ValueInternalArray::ValueInternalArray()
|
||||
: pages_( 0 )
|
||||
, size_( 0 )
|
||||
, pageCount_( 0 )
|
||||
{
|
||||
}
|
||||
|
||||
ValueInternalArray::ValueInternalArray() : pages_(0), size_(0), pageCount_(0) {}
|
||||
|
||||
ValueInternalArray::ValueInternalArray(const ValueInternalArray& other)
|
||||
: pages_( 0 )
|
||||
, size_( other.size_ )
|
||||
, pageCount_( 0 )
|
||||
{
|
||||
: pages_(0), size_(other.size_), pageCount_(0) {
|
||||
PageIndex minNewPages = other.size_ / itemsPerPage;
|
||||
arrayAllocator()->reallocateArrayPageIndex(pages_, pageCount_, minNewPages);
|
||||
JSON_ASSERT_MESSAGE(pageCount_ >= minNewPages,
|
||||
@ -266,10 +210,8 @@ ValueInternalArray::ValueInternalArray( const ValueInternalArray &other )
|
||||
IteratorState itOther;
|
||||
other.makeBeginIterator(itOther);
|
||||
Value* value;
|
||||
for ( ArrayIndex index = 0; index < size_; ++index, increment(itOther) )
|
||||
{
|
||||
if ( index % itemsPerPage == 0 )
|
||||
{
|
||||
for (ArrayIndex index = 0; index < size_; ++index, increment(itOther)) {
|
||||
if (index % itemsPerPage == 0) {
|
||||
PageIndex pageIndex = index / itemsPerPage;
|
||||
value = arrayAllocator()->allocateArrayPage();
|
||||
pages_[pageIndex] = value;
|
||||
@ -278,24 +220,18 @@ ValueInternalArray::ValueInternalArray( const ValueInternalArray &other )
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
ValueInternalArray &
|
||||
ValueInternalArray::operator=(ValueInternalArray other)
|
||||
{
|
||||
ValueInternalArray& ValueInternalArray::operator=(ValueInternalArray other) {
|
||||
swap(other);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
ValueInternalArray::~ValueInternalArray()
|
||||
{
|
||||
ValueInternalArray::~ValueInternalArray() {
|
||||
// destroy all constructed items
|
||||
IteratorState it;
|
||||
IteratorState itEnd;
|
||||
makeBeginIterator(it);
|
||||
makeEndIterator(itEnd);
|
||||
for ( ; !equals(it,itEnd); increment(it) )
|
||||
{
|
||||
for (; !equals(it, itEnd); increment(it)) {
|
||||
Value* value = &dereference(it);
|
||||
value->~Value();
|
||||
}
|
||||
@ -307,10 +243,7 @@ ValueInternalArray::~ValueInternalArray()
|
||||
arrayAllocator()->releaseArrayPageIndex(pages_, pageCount_);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ValueInternalArray::swap( ValueInternalArray &other )
|
||||
{
|
||||
void ValueInternalArray::swap(ValueInternalArray& other) {
|
||||
Value** tempPages = pages_;
|
||||
pages_ = other.pages_;
|
||||
other.pages_ = tempPages;
|
||||
@ -322,27 +255,20 @@ ValueInternalArray::swap( ValueInternalArray &other )
|
||||
other.pageCount_ = tempPageCount;
|
||||
}
|
||||
|
||||
void
|
||||
ValueInternalArray::clear()
|
||||
{
|
||||
void ValueInternalArray::clear() {
|
||||
ValueInternalArray dummy;
|
||||
swap(dummy);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ValueInternalArray::resize( ArrayIndex newSize )
|
||||
{
|
||||
void ValueInternalArray::resize(ArrayIndex newSize) {
|
||||
if (newSize == 0)
|
||||
clear();
|
||||
else if ( newSize < size_ )
|
||||
{
|
||||
else if (newSize < size_) {
|
||||
IteratorState it;
|
||||
IteratorState itEnd;
|
||||
makeIterator(it, newSize);
|
||||
makeIterator(itEnd, size_);
|
||||
for ( ; !equals(it,itEnd); increment(it) )
|
||||
{
|
||||
for (; !equals(it, itEnd); increment(it)) {
|
||||
Value* value = &dereference(it);
|
||||
value->~Value();
|
||||
}
|
||||
@ -351,29 +277,24 @@ ValueInternalArray::resize( ArrayIndex newSize )
|
||||
for (; pageIndex < lastPageIndex; ++pageIndex)
|
||||
arrayAllocator()->releaseArrayPage(pages_[pageIndex]);
|
||||
size_ = newSize;
|
||||
}
|
||||
else if ( newSize > size_ )
|
||||
} else if (newSize > size_)
|
||||
resolveReference(newSize);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ValueInternalArray::makeIndexValid( ArrayIndex index )
|
||||
{
|
||||
void ValueInternalArray::makeIndexValid(ArrayIndex index) {
|
||||
// Need to enlarge page index ?
|
||||
if ( index >= pageCount_ * itemsPerPage )
|
||||
{
|
||||
if (index >= pageCount_ * itemsPerPage) {
|
||||
PageIndex minNewPages = (index + 1) / itemsPerPage;
|
||||
arrayAllocator()->reallocateArrayPageIndex(pages_, pageCount_, minNewPages);
|
||||
JSON_ASSERT_MESSAGE( pageCount_ >= minNewPages, "ValueInternalArray::reserve(): bad reallocation" );
|
||||
JSON_ASSERT_MESSAGE(pageCount_ >= minNewPages,
|
||||
"ValueInternalArray::reserve(): bad reallocation");
|
||||
}
|
||||
|
||||
// Need to allocate new pages ?
|
||||
ArrayIndex nextPageIndex =
|
||||
(size_ % itemsPerPage) != 0 ? size_ - (size_%itemsPerPage) + itemsPerPage
|
||||
ArrayIndex nextPageIndex = (size_ % itemsPerPage) != 0
|
||||
? size_ - (size_ % itemsPerPage) + itemsPerPage
|
||||
: size_;
|
||||
if ( nextPageIndex <= index )
|
||||
{
|
||||
if (nextPageIndex <= index) {
|
||||
PageIndex pageIndex = nextPageIndex / itemsPerPage;
|
||||
PageIndex pageToAllocate = (index - nextPageIndex) / itemsPerPage + 1;
|
||||
for (; pageToAllocate-- > 0; ++pageIndex)
|
||||
@ -386,62 +307,48 @@ ValueInternalArray::makeIndexValid( ArrayIndex index )
|
||||
makeIterator(it, size_);
|
||||
size_ = index + 1;
|
||||
makeIterator(itEnd, size_);
|
||||
for ( ; !equals(it,itEnd); increment(it) )
|
||||
{
|
||||
for (; !equals(it, itEnd); increment(it)) {
|
||||
Value* value = &dereference(it);
|
||||
new (value) Value(); // Construct a default value using placement new
|
||||
}
|
||||
}
|
||||
|
||||
Value &
|
||||
ValueInternalArray::resolveReference( ArrayIndex index )
|
||||
{
|
||||
Value& ValueInternalArray::resolveReference(ArrayIndex index) {
|
||||
if (index >= size_)
|
||||
makeIndexValid(index);
|
||||
return pages_[index / itemsPerPage][index % itemsPerPage];
|
||||
}
|
||||
|
||||
Value *
|
||||
ValueInternalArray::find( ArrayIndex index ) const
|
||||
{
|
||||
Value* ValueInternalArray::find(ArrayIndex index) const {
|
||||
if (index >= size_)
|
||||
return 0;
|
||||
return &(pages_[index / itemsPerPage][index % itemsPerPage]);
|
||||
}
|
||||
|
||||
ValueInternalArray::ArrayIndex
|
||||
ValueInternalArray::size() const
|
||||
{
|
||||
ValueInternalArray::ArrayIndex ValueInternalArray::size() const {
|
||||
return size_;
|
||||
}
|
||||
|
||||
int
|
||||
ValueInternalArray::distance( const IteratorState &x, const IteratorState &y )
|
||||
{
|
||||
int ValueInternalArray::distance(const IteratorState& x,
|
||||
const IteratorState& y) {
|
||||
return indexOf(y) - indexOf(x);
|
||||
}
|
||||
|
||||
|
||||
ValueInternalArray::ArrayIndex
|
||||
ValueInternalArray::indexOf( const IteratorState &iterator )
|
||||
{
|
||||
ValueInternalArray::indexOf(const IteratorState& iterator) {
|
||||
if (!iterator.array_)
|
||||
return ArrayIndex(-1);
|
||||
return ArrayIndex(
|
||||
(iterator.currentPageIndex_ - iterator.array_->pages_) * itemsPerPage
|
||||
+ iterator.currentItemIndex_ );
|
||||
return ArrayIndex((iterator.currentPageIndex_ - iterator.array_->pages_) *
|
||||
itemsPerPage +
|
||||
iterator.currentItemIndex_);
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
ValueInternalArray::compare( const ValueInternalArray &other ) const
|
||||
{
|
||||
int ValueInternalArray::compare(const ValueInternalArray& other) const {
|
||||
int sizeDiff(size_ - other.size_);
|
||||
if (sizeDiff != 0)
|
||||
return sizeDiff;
|
||||
|
||||
for ( ArrayIndex index =0; index < size_; ++index )
|
||||
{
|
||||
for (ArrayIndex index = 0; index < size_; ++index) {
|
||||
int diff = pages_[index / itemsPerPage][index % itemsPerPage].compare(
|
||||
other.pages_[index / itemsPerPage][index % itemsPerPage]);
|
||||
if (diff != 0)
|
||||
|
@ -15,146 +15,106 @@ namespace Json {
|
||||
// //////////////////////////////////////////////////////////////////
|
||||
// //////////////////////////////////////////////////////////////////
|
||||
|
||||
/** \internal MUST be safely initialized using memset( this, 0, sizeof(ValueInternalLink) );
|
||||
/** \internal MUST be safely initialized using memset( this, 0,
|
||||
* sizeof(ValueInternalLink) );
|
||||
* This optimization is used by the fast allocator.
|
||||
*/
|
||||
ValueInternalLink::ValueInternalLink()
|
||||
: previous_( 0 )
|
||||
, next_( 0 )
|
||||
{
|
||||
}
|
||||
ValueInternalLink::ValueInternalLink() : previous_(0), next_(0) {}
|
||||
|
||||
ValueInternalLink::~ValueInternalLink()
|
||||
{
|
||||
for ( int index =0; index < itemPerLink; ++index )
|
||||
{
|
||||
if ( !items_[index].isItemAvailable() )
|
||||
{
|
||||
ValueInternalLink::~ValueInternalLink() {
|
||||
for (int index = 0; index < itemPerLink; ++index) {
|
||||
if (!items_[index].isItemAvailable()) {
|
||||
if (!items_[index].isMemberNameStatic())
|
||||
free(keys_[index]);
|
||||
}
|
||||
else
|
||||
} else
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
ValueMapAllocator::~ValueMapAllocator()
|
||||
{
|
||||
}
|
||||
ValueMapAllocator::~ValueMapAllocator() {}
|
||||
|
||||
#ifdef JSON_USE_SIMPLE_INTERNAL_ALLOCATOR
|
||||
class DefaultValueMapAllocator : public ValueMapAllocator
|
||||
{
|
||||
class DefaultValueMapAllocator : public ValueMapAllocator {
|
||||
public: // overridden from ValueMapAllocator
|
||||
virtual ValueInternalMap *newMap()
|
||||
{
|
||||
return new ValueInternalMap();
|
||||
}
|
||||
virtual ValueInternalMap* newMap() { return new ValueInternalMap(); }
|
||||
|
||||
virtual ValueInternalMap *newMapCopy( const ValueInternalMap &other )
|
||||
{
|
||||
virtual ValueInternalMap* newMapCopy(const ValueInternalMap& other) {
|
||||
return new ValueInternalMap(other);
|
||||
}
|
||||
|
||||
virtual void destructMap( ValueInternalMap *map )
|
||||
{
|
||||
delete map;
|
||||
}
|
||||
virtual void destructMap(ValueInternalMap* map) { delete map; }
|
||||
|
||||
virtual ValueInternalLink *allocateMapBuckets( unsigned int size )
|
||||
{
|
||||
virtual ValueInternalLink* allocateMapBuckets(unsigned int size) {
|
||||
return new ValueInternalLink[size];
|
||||
}
|
||||
|
||||
virtual void releaseMapBuckets( ValueInternalLink *links )
|
||||
{
|
||||
delete [] links;
|
||||
}
|
||||
virtual void releaseMapBuckets(ValueInternalLink* links) { delete[] links; }
|
||||
|
||||
virtual ValueInternalLink *allocateMapLink()
|
||||
{
|
||||
virtual ValueInternalLink* allocateMapLink() {
|
||||
return new ValueInternalLink();
|
||||
}
|
||||
|
||||
virtual void releaseMapLink( ValueInternalLink *link )
|
||||
{
|
||||
delete link;
|
||||
}
|
||||
virtual void releaseMapLink(ValueInternalLink* link) { delete link; }
|
||||
};
|
||||
#else
|
||||
/// @todo make this thread-safe (lock when accessign batch allocator)
|
||||
class DefaultValueMapAllocator : public ValueMapAllocator
|
||||
{
|
||||
class DefaultValueMapAllocator : public ValueMapAllocator {
|
||||
public: // overridden from ValueMapAllocator
|
||||
virtual ValueInternalMap *newMap()
|
||||
{
|
||||
virtual ValueInternalMap* newMap() {
|
||||
ValueInternalMap* map = mapsAllocator_.allocate();
|
||||
new (map) ValueInternalMap(); // placement new
|
||||
return map;
|
||||
}
|
||||
|
||||
virtual ValueInternalMap *newMapCopy( const ValueInternalMap &other )
|
||||
{
|
||||
virtual ValueInternalMap* newMapCopy(const ValueInternalMap& other) {
|
||||
ValueInternalMap* map = mapsAllocator_.allocate();
|
||||
new (map) ValueInternalMap(other); // placement new
|
||||
return map;
|
||||
}
|
||||
|
||||
virtual void destructMap( ValueInternalMap *map )
|
||||
{
|
||||
if ( map )
|
||||
{
|
||||
virtual void destructMap(ValueInternalMap* map) {
|
||||
if (map) {
|
||||
map->~ValueInternalMap();
|
||||
mapsAllocator_.release(map);
|
||||
}
|
||||
}
|
||||
|
||||
virtual ValueInternalLink *allocateMapBuckets( unsigned int size )
|
||||
{
|
||||
virtual ValueInternalLink* allocateMapBuckets(unsigned int size) {
|
||||
return new ValueInternalLink[size];
|
||||
}
|
||||
|
||||
virtual void releaseMapBuckets( ValueInternalLink *links )
|
||||
{
|
||||
delete [] links;
|
||||
}
|
||||
virtual void releaseMapBuckets(ValueInternalLink* links) { delete[] links; }
|
||||
|
||||
virtual ValueInternalLink *allocateMapLink()
|
||||
{
|
||||
virtual ValueInternalLink* allocateMapLink() {
|
||||
ValueInternalLink* link = linksAllocator_.allocate();
|
||||
memset(link, 0, sizeof(ValueInternalLink));
|
||||
return link;
|
||||
}
|
||||
|
||||
virtual void releaseMapLink( ValueInternalLink *link )
|
||||
{
|
||||
virtual void releaseMapLink(ValueInternalLink* link) {
|
||||
link->~ValueInternalLink();
|
||||
linksAllocator_.release(link);
|
||||
}
|
||||
|
||||
private:
|
||||
BatchAllocator<ValueInternalMap, 1> mapsAllocator_;
|
||||
BatchAllocator<ValueInternalLink, 1> linksAllocator_;
|
||||
};
|
||||
#endif
|
||||
|
||||
static ValueMapAllocator *&mapAllocator()
|
||||
{
|
||||
static ValueMapAllocator*& mapAllocator() {
|
||||
static DefaultValueMapAllocator defaultAllocator;
|
||||
static ValueMapAllocator* mapAllocator = &defaultAllocator;
|
||||
return mapAllocator;
|
||||
}
|
||||
|
||||
static struct DummyMapAllocatorInitializer {
|
||||
DummyMapAllocatorInitializer()
|
||||
{
|
||||
mapAllocator(); // ensure mapAllocator() statics are initialized before main().
|
||||
DummyMapAllocatorInitializer() {
|
||||
mapAllocator(); // ensure mapAllocator() statics are initialized before
|
||||
// main().
|
||||
}
|
||||
} dummyMapAllocatorInitializer;
|
||||
|
||||
|
||||
|
||||
// h(K) = value * K >> w ; with w = 32 & K prime w.r.t. 2^32.
|
||||
|
||||
/*
|
||||
@ -164,29 +124,17 @@ linked list element contains 6 key/values. (memory = (16+4) * 6 + 4 = 124)
|
||||
value have extra state: valid, available, deleted
|
||||
*/
|
||||
|
||||
|
||||
ValueInternalMap::ValueInternalMap()
|
||||
: buckets_( 0 )
|
||||
, tailLink_( 0 )
|
||||
, bucketsSize_( 0 )
|
||||
, itemCount_( 0 )
|
||||
{
|
||||
}
|
||||
|
||||
: buckets_(0), tailLink_(0), bucketsSize_(0), itemCount_(0) {}
|
||||
|
||||
ValueInternalMap::ValueInternalMap(const ValueInternalMap& other)
|
||||
: buckets_( 0 )
|
||||
, tailLink_( 0 )
|
||||
, bucketsSize_( 0 )
|
||||
, itemCount_( 0 )
|
||||
{
|
||||
: buckets_(0), tailLink_(0), bucketsSize_(0), itemCount_(0) {
|
||||
reserve(other.itemCount_);
|
||||
IteratorState it;
|
||||
IteratorState itEnd;
|
||||
other.makeBeginIterator(it);
|
||||
other.makeEndIterator(itEnd);
|
||||
for ( ; !equals(it,itEnd); increment(it) )
|
||||
{
|
||||
for (; !equals(it, itEnd); increment(it)) {
|
||||
bool isStatic;
|
||||
const char* memberName = key(it, isStatic);
|
||||
const Value& aValue = value(it);
|
||||
@ -194,24 +142,17 @@ ValueInternalMap::ValueInternalMap( const ValueInternalMap &other )
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
ValueInternalMap &
|
||||
ValueInternalMap::operator=(ValueInternalMap other)
|
||||
{
|
||||
ValueInternalMap& ValueInternalMap::operator=(ValueInternalMap other) {
|
||||
swap(other);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
ValueInternalMap::~ValueInternalMap()
|
||||
{
|
||||
if ( buckets_ )
|
||||
{
|
||||
for ( BucketIndex bucketIndex =0; bucketIndex < bucketsSize_; ++bucketIndex )
|
||||
{
|
||||
ValueInternalMap::~ValueInternalMap() {
|
||||
if (buckets_) {
|
||||
for (BucketIndex bucketIndex = 0; bucketIndex < bucketsSize_;
|
||||
++bucketIndex) {
|
||||
ValueInternalLink* link = buckets_[bucketIndex].next_;
|
||||
while ( link )
|
||||
{
|
||||
while (link) {
|
||||
ValueInternalLink* linkToRelease = link;
|
||||
link = link->next_;
|
||||
mapAllocator()->releaseMapLink(linkToRelease);
|
||||
@ -221,10 +162,7 @@ ValueInternalMap::~ValueInternalMap()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ValueInternalMap::swap( ValueInternalMap &other )
|
||||
{
|
||||
void ValueInternalMap::swap(ValueInternalMap& other) {
|
||||
ValueInternalLink* tempBuckets = buckets_;
|
||||
buckets_ = other.buckets_;
|
||||
other.buckets_ = tempBuckets;
|
||||
@ -239,54 +177,39 @@ ValueInternalMap::swap( ValueInternalMap &other )
|
||||
other.itemCount_ = tempItemCount;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ValueInternalMap::clear()
|
||||
{
|
||||
void ValueInternalMap::clear() {
|
||||
ValueInternalMap dummy;
|
||||
swap(dummy);
|
||||
}
|
||||
|
||||
|
||||
ValueInternalMap::BucketIndex
|
||||
ValueInternalMap::size() const
|
||||
{
|
||||
ValueInternalMap::BucketIndex ValueInternalMap::size() const {
|
||||
return itemCount_;
|
||||
}
|
||||
|
||||
bool
|
||||
ValueInternalMap::reserveDelta( BucketIndex growth )
|
||||
{
|
||||
bool ValueInternalMap::reserveDelta(BucketIndex growth) {
|
||||
return reserve(itemCount_ + growth);
|
||||
}
|
||||
|
||||
bool
|
||||
ValueInternalMap::reserve( BucketIndex newItemCount )
|
||||
{
|
||||
if ( !buckets_ && newItemCount > 0 )
|
||||
{
|
||||
bool ValueInternalMap::reserve(BucketIndex newItemCount) {
|
||||
if (!buckets_ && newItemCount > 0) {
|
||||
buckets_ = mapAllocator()->allocateMapBuckets(1);
|
||||
bucketsSize_ = 1;
|
||||
tailLink_ = &buckets_[0];
|
||||
}
|
||||
// BucketIndex idealBucketCount = (newItemCount + ValueInternalLink::itemPerLink) / ValueInternalLink::itemPerLink;
|
||||
// BucketIndex idealBucketCount = (newItemCount +
|
||||
// ValueInternalLink::itemPerLink) / ValueInternalLink::itemPerLink;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
const Value *
|
||||
ValueInternalMap::find( const char *key ) const
|
||||
{
|
||||
const Value* ValueInternalMap::find(const char* key) const {
|
||||
if (!bucketsSize_)
|
||||
return 0;
|
||||
HashKey hashedKey = hash(key);
|
||||
BucketIndex bucketIndex = hashedKey % bucketsSize_;
|
||||
for ( const ValueInternalLink *current = &buckets_[bucketIndex];
|
||||
current != 0;
|
||||
current = current->next_ )
|
||||
{
|
||||
for ( BucketIndex index=0; index < ValueInternalLink::itemPerLink; ++index )
|
||||
{
|
||||
for (const ValueInternalLink* current = &buckets_[bucketIndex]; current != 0;
|
||||
current = current->next_) {
|
||||
for (BucketIndex index = 0; index < ValueInternalLink::itemPerLink;
|
||||
++index) {
|
||||
if (current->items_[index].isItemAvailable())
|
||||
return 0;
|
||||
if (strcmp(key, current->keys_[index]) == 0)
|
||||
@ -296,31 +219,20 @@ ValueInternalMap::find( const char *key ) const
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Value *
|
||||
ValueInternalMap::find( const char *key )
|
||||
{
|
||||
Value* ValueInternalMap::find(const char* key) {
|
||||
const ValueInternalMap* constThis = this;
|
||||
return const_cast<Value*>(constThis->find(key));
|
||||
}
|
||||
|
||||
|
||||
Value &
|
||||
ValueInternalMap::resolveReference( const char *key,
|
||||
bool isStatic )
|
||||
{
|
||||
Value& ValueInternalMap::resolveReference(const char* key, bool isStatic) {
|
||||
HashKey hashedKey = hash(key);
|
||||
if ( bucketsSize_ )
|
||||
{
|
||||
if (bucketsSize_) {
|
||||
BucketIndex bucketIndex = hashedKey % bucketsSize_;
|
||||
ValueInternalLink** previous = 0;
|
||||
BucketIndex index;
|
||||
for ( ValueInternalLink *current = &buckets_[bucketIndex];
|
||||
current != 0;
|
||||
previous = ¤t->next_, current = current->next_ )
|
||||
{
|
||||
for ( index=0; index < ValueInternalLink::itemPerLink; ++index )
|
||||
{
|
||||
for (ValueInternalLink* current = &buckets_[bucketIndex]; current != 0;
|
||||
previous = ¤t->next_, current = current->next_) {
|
||||
for (index = 0; index < ValueInternalLink::itemPerLink; ++index) {
|
||||
if (current->items_[index].isItemAvailable())
|
||||
return setNewItem(key, isStatic, current, index);
|
||||
if (strcmp(key, current->keys_[index]) == 0)
|
||||
@ -333,25 +245,18 @@ ValueInternalMap::resolveReference( const char *key,
|
||||
return unsafeAdd(key, isStatic, hashedKey);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ValueInternalMap::remove( const char *key )
|
||||
{
|
||||
void ValueInternalMap::remove(const char* key) {
|
||||
HashKey hashedKey = hash(key);
|
||||
if (!bucketsSize_)
|
||||
return;
|
||||
BucketIndex bucketIndex = hashedKey % bucketsSize_;
|
||||
for ( ValueInternalLink *link = &buckets_[bucketIndex];
|
||||
link != 0;
|
||||
link = link->next_ )
|
||||
{
|
||||
for (ValueInternalLink* link = &buckets_[bucketIndex]; link != 0;
|
||||
link = link->next_) {
|
||||
BucketIndex index;
|
||||
for ( index =0; index < ValueInternalLink::itemPerLink; ++index )
|
||||
{
|
||||
for (index = 0; index < ValueInternalLink::itemPerLink; ++index) {
|
||||
if (link->items_[index].isItemAvailable())
|
||||
return;
|
||||
if ( strcmp( key, link->keys_[index] ) == 0 )
|
||||
{
|
||||
if (strcmp(key, link->keys_[index]) == 0) {
|
||||
doActualRemove(link, index, bucketIndex);
|
||||
return;
|
||||
}
|
||||
@ -359,18 +264,16 @@ ValueInternalMap::remove( const char *key )
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
ValueInternalMap::doActualRemove( ValueInternalLink *link,
|
||||
void ValueInternalMap::doActualRemove(ValueInternalLink* link,
|
||||
BucketIndex index,
|
||||
BucketIndex bucketIndex )
|
||||
{
|
||||
BucketIndex bucketIndex) {
|
||||
// find last item of the bucket and swap it with the 'removed' one.
|
||||
// set removed items flags to 'available'.
|
||||
// if last page only contains 'available' items, then desallocate it (it's empty)
|
||||
// if last page only contains 'available' items, then desallocate it (it's
|
||||
// empty)
|
||||
ValueInternalLink*& lastLink = getLastLinkInBucket(index);
|
||||
BucketIndex lastItemIndex = 1; // a link can never be empty, so start at 1
|
||||
for ( ;
|
||||
lastItemIndex < ValueInternalLink::itemPerLink;
|
||||
for (; lastItemIndex < ValueInternalLink::itemPerLink;
|
||||
++lastItemIndex) // may be optimized with dicotomic search
|
||||
{
|
||||
if (lastLink->items_[lastItemIndex].isItemAvailable())
|
||||
@ -391,9 +294,7 @@ ValueInternalMap::doActualRemove( ValueInternalLink *link,
|
||||
linkPreviousToLast->next_ = 0;
|
||||
lastLink = linkPreviousToLast;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
Value dummy;
|
||||
valueToPreserve->swap(dummy); // restore deleted to default Value.
|
||||
valueToPreserve->setItemUsed(false);
|
||||
@ -401,10 +302,8 @@ ValueInternalMap::doActualRemove( ValueInternalLink *link,
|
||||
--itemCount_;
|
||||
}
|
||||
|
||||
|
||||
ValueInternalLink*&
|
||||
ValueInternalMap::getLastLinkInBucket( BucketIndex bucketIndex )
|
||||
{
|
||||
ValueInternalMap::getLastLinkInBucket(BucketIndex bucketIndex) {
|
||||
if (bucketIndex == bucketsSize_ - 1)
|
||||
return tailLink_;
|
||||
ValueInternalLink*& previous = buckets_[bucketIndex + 1].previous_;
|
||||
@ -413,13 +312,10 @@ ValueInternalMap::getLastLinkInBucket( BucketIndex bucketIndex )
|
||||
return previous;
|
||||
}
|
||||
|
||||
|
||||
Value &
|
||||
ValueInternalMap::setNewItem( const char *key,
|
||||
Value& ValueInternalMap::setNewItem(const char* key,
|
||||
bool isStatic,
|
||||
ValueInternalLink* link,
|
||||
BucketIndex index )
|
||||
{
|
||||
BucketIndex index) {
|
||||
char* duplicatedKey = makeMemberName(key);
|
||||
++itemCount_;
|
||||
link->keys_[index] = duplicatedKey;
|
||||
@ -428,19 +324,15 @@ ValueInternalMap::setNewItem( const char *key,
|
||||
return link->items_[index]; // items already default constructed.
|
||||
}
|
||||
|
||||
|
||||
Value&
|
||||
ValueInternalMap::unsafeAdd( const char *key,
|
||||
bool isStatic,
|
||||
HashKey hashedKey )
|
||||
{
|
||||
JSON_ASSERT_MESSAGE( bucketsSize_ > 0, "ValueInternalMap::unsafeAdd(): internal logic error." );
|
||||
ValueInternalMap::unsafeAdd(const char* key, bool isStatic, HashKey hashedKey) {
|
||||
JSON_ASSERT_MESSAGE(bucketsSize_ > 0,
|
||||
"ValueInternalMap::unsafeAdd(): internal logic error.");
|
||||
BucketIndex bucketIndex = hashedKey % bucketsSize_;
|
||||
ValueInternalLink*& previousLink = getLastLinkInBucket(bucketIndex);
|
||||
ValueInternalLink* link = previousLink;
|
||||
BucketIndex index;
|
||||
for ( index =0; index < ValueInternalLink::itemPerLink; ++index )
|
||||
{
|
||||
for (index = 0; index < ValueInternalLink::itemPerLink; ++index) {
|
||||
if (link->items_[index].isItemAvailable())
|
||||
break;
|
||||
}
|
||||
@ -455,38 +347,31 @@ ValueInternalMap::unsafeAdd( const char *key,
|
||||
return setNewItem(key, isStatic, link, index);
|
||||
}
|
||||
|
||||
|
||||
ValueInternalMap::HashKey
|
||||
ValueInternalMap::hash( const char *key ) const
|
||||
{
|
||||
ValueInternalMap::HashKey ValueInternalMap::hash(const char* key) const {
|
||||
HashKey hash = 0;
|
||||
while (*key)
|
||||
hash += *key++ * 37;
|
||||
return hash;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
ValueInternalMap::compare( const ValueInternalMap &other ) const
|
||||
{
|
||||
int ValueInternalMap::compare(const ValueInternalMap& other) const {
|
||||
int sizeDiff(itemCount_ - other.itemCount_);
|
||||
if (sizeDiff != 0)
|
||||
return sizeDiff;
|
||||
// Strict order guaranty is required. Compare all keys FIRST, then compare values.
|
||||
// Strict order guaranty is required. Compare all keys FIRST, then compare
|
||||
// values.
|
||||
IteratorState it;
|
||||
IteratorState itEnd;
|
||||
makeBeginIterator(it);
|
||||
makeEndIterator(itEnd);
|
||||
for ( ; !equals(it,itEnd); increment(it) )
|
||||
{
|
||||
for (; !equals(it, itEnd); increment(it)) {
|
||||
if (!other.find(key(it)))
|
||||
return 1;
|
||||
}
|
||||
|
||||
// All keys are equals, let's compare values
|
||||
makeBeginIterator(it);
|
||||
for ( ; !equals(it,itEnd); increment(it) )
|
||||
{
|
||||
for (; !equals(it, itEnd); increment(it)) {
|
||||
const Value* otherValue = other.find(key(it));
|
||||
int valueDiff = value(it).compare(*otherValue);
|
||||
if (valueDiff != 0)
|
||||
@ -495,42 +380,30 @@ ValueInternalMap::compare( const ValueInternalMap &other ) const
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ValueInternalMap::makeBeginIterator( IteratorState &it ) const
|
||||
{
|
||||
void ValueInternalMap::makeBeginIterator(IteratorState& it) const {
|
||||
it.map_ = const_cast<ValueInternalMap*>(this);
|
||||
it.bucketIndex_ = 0;
|
||||
it.itemIndex_ = 0;
|
||||
it.link_ = buckets_;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ValueInternalMap::makeEndIterator( IteratorState &it ) const
|
||||
{
|
||||
void ValueInternalMap::makeEndIterator(IteratorState& it) const {
|
||||
it.map_ = const_cast<ValueInternalMap*>(this);
|
||||
it.bucketIndex_ = bucketsSize_;
|
||||
it.itemIndex_ = 0;
|
||||
it.link_ = 0;
|
||||
}
|
||||
|
||||
|
||||
bool
|
||||
ValueInternalMap::equals( const IteratorState &x, const IteratorState &other )
|
||||
{
|
||||
return x.map_ == other.map_
|
||||
&& x.bucketIndex_ == other.bucketIndex_
|
||||
&& x.link_ == other.link_
|
||||
&& x.itemIndex_ == other.itemIndex_;
|
||||
bool ValueInternalMap::equals(const IteratorState& x,
|
||||
const IteratorState& other) {
|
||||
return x.map_ == other.map_ && x.bucketIndex_ == other.bucketIndex_ &&
|
||||
x.link_ == other.link_ && x.itemIndex_ == other.itemIndex_;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ValueInternalMap::incrementBucket( IteratorState &iterator )
|
||||
{
|
||||
void ValueInternalMap::incrementBucket(IteratorState& iterator) {
|
||||
++iterator.bucketIndex_;
|
||||
JSON_ASSERT_MESSAGE( iterator.bucketIndex_ <= iterator.map_->bucketsSize_,
|
||||
JSON_ASSERT_MESSAGE(
|
||||
iterator.bucketIndex_ <= iterator.map_->bucketsSize_,
|
||||
"ValueInternalMap::increment(): attempting to iterate beyond end.");
|
||||
if (iterator.bucketIndex_ == iterator.map_->bucketsSize_)
|
||||
iterator.link_ = 0;
|
||||
@ -539,36 +412,29 @@ ValueInternalMap::incrementBucket( IteratorState &iterator )
|
||||
iterator.itemIndex_ = 0;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ValueInternalMap::increment( IteratorState &iterator )
|
||||
{
|
||||
JSON_ASSERT_MESSAGE( iterator.map_, "Attempting to iterator using invalid iterator." );
|
||||
void ValueInternalMap::increment(IteratorState& iterator) {
|
||||
JSON_ASSERT_MESSAGE(iterator.map_,
|
||||
"Attempting to iterator using invalid iterator.");
|
||||
++iterator.itemIndex_;
|
||||
if ( iterator.itemIndex_ == ValueInternalLink::itemPerLink )
|
||||
{
|
||||
JSON_ASSERT_MESSAGE( iterator.link_ != 0,
|
||||
if (iterator.itemIndex_ == ValueInternalLink::itemPerLink) {
|
||||
JSON_ASSERT_MESSAGE(
|
||||
iterator.link_ != 0,
|
||||
"ValueInternalMap::increment(): attempting to iterate beyond end.");
|
||||
iterator.link_ = iterator.link_->next_;
|
||||
if (iterator.link_ == 0)
|
||||
incrementBucket(iterator);
|
||||
}
|
||||
else if ( iterator.link_->items_[iterator.itemIndex_].isItemAvailable() )
|
||||
{
|
||||
} else if (iterator.link_->items_[iterator.itemIndex_].isItemAvailable()) {
|
||||
incrementBucket(iterator);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ValueInternalMap::decrement( IteratorState &iterator )
|
||||
{
|
||||
if ( iterator.itemIndex_ == 0 )
|
||||
{
|
||||
JSON_ASSERT_MESSAGE( iterator.map_, "Attempting to iterate using invalid iterator." );
|
||||
if ( iterator.link_ == &iterator.map_->buckets_[iterator.bucketIndex_] )
|
||||
{
|
||||
JSON_ASSERT_MESSAGE( iterator.bucketIndex_ > 0, "Attempting to iterate beyond beginning." );
|
||||
void ValueInternalMap::decrement(IteratorState& iterator) {
|
||||
if (iterator.itemIndex_ == 0) {
|
||||
JSON_ASSERT_MESSAGE(iterator.map_,
|
||||
"Attempting to iterate using invalid iterator.");
|
||||
if (iterator.link_ == &iterator.map_->buckets_[iterator.bucketIndex_]) {
|
||||
JSON_ASSERT_MESSAGE(iterator.bucketIndex_ > 0,
|
||||
"Attempting to iterate beyond beginning.");
|
||||
--(iterator.bucketIndex_);
|
||||
}
|
||||
iterator.link_ = iterator.link_->previous_;
|
||||
@ -576,34 +442,27 @@ ValueInternalMap::decrement( IteratorState &iterator )
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const char *
|
||||
ValueInternalMap::key( const IteratorState &iterator )
|
||||
{
|
||||
JSON_ASSERT_MESSAGE( iterator.link_, "Attempting to iterate using invalid iterator." );
|
||||
const char* ValueInternalMap::key(const IteratorState& iterator) {
|
||||
JSON_ASSERT_MESSAGE(iterator.link_,
|
||||
"Attempting to iterate using invalid iterator.");
|
||||
return iterator.link_->keys_[iterator.itemIndex_];
|
||||
}
|
||||
|
||||
const char *
|
||||
ValueInternalMap::key( const IteratorState &iterator, bool &isStatic )
|
||||
{
|
||||
JSON_ASSERT_MESSAGE( iterator.link_, "Attempting to iterate using invalid iterator." );
|
||||
const char* ValueInternalMap::key(const IteratorState& iterator,
|
||||
bool& isStatic) {
|
||||
JSON_ASSERT_MESSAGE(iterator.link_,
|
||||
"Attempting to iterate using invalid iterator.");
|
||||
isStatic = iterator.link_->items_[iterator.itemIndex_].isMemberNameStatic();
|
||||
return iterator.link_->keys_[iterator.itemIndex_];
|
||||
}
|
||||
|
||||
|
||||
Value &
|
||||
ValueInternalMap::value( const IteratorState &iterator )
|
||||
{
|
||||
JSON_ASSERT_MESSAGE( iterator.link_, "Attempting to iterate using invalid iterator." );
|
||||
Value& ValueInternalMap::value(const IteratorState& iterator) {
|
||||
JSON_ASSERT_MESSAGE(iterator.link_,
|
||||
"Attempting to iterate using invalid iterator.");
|
||||
return iterator.link_->items_[iterator.itemIndex_];
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
ValueInternalMap::distance( const IteratorState &x, const IteratorState &y )
|
||||
{
|
||||
int ValueInternalMap::distance(const IteratorState& x, const IteratorState& y) {
|
||||
int offset = 0;
|
||||
IteratorState it = x;
|
||||
while (!equals(it, y))
|
||||
|
@ -17,43 +17,33 @@ namespace Json {
|
||||
|
||||
ValueIteratorBase::ValueIteratorBase()
|
||||
#ifndef JSON_VALUE_USE_INTERNAL_MAP
|
||||
: current_()
|
||||
, isNull_( true )
|
||||
{
|
||||
: current_(), isNull_(true) {
|
||||
}
|
||||
#else
|
||||
: isArray_( true )
|
||||
, isNull_( true )
|
||||
{
|
||||
: isArray_(true), isNull_(true) {
|
||||
iterator_.array_ = ValueInternalArray::IteratorState();
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef JSON_VALUE_USE_INTERNAL_MAP
|
||||
ValueIteratorBase::ValueIteratorBase( const Value::ObjectValues::iterator ¤t )
|
||||
: current_( current )
|
||||
, isNull_( false )
|
||||
{
|
||||
}
|
||||
ValueIteratorBase::ValueIteratorBase(
|
||||
const Value::ObjectValues::iterator& current)
|
||||
: current_(current), isNull_(false) {}
|
||||
#else
|
||||
ValueIteratorBase::ValueIteratorBase( const ValueInternalArray::IteratorState &state )
|
||||
: isArray_( true )
|
||||
{
|
||||
ValueIteratorBase::ValueIteratorBase(
|
||||
const ValueInternalArray::IteratorState& state)
|
||||
: isArray_(true) {
|
||||
iterator_.array_ = state;
|
||||
}
|
||||
|
||||
|
||||
ValueIteratorBase::ValueIteratorBase( const ValueInternalMap::IteratorState &state )
|
||||
: isArray_( false )
|
||||
{
|
||||
ValueIteratorBase::ValueIteratorBase(
|
||||
const ValueInternalMap::IteratorState& state)
|
||||
: isArray_(false) {
|
||||
iterator_.map_ = state;
|
||||
}
|
||||
#endif
|
||||
|
||||
Value &
|
||||
ValueIteratorBase::deref() const
|
||||
{
|
||||
Value& ValueIteratorBase::deref() const {
|
||||
#ifndef JSON_VALUE_USE_INTERNAL_MAP
|
||||
return current_->second;
|
||||
#else
|
||||
@ -63,10 +53,7 @@ ValueIteratorBase::deref() const
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ValueIteratorBase::increment()
|
||||
{
|
||||
void ValueIteratorBase::increment() {
|
||||
#ifndef JSON_VALUE_USE_INTERNAL_MAP
|
||||
++current_;
|
||||
#else
|
||||
@ -76,10 +63,7 @@ ValueIteratorBase::increment()
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ValueIteratorBase::decrement()
|
||||
{
|
||||
void ValueIteratorBase::decrement() {
|
||||
#ifndef JSON_VALUE_USE_INTERNAL_MAP
|
||||
--current_;
|
||||
#else
|
||||
@ -89,10 +73,8 @@ ValueIteratorBase::decrement()
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
ValueIteratorBase::difference_type
|
||||
ValueIteratorBase::computeDistance( const SelfType &other ) const
|
||||
{
|
||||
ValueIteratorBase::computeDistance(const SelfType& other) const {
|
||||
#ifndef JSON_VALUE_USE_INTERNAL_MAP
|
||||
#ifdef JSON_USE_CPPTL_SMALLMAP
|
||||
return current_ - other.current_;
|
||||
@ -102,37 +84,33 @@ ValueIteratorBase::computeDistance( const SelfType &other ) const
|
||||
// std::map::iterator. As begin() and end() are two instance
|
||||
// of the default std::map::iterator, they can not be compared.
|
||||
// To allow this, we handle this comparison specifically.
|
||||
if ( isNull_ && other.isNull_ )
|
||||
{
|
||||
if (isNull_ && other.isNull_) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
// Usage of std::distance is not portable (does not compile with Sun Studio 12 RogueWave STL,
|
||||
// Usage of std::distance is not portable (does not compile with Sun Studio 12
|
||||
// RogueWave STL,
|
||||
// which is the one used by default).
|
||||
// Using a portable hand-made version for non random iterator instead:
|
||||
// return difference_type( std::distance( current_, other.current_ ) );
|
||||
difference_type myDistance = 0;
|
||||
for ( Value::ObjectValues::iterator it = current_; it != other.current_; ++it )
|
||||
{
|
||||
for (Value::ObjectValues::iterator it = current_; it != other.current_;
|
||||
++it) {
|
||||
++myDistance;
|
||||
}
|
||||
return myDistance;
|
||||
#endif
|
||||
#else
|
||||
if (isArray_)
|
||||
return ValueInternalArray::distance( iterator_.array_, other.iterator_.array_ );
|
||||
return ValueInternalArray::distance(iterator_.array_,
|
||||
other.iterator_.array_);
|
||||
return ValueInternalMap::distance(iterator_.map_, other.iterator_.map_);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
bool
|
||||
ValueIteratorBase::isEqual( const SelfType &other ) const
|
||||
{
|
||||
bool ValueIteratorBase::isEqual(const SelfType& other) const {
|
||||
#ifndef JSON_VALUE_USE_INTERNAL_MAP
|
||||
if ( isNull_ )
|
||||
{
|
||||
if (isNull_) {
|
||||
return other.isNull_;
|
||||
}
|
||||
return current_ == other.current_;
|
||||
@ -143,10 +121,7 @@ ValueIteratorBase::isEqual( const SelfType &other ) const
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ValueIteratorBase::copy( const SelfType &other )
|
||||
{
|
||||
void ValueIteratorBase::copy(const SelfType& other) {
|
||||
#ifndef JSON_VALUE_USE_INTERNAL_MAP
|
||||
current_ = other.current_;
|
||||
isNull_ = other.isNull_;
|
||||
@ -157,14 +132,10 @@ ValueIteratorBase::copy( const SelfType &other )
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
Value
|
||||
ValueIteratorBase::key() const
|
||||
{
|
||||
Value ValueIteratorBase::key() const {
|
||||
#ifndef JSON_VALUE_USE_INTERNAL_MAP
|
||||
const Value::CZString czstring = (*current_).first;
|
||||
if ( czstring.c_str() )
|
||||
{
|
||||
if (czstring.c_str()) {
|
||||
if (czstring.isStaticString())
|
||||
return Value(StaticString(czstring.c_str()));
|
||||
return Value(czstring.c_str());
|
||||
@ -181,10 +152,7 @@ ValueIteratorBase::key() const
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
UInt
|
||||
ValueIteratorBase::index() const
|
||||
{
|
||||
UInt ValueIteratorBase::index() const {
|
||||
#ifndef JSON_VALUE_USE_INTERNAL_MAP
|
||||
const Value::CZString czstring = (*current_).first;
|
||||
if (!czstring.c_str())
|
||||
@ -197,10 +165,7 @@ ValueIteratorBase::index() const
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
const char *
|
||||
ValueIteratorBase::memberName() const
|
||||
{
|
||||
const char* ValueIteratorBase::memberName() const {
|
||||
#ifndef JSON_VALUE_USE_INTERNAL_MAP
|
||||
const char* name = (*current_).first.c_str();
|
||||
return name ? name : "";
|
||||
@ -211,7 +176,6 @@ ValueIteratorBase::memberName() const
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
// //////////////////////////////////////////////////////////////////
|
||||
// //////////////////////////////////////////////////////////////////
|
||||
// //////////////////////////////////////////////////////////////////
|
||||
@ -220,36 +184,28 @@ ValueIteratorBase::memberName() const
|
||||
// //////////////////////////////////////////////////////////////////
|
||||
// //////////////////////////////////////////////////////////////////
|
||||
|
||||
ValueConstIterator::ValueConstIterator()
|
||||
{
|
||||
}
|
||||
|
||||
ValueConstIterator::ValueConstIterator() {}
|
||||
|
||||
#ifndef JSON_VALUE_USE_INTERNAL_MAP
|
||||
ValueConstIterator::ValueConstIterator( const Value::ObjectValues::iterator ¤t )
|
||||
: ValueIteratorBase( current )
|
||||
{
|
||||
}
|
||||
ValueConstIterator::ValueConstIterator(
|
||||
const Value::ObjectValues::iterator& current)
|
||||
: ValueIteratorBase(current) {}
|
||||
#else
|
||||
ValueConstIterator::ValueConstIterator( const ValueInternalArray::IteratorState &state )
|
||||
: ValueIteratorBase( state )
|
||||
{
|
||||
}
|
||||
ValueConstIterator::ValueConstIterator(
|
||||
const ValueInternalArray::IteratorState& state)
|
||||
: ValueIteratorBase(state) {}
|
||||
|
||||
ValueConstIterator::ValueConstIterator( const ValueInternalMap::IteratorState &state )
|
||||
: ValueIteratorBase( state )
|
||||
{
|
||||
}
|
||||
ValueConstIterator::ValueConstIterator(
|
||||
const ValueInternalMap::IteratorState& state)
|
||||
: ValueIteratorBase(state) {}
|
||||
#endif
|
||||
|
||||
ValueConstIterator &
|
||||
ValueConstIterator::operator =( const ValueIteratorBase &other )
|
||||
{
|
||||
ValueConstIterator& ValueConstIterator::
|
||||
operator=(const ValueIteratorBase& other) {
|
||||
copy(other);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
// //////////////////////////////////////////////////////////////////
|
||||
// //////////////////////////////////////////////////////////////////
|
||||
// //////////////////////////////////////////////////////////////////
|
||||
@ -258,41 +214,26 @@ ValueConstIterator::operator =( const ValueIteratorBase &other )
|
||||
// //////////////////////////////////////////////////////////////////
|
||||
// //////////////////////////////////////////////////////////////////
|
||||
|
||||
ValueIterator::ValueIterator()
|
||||
{
|
||||
}
|
||||
|
||||
ValueIterator::ValueIterator() {}
|
||||
|
||||
#ifndef JSON_VALUE_USE_INTERNAL_MAP
|
||||
ValueIterator::ValueIterator(const Value::ObjectValues::iterator& current)
|
||||
: ValueIteratorBase( current )
|
||||
{
|
||||
}
|
||||
: ValueIteratorBase(current) {}
|
||||
#else
|
||||
ValueIterator::ValueIterator(const ValueInternalArray::IteratorState& state)
|
||||
: ValueIteratorBase( state )
|
||||
{
|
||||
}
|
||||
: ValueIteratorBase(state) {}
|
||||
|
||||
ValueIterator::ValueIterator(const ValueInternalMap::IteratorState& state)
|
||||
: ValueIteratorBase( state )
|
||||
{
|
||||
}
|
||||
: ValueIteratorBase(state) {}
|
||||
#endif
|
||||
|
||||
ValueIterator::ValueIterator(const ValueConstIterator& other)
|
||||
: ValueIteratorBase( other )
|
||||
{
|
||||
}
|
||||
: ValueIteratorBase(other) {}
|
||||
|
||||
ValueIterator::ValueIterator(const ValueIterator& other)
|
||||
: ValueIteratorBase( other )
|
||||
{
|
||||
}
|
||||
: ValueIteratorBase(other) {}
|
||||
|
||||
ValueIterator &
|
||||
ValueIterator::operator =( const SelfType &other )
|
||||
{
|
||||
ValueIterator& ValueIterator::operator=(const SelfType& other) {
|
||||
copy(other);
|
||||
return *this;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user