Deep Equality (optimized)

by Ricardo “Rudxain” Fernández Serrata

Version 11 (November 1, 2021)

Download (51 downloads)

Checks if ANY 2 values are "deeply equal" (can check flat and nested containers). It uses an explicit stack to avoid subroutine calls, for better performance and memory usage.

I used a stack format where each frame contains a 3-tuple (array of fixed length of 3), instead of storing each value as an individual frame. `top` is the last stack frame. top[0] is the iteration index of the last frame. top[1] and top[2] are inner/deeper values of A and B respectively.

If I didn't use 3-tuples, the format would've been like this: S[-1] is B, S[-2] is A and S[-3] is the iteration index. This format would require more blocks, so I fit all 3 values in 1 frame for a minified flow.

type(a)[-1] = 0x79
(equal to substr(type(a), -1, 1) = "y")
Checks if `a` is a container (array or dictionary) and not a text string.

I already included example inputs, you can edit them to check correctness of this flow.


Please read:

The `=` before recurring already "protects" just a little against recursive references, however I'm afraid my flow will throw a "Max stack size exceeded" error on some edge cases. Even if those edge cases don't happen, my implementation is still NOT THE fastest, because some cycles would cause redundant iterations (not infinite, but just more than necessary)

There are some cases where "=" operator successfully detects that 2 containers are equal. This happens when 2 or more variables are referencing the same memory address (shared data structure), which allows programming languages (AM included) to easily detect the equality of these variables just by reading metadata.

This is why I included the "a = b" check, to avoid unnecessary processing, when possible.

The order of the logical ANDs matters, but I don't know if the order I used is the fastest

4.0 average rating from 1 reviews

5 stars
4 stars
3 stars
2 stars
1 star

Rate and review within the app in the Community section.