How to determine if two mutable data structures are the same?

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} *)

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

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"]},
NativeSameInstanceQ[e1, e2]]];

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

SameQ[a, b, c]

(* True *)

sameInstanceQ[a, b]

(* False *)

sameInstanceQ[b, c]

(* True *)