简体   繁体   English

为什么在 PatriciaTrie 中无法访问 `floorEntry` 和其他方法?

[英]Why `floorEntry` and other methods are not accessible in PatriciaTrie?

While implementing an ip-lookup structure, I was trying to maintain a set of keys in a trie-like structure that allows me to search the "floor" of a key (that is, the largest key that is less or equal to a given key).在实现 ip-lookup 结构时,我试图在类似 trie 的结构中维护一组键,该结构允许我搜索键的“地板”(即小于或等于给定的最大键)钥匙)。 I decided to use Apache Collections 4 PatriciaTrie but sadly, I found that the floorEntry and related methods are not public .我决定使用 Apache Collections 4 PatriciaTrie但遗憾的是,我发现floorEntry和相关方法不是public My current "dirty" solution is forcing them with reflection (in Scala):我目前的“肮脏”解决方案是通过反射(在 Scala 中)强制它们:

val pt = new PatriciaTrie[String]()
val method = pt.getClass.getSuperclass.getDeclaredMethod("floorEntry", classOf[Object])
method.setAccessible(true)
// and then for retrieving the entry for floor(key) 
val entry = method.invoke(pt, key).asInstanceOf[Entry[String, String]]

Is there any clean way to have the same functionality?有没有干净的方法来拥有相同的功能? Why this methods are not publicly available?为什么这种方法不公开?

Why those methods are not public, I don't know.为什么这些方法不是公开的,我不知道。 (Maybe it's because you can achieve what you want with common Map API). (也许是因为你可以用通用的Map API 实现你想要的)。

Here's a way to fulfil your requirement:这是满足您要求的一种方法:

PatriciaTrie<String> trie = new PatriciaTrie<>();
trie.put("a", "a");
trie.put("b", "b");
trie.put("d", "d");

String floorKey = trie.headMap("d").lastKey(); // d

According to the docs, this is very efficient, since it depends on the number of bits of the largest key of the trie.根据文档,这是非常有效的,因为它取决于特里树的最大密钥的位数。

EDIT: As per the comment below, the code above has a bounds issue: headMap() returns a view of the map whose keys are strictly lower than the given key.编辑:根据下面的评论,上面的代码有一个边界问题: headMap()返回其键严格低于给定键的地图视图。 This means that, ie for the above example, trie.headMap("b").lastKey() will return "a" , instead of "b" (as needed).这意味着,即对于上面的示例, trie.headMap("b").lastKey()将返回"a" ,而不是"b" (根据需要)。

In order to fix this bounds issue, you can use the following trick:为了解决这个边界问题,您可以使用以下技巧:

String cFloorKey = trie.headMap("c" + "\uefff").lastKey(); // b

String dFloorKey = trie.headMap("d" + "\uefff").lastKey(); // d

Now everything works as expected, since \ is the highest unicode character.现在一切都按预期工作,因为\是最高的 unicode 字符。 Actually, searching for key + "\" , whatever key is, will always return key if it belongs to the trie, or the element immediately prior to key , if key is not present in the trie.实际上,搜索key + "\" ,不管key是,总是会返回key ,如果它属于该线索,或元素之前立即key ,如果key是不存在的线索。

Now, this trick works for String keys, but is extensible to other types as well.现在,这个技巧适用于String键,但也可以扩展到其他类型。 ie for Integer keys you could search for key + 1 , for Date keys you could add 1 millisecond, etc.即对于Integer键,您可以搜索key + 1 ,对于Date键,您可以添加 1 毫秒,等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM