recursive equal, and firstDifference functions

H. S. Teoh hsteoh at quickfur.ath.cx
Tue Mar 19 11:12:38 PDT 2013


On Tue, Mar 19, 2013 at 01:48:43PM -0400, Jonathan M Davis wrote:
> On Tuesday, March 19, 2013 18:26:16 timotheecour wrote:
> > > somewhere else, but I don't see a relevant package. Maybe a new
> > > std.algorithm2 for non-ranges?
> > > 
> > > Also, the OT's firstDifference would go there too, and I have a
> > > recursive (to specified level) toStringRecurse that would
> > > belong there too.
> > 
> > Also, I'd add to that list copyRecurse and some more, that
> > operate on arbitrary types, not just ranges, so we have:
> > 
> > equalRecurse
> > copyRecurse (deep copy)
> > toStringRecurse
> > firstDifference (see OT)
> > toHashRecurse (should compare equal with a data structure
> > serialized and then deserialized via a serialization function, eg
> > std.orange)
> > 
> > I'm sure there's more.
> > 
> > that seems a starting point for a new package that operates on any
> > type recursively (not just ranges), no?  std.deep?std.recurse?  Some
> > of those could have a depth level compile time parameter that stops
> > recursion at that level, which would be infinity by default.
> 
> And how do you even have the concept of recursion without some sort of
> range or container to recursively iterate through?
[...]

One can iterate over every member of a struct/class and recursively
invoke equalRecurse on them. Something like this:

	bool recursiveEqual(T)(T a, T b) {
		static if (isAtomic!T) {
			return a == b;
		} else {
			foreach (attr; __traits(getAllMembers, T)) {
				// TBD: need to skip stuff like member
				// functions or internal stuff like
				// compiler-generated attributes, hidden
				// context ptrs, etc.

				alias attr1 = __traits(getMember, a, attr);
				alias attr2 = __traits(getMember, b, attr);

				if (!recursiveEqual(attr, attr2))
					return false;
			}
		}
		return true;
	}


T

-- 
The best way to destroy a cause is to defend it poorly.


More information about the Digitalmars-d-learn mailing list