16
$\begingroup$

How can I determine if two mutable data structures (DataStructure) are truly the same? I do not mean checking if they have the same value. I mean checking if they are the same data structure, i.e. whether they both respond to mutation.

Example:

a = CreateDataStructure["Value", 1];

b = a["Copy"];

c = b;

Now b and c are the same, but a and b are different. They merely hold the same value. a === b tests if the value if the same, and therefore returns True.

But mutating a does not mutate b. The variables a and b do not point to the same data structure.

a["Set", 2]
(* 2 *)

{a["Get"], b["Get"]}
(* {2, 1} *)

b and c do point to the same data structure:

b["Set", 5]
(* 5 *)

{b["Get"], c["Get"]}
(* {5, 5} *)
$\endgroup$
  • 2
    $\begingroup$ Maybe try something like GroupBy[{a, b, c}, Hash] $\endgroup$ – ilian Apr 1 at 13:58
  • $\begingroup$ If you're in a <12.1 environment you can use any internal function that knows about this distinction, e.g. make a Language`ExpressionStore (the memory will clean itself up automatically if you do) and then attach a UUID to each Expr that you can check against. $\endgroup$ – b3m2a1 Apr 1 at 21:53
16
$\begingroup$

This way of comparison is not directly exposed in top-level at the moment.

However, it could be done through the compiler, for example

sameInstanceQ = 
  FunctionCompile[Function[{Typed[e1, "Expression"], Typed[e2, "Expression"]}, 
    Native`SameInstanceQ[e1, e2]]];

a = CreateDataStructure["Value", 1];
b = a["Copy"];
c = b;

SameQ[a, b, c]

(* True *)

sameInstanceQ[a, b]

(* False *)

sameInstanceQ[b, c]

(* True *)
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.