Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Duplicate Query Results: Use NSOrderedSet instead of NSArray #319

Open
sapieneptus opened this issue Jan 7, 2016 · 2 comments
Open

Comments

@sapieneptus
Copy link

Motivation

Suppose you have a hierarchy:

myView <UIView>
    └ myScrollView <UIScrollView>
        └ myLabel <UILabel>

And you do a query

query "* UILabel"

You will end up returning 3 duplicate copies of myLabel.

Explanation

"* UILabel" means any UILabel, or any descendent of a UIView that is a UILabel. myLabel matches this condition on three separate occasions:

  1. myLabel is a UILabel that has descended from myView, which is a UIView
  2. myLabel is a UILabel that has descended from myScrollView, which is a UIView
  3. myLabel is a UILabel

Walkthrough

# Evaluation algorithm for finding views by class
def find(view, class):
    res = []
    if view.matches(class):
        res.add(view)
    for subview in view.subviews:
        res.addAll(find(subview, class))

# query string: "* UILabel" =>  "UIView UILabel" 
# So we are searching for all UIViews, then from those results, 
# we check if any result is a UILabel, recurse on the subviews, 
# and return cumulation of all UILabel results. 

find(mainWindow, UIView) =>  [ myView, myScrollView, myLabel ]  #returns the whole heirarchy 

#Then we recurse on each match, but searching for UILabel
find(myView, UILabel) => [ myLabel ]
find(myScrollView, UILabel) => [ myLabel ]
find(myLabel, UILabel) => [ myLabel ]

#Final return value:
[ myLabel, myLabel, myLabel ]

This can be easily resolved if we use an NSMutableOrderedSet to gather results instead of an NSMutableArray.

@jmoody
Copy link
Contributor

jmoody commented Jan 7, 2016

Won't using NSSet loose the order?

@sapieneptus sapieneptus changed the title Fix Duplicate Query Results: Use NSSet instead of NSArray Fix Duplicate Query Results: Use NSOrderedSet instead of NSArray Jan 7, 2016
@sapieneptus
Copy link
Author

NSOrderedSet

@jmoody jmoody modified the milestones: 2.0, 0.20.0 May 27, 2016
@jmoody jmoody modified the milestone: 0.20.0 Sep 12, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants