Project

General

Profile

Feature #12210

Add IdentitySet class that compares members by identity

Added by tjwp (Tim Perkins) over 1 year ago. Updated 10 months ago.

Status:
Closed
Priority:
Normal
Target version:
-
[ruby-core:74498]

Description

This subclass of Set handles a use case that we ran into where we needed to track instances of objects that might compare as equal.

I was surprised that there was not a core way to do this. IdentitySet allows you to do the following (trivial example using strings):

  a_str = "a"
  s = IdentitySet.new([a_str, a_str, "b", "b"])

  p s # => #<IdentitySet: {"a", "b", "b"}>
identity_set.diff (1.18 KB) identity_set.diff patch tjwp (Tim Perkins), 03/23/2016 07:20 PM

Associated revisions

Revision 56589
Added by knu (Akinori MUSHA) 10 months ago

Add Set#compare_by_identity and Set#compare_by_identity?

  • lib/set.rb (Set#compare_by_identity, Set#compare_by_identity?): New methods. [Feature #12210]

Revision 56589
Added by knu (Akinori MUSHA) 10 months ago

Add Set#compare_by_identity and Set#compare_by_identity?

  • lib/set.rb (Set#compare_by_identity, Set#compare_by_identity?): New methods. [Feature #12210]

History

#1 [ruby-core:74501] Updated by duerst (Martin Dürst) over 1 year ago

I think that rather than creating a new class, adding an option on Set.new (e.g. Set.new([a_str, a_str, "b", "b"], compare_by: :identity)) is more Ruby-like.

#2 [ruby-core:74502] Updated by shyouhei (Shyouhei Urabe) over 1 year ago

If you want a new class, why not start as a separate gem for it?

#3 [ruby-core:74534] Updated by tjwp (Tim Perkins) over 1 year ago

I submitted this as a patch because the std lib already contains one subclass of Set (SortedSet) and a commented-out implementation of another (RestrictedSet).

I don’t expect that this class will change beyond the initial implementation. Following the precedent of SortedSet, I think that including IdentitySet in the std lib improves discoverability over providing it via a gem, which seems like a more heavyweight solution for a simple, useful variation on a Set.

I was surprised that there wasn't already a standard way to do this.

#4 [ruby-core:74613] Updated by shyouhei (Shyouhei Urabe) over 1 year ago

  • Status changed from Open to Assigned
  • Assignee set to knu (Akinori MUSHA)

#5 [ruby-core:77964] Updated by knu (Akinori MUSHA) 10 months ago

  • Tracker changed from Misc to Feature

I'd rather add Set#compare_by_identity.

#6 Updated by knu (Akinori MUSHA) 10 months ago

  • Status changed from Assigned to Closed

Applied in changeset r56589.


Add Set#compare_by_identity and Set#compare_by_identity?

  • lib/set.rb (Set#compare_by_identity, Set#compare_by_identity?): New methods. [Feature #12210]

#7 [ruby-core:77965] Updated by knu (Akinori MUSHA) 10 months ago

Note that SortedSet#compare_by_identity is unimplemented when SortedSet uses rbtree.

rbtree.readjust { |a, b| a.object_id <=> b.object_id } comes pretty close, but rbtree treats string keys specially by storing a copy of each string key given, so it does not work as expected for string keys. There needs some way provided on the rbtree side.

Also available in: Atom PDF