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

Commit

Permalink
Merge pull request #523 from denis-sh/override-equals-in-`TypeInfo_…
Browse files Browse the repository at this point in the history
…AssociativeArray`

Override `equals` in `TypeInfo_AssociativeArray`.
  • Loading branch information
MartinNowak committed Jun 22, 2013
2 parents e4b01a5 + 3bf1cb0 commit b314533
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 82 deletions.
44 changes: 30 additions & 14 deletions src/object.di
Expand Up @@ -372,22 +372,22 @@ extern (C)
{
// from druntime/src/compiler/dmd/aaA.d

size_t _aaLen(void* p);
void* _aaGet(void** pp, TypeInfo keyti, size_t valuesize, ...);
void* _aaGetRvalue(void* p, TypeInfo keyti, size_t valuesize, ...);
void* _aaIn(void* p, TypeInfo keyti);
void _aaDel(void* p, TypeInfo keyti, ...);
void[] _aaValues(void* p, size_t keysize, size_t valuesize);
void[] _aaKeys(void* p, size_t keysize);
void* _aaRehash(void** pp, TypeInfo keyti);
size_t _aaLen(in void* p) pure nothrow;
void* _aaGet(void** pp, const TypeInfo keyti, in size_t valuesize, ...);
inout(void)* _aaGetRvalue(inout void* p, in TypeInfo keyti, in size_t valuesize, ...);
inout(void)* _aaIn(inout void* p, in TypeInfo keyti);
void _aaDel(void* p, in TypeInfo keyti, ...);
inout(void)[] _aaValues(inout void* p, in size_t keysize, in size_t valuesize) pure nothrow;
inout(void)[] _aaKeys(inout void* p, in size_t keysize) pure nothrow;
void* _aaRehash(void** pp, in TypeInfo keyti) pure nothrow;

extern (D) alias scope int delegate(void *) _dg_t;
int _aaApply(void* aa, size_t keysize, _dg_t dg);

extern (D) alias scope int delegate(void *, void *) _dg2_t;
int _aaApply2(void* aa, size_t keysize, _dg2_t dg);

void* _d_assocarrayliteralT(TypeInfo_AssociativeArray ti, size_t length, ...);
void* _d_assocarrayliteralT(TypeInfo_AssociativeArray ti, in size_t length, ...);
}

private template _Unqual(T)
Expand Down Expand Up @@ -476,26 +476,42 @@ private:

public:

@property size_t length() { return _aaLen(p); }
@property size_t length() const { return _aaLen(p); }

Value[Key] rehash() @property
{
auto p = _aaRehash(cast(void**) &p, typeid(Value[Key]));
return *cast(Value[Key]*)(&p);
}

Value[] values() @property
// Note: can't make `values` and `keys` inout as it is used
// e.g. in Phobos like `ReturnType!(aa.keys)` instead of `typeof(aa.keys)`
// which will result in `inout` propagation.

inout(Value)[] inout_values() inout @property
{
auto a = _aaValues(p, Key.sizeof, Value.sizeof);
return *cast(Value[]*) &a;
return *cast(inout Value[]*) &a;
}

Key[] keys() @property
inout(Key)[] inout_keys() inout @property
{
auto a = _aaKeys(p, Key.sizeof);
return *cast(Key[]*) &a;
return *cast(inout Key[]*) &a;
}

Value[] values() @property
{ return inout_values; }

Key[] keys() @property
{ return inout_keys; }

const(Value)[] values() const @property
{ return inout_values; }

const(Key)[] keys() const @property
{ return inout_keys; }

int opApply(scope int delegate(ref Key, ref Value) dg)
{
return _aaApply2(p, Key.sizeof, cast(_dg2_t)dg);
Expand Down
52 changes: 37 additions & 15 deletions src/object_.d
Expand Up @@ -619,6 +619,11 @@ class TypeInfo_AssociativeArray : TypeInfo
this.value == c.value;
}

override bool equals(in void* p1, in void* p2) @trusted const
{
return !!_aaEqual(this, *cast(const void**) p1, *cast(const void**) p2);
}

override hash_t getHash(in void* p) nothrow @trusted const
{
return _aaGetHash(cast(void*)p, this);
Expand Down Expand Up @@ -2051,23 +2056,24 @@ extern (C)
{
// from druntime/src/compiler/dmd/aaA.d

size_t _aaLen(void* p);
void* _aaGet(void** pp, TypeInfo keyti, size_t valuesize, ...);
void* _aaGetRvalue(void* p, TypeInfo keyti, size_t valuesize, ...);
void* _aaIn(void* p, TypeInfo keyti);
void _aaDel(void* p, TypeInfo keyti, ...);
void[] _aaValues(void* p, size_t keysize, size_t valuesize);
void[] _aaKeys(void* p, size_t keysize);
void* _aaRehash(void** pp, TypeInfo keyti);
size_t _aaLen(in void* p) pure nothrow;
void* _aaGet(void** pp, const TypeInfo keyti, in size_t valuesize, ...);
inout(void)* _aaGetRvalue(inout void* p, in TypeInfo keyti, in size_t valuesize, ...);
inout(void)* _aaIn(inout void* p, in TypeInfo keyti);
void _aaDel(void* p, in TypeInfo keyti, ...);
inout(void)[] _aaValues(inout void* p, in size_t keysize, in size_t valuesize) pure nothrow;
inout(void)[] _aaKeys(inout void* p, in size_t keysize) pure nothrow;
void* _aaRehash(void** pp, in TypeInfo keyti) pure nothrow;

extern (D) alias scope int delegate(void *) _dg_t;
int _aaApply(void* aa, size_t keysize, _dg_t dg);

extern (D) alias scope int delegate(void *, void *) _dg2_t;
int _aaApply2(void* aa, size_t keysize, _dg2_t dg);

void* _d_assocarrayliteralT(TypeInfo_AssociativeArray ti, size_t length, ...);
hash_t _aaGetHash(void* aa, const(TypeInfo) tiRaw) nothrow;
void* _d_assocarrayliteralT(TypeInfo_AssociativeArray ti, in size_t length, ...);
int _aaEqual(in TypeInfo tiRaw, in void* e1, in void* e2);
hash_t _aaGetHash(in void* aa, in TypeInfo tiRaw) nothrow;
}

private template _Unqual(T)
Expand Down Expand Up @@ -2156,26 +2162,42 @@ private:

public:

@property size_t length() { return _aaLen(p); }
@property size_t length() const { return _aaLen(p); }

Value[Key] rehash() @property
{
auto p = _aaRehash(cast(void**) &p, typeid(Value[Key]));
return *cast(Value[Key]*)(&p);
}

Value[] values() @property
// Note: can't make `values` and `keys` inout as it is used
// e.g. in Phobos like `ReturnType!(aa.keys)` instead of `typeof(aa.keys)`
// which will result in `inout` propagation.

inout(Value)[] inout_values() inout @property
{
auto a = _aaValues(p, Key.sizeof, Value.sizeof);
return *cast(Value[]*) &a;
return *cast(inout Value[]*) &a;
}

Key[] keys() @property
inout(Key)[] inout_keys() inout @property
{
auto a = _aaKeys(p, Key.sizeof);
return *cast(Key[]*) &a;
return *cast(inout Key[]*) &a;
}

Value[] values() @property
{ return inout_values; }

Key[] keys() @property
{ return inout_keys; }

const(Value)[] values() const @property
{ return inout_values; }

const(Key)[] keys() const @property
{ return inout_keys; }

int opApply(scope int delegate(ref Key, ref Value) dg)
{
return _aaApply2(p, Key.sizeof, cast(_dg2_t)dg);
Expand Down

0 comments on commit b314533

Please sign in to comment.