Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Implement AA .byPair forward range #574

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/object.di
Expand Up @@ -485,6 +485,32 @@ public:

return Result(_aaRange(p));
}

@property auto byPair()
{
static struct Result
{
AARange r;

@property bool empty() { return _aaRangeEmpty(r); }
@property auto front() @trusted
{
static struct Pair
{
private Key* keyp;
private Value* valp;
@property ref Key key() { return *keyp; }
@property ref Value value() { return *valp; }
}
return Pair(cast(Key*)_aaRangeFrontKey(r),
cast(Value*)_aaRangeFrontValue(r));
}
void popFront() { _aaRangePopFront(r); }
Result save() { return this; }
}

return Result(_aaRange(p));
}
}

// Scheduled for deprecation in December 2012.
Expand Down
62 changes: 62 additions & 0 deletions src/object_.d
Expand Up @@ -2039,6 +2039,34 @@ public:

return Result(_aaRange(p));
}

@property auto byPair()
{
static struct Result
{
AARange r;

@property bool empty() { return _aaRangeEmpty(r); }
@property auto front() @trusted
{
static struct Pair
{
// We save the pointers here so that the Pair we return
// won't mutate when Result.popFront is called afterwards.
private Key* keyp;
private Value* valp;
@property ref Key key() { return *keyp; }
@property ref Value value() { return *valp; }
}
return Pair(cast(Key*)_aaRangeFrontKey(r),
cast(Value*)_aaRangeFrontValue(r));
}
void popFront() { _aaRangePopFront(r); }
Result save() { return this; }
}

return Result(_aaRange(p));
}
}

unittest
Expand Down Expand Up @@ -2226,6 +2254,40 @@ unittest
aa[1] = 1;
}

unittest
{
// Issue 9119
int[string] aa;
assert(aa.byPair.empty);

aa["a"] = 1;
aa["b"] = 2;
aa["c"] = 3;

auto pairs = aa.byPair;
auto savedPairs = pairs.save;
size_t count = 0;
while (!pairs.empty)
{
assert(pairs.front.key in aa);
assert(pairs.front.value == aa[pairs.front.key]);
count++;
pairs.popFront();
}
assert(count == aa.length);

// Verify that saved range can iterate over the AA again
count = 0;
while (!savedPairs.empty)
{
assert(savedPairs.front.key in aa);
assert(savedPairs.front.value == aa[savedPairs.front.key]);
count++;
savedPairs.popFront();
}
assert(count == aa.length);
}

deprecated("Please use destroy instead of clear.")
alias destroy clear;

Expand Down