djyin4op / concurrent-trees

Automatically exported from code.google.com/p/concurrent-trees
0 stars 0 forks source link

add tree.contains()-method #9

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
In my opinion a very useful method would be a boolean .contains()-method, which 
check if a query is contained in the tree: This should be similar to 
.getKeysStartingWith(query), but break if it finds the first path matching the 
query and return true.

For example:

tree.put("TEST", 1);
tree.put("TOAST", 2);
tree.put("TEAM", 3);

tree.contains("TO") -> returns true.

Original issue reported on code.google.com by steffen...@web.de on 25 Sep 2014 at 1:34

GoogleCodeExporter commented 9 years ago
This is a good suggestion, thanks!

There is a subtle difference though in how contains might be implemented. In 
your example above, "TO" was never added to the tree explicitly. So I'm not 
sure that contains() should return true for that(?)

There are two options for how contains("TO") could be implemented:

Option 1:
    return tree.getKeysStartingWith("TO").hasNext()

Option 2:
    return tree.getValueForExactKey("TO") != null

TBH I'd probably lean towards the second option. But both options are possible.

Original comment by ni...@npgall.com on 25 Sep 2014 at 2:16

GoogleCodeExporter commented 9 years ago
If "TO" would return true (even if it was never added explicitly) this would 
fit my need =)

Your option 1:
With my example above: return tree.getKeysStartingWith("TOAST").hasNext() would 
return false, but should return true.

your option 2:
I'm not sure if this will work... tree.getValueForExactKey("TO") returns null, 
right? So null != null will be false. But I ran an example: this case returns 
true. But it also returns true for tree.getValueForExactKey("FOO") != null

Original comment by steffen...@web.de on 25 Sep 2014 at 2:39

GoogleCodeExporter commented 9 years ago
edit to my last comment: If contains("TO") would return true (even if it was 
never added explicitly) this would fit my needs =)

Original comment by steffen...@web.de on 25 Sep 2014 at 2:41

GoogleCodeExporter commented 9 years ago
(Why can't I edit my comment? Sorry for multiposting...)
A combination of both would work:

return (tree.getKeysStartingWith("TO").hasNext() || 
tree.getValueForExactKey("TO"))
return (tree.getKeysStartingWith("TOAST").hasNext() || 
tree.getValueForExactKey("TOAST"))

both return true.

Maybe you can implement .contains() and .containsExact() 
=D

Original comment by steffen...@web.de on 25 Sep 2014 at 2:48

GoogleCodeExporter commented 9 years ago
If tree.getValueForExactKey("TO") != null, then 
tree.getKeysStartingWith("TO").hasNext() would return true also actually. So we 
only need to evaluate getValueForExactKey() there.

I think there should be a contains() method, but I think it should indicate if 
the key was added to the tree explicitly. So it would be more like your 
containsExact() idea. That would be consistent with contains() in Java 
collections framework. So the implementation might be:

public boolean contains(CharSequence key) {
    return tree.getValueForExactKey(key) != null;
}

There is already a reasonably nice API to check if keys starting with "TO" are 
contained in the tree: 
tree.getKeysStartingWith("TOAST").hasNext()

So hopefully with those two APIs available people would have a choice.

Original comment by ni...@npgall.com on 25 Sep 2014 at 5:44