StringBuilder 与 Ropes
早上好,
我正在编写一个语言解析器,并且正在寻找用于回滚缓存的最佳结构,该结构当前执行以下操作:
- 当从流中请求新字符时,该字符被添加到缓存中,以防回滚被要求。
- 当请求回滚时,返回到缓存中的某个点,以便当请求另一个字符时,它会从那里获取它。
- 找到令牌后,删除回滚缓存中直到当前位置的所有内容。
简而言之,我很想知道您认为哪种数据结构是最佳数据结构:
- 优先级 1:附加字符(codePoints 将是一个受欢迎的添加)
- 优先级 2:执行子字符串(例如 StringBuilder.delete(...) )关于数据结构(或完全清除)
- 优先级 3:能够创建缓存字符串(例如 StringBuilder.toString())
我希望很快能收到您的来信!
Good morning,
I am writing a language parser, and am looking for the best structure to use for a rollback cache which currently does the following:
- When requesting a new character from the stream, the character is added to the cache, in case a rollback is requested.
- When a rollback is requested, go back to a certain point in the cache so that when another character is requested, it gets it from there instead.
- When a token is found, remove everything in the rollback cache up to the current position.
So in short, I would love to know which you feel to be the best data structure for:
- Priority 1: appending characters (codePoints would be a welcome addition)
- Priority 2: Doing a substring (such as StringBuilder.delete(...)) on the data structure (or clearing completely)
- Priority 3: Being able to create a string of the cache (e.g. StringBuilder.toString())
I hope to hear from you soon!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我是您,对于这样的专门用途以及可能的性能和资源限制,我会从基元实现我自己的缓冲区。我认为适应现有的结构会更麻烦。当然,如果不影响的话,我会尽量遵循众所周知的相关接口,例如
CharSequence
、Appendable
、List
> 等If I were you, for such a specialised use and with possible performance and resource constraints, I'd implement my own buffer from primitives. I think it's more trouble adapting existing structures. Of course, if it didn't hurt, I'd try to conform to the well known relevant interfaces, such as
CharSequence
,Appendable
,List
, etc.我怀疑 StringBuilder 和
PushbackReader 的组合
会给你你所需要的。使用StringBuilder积累字符并创建token String,使用PushbackReader的mark
和reset
方法实现回滚。或者,将整个输入文件作为字符串预读取,然后通过索引字符串并获取子字符串来实现分词器。
I suspect that a combination of a StringBuilder and
PushbackReader
will give you what you need. Use the StringBuilder to accumulate characters and create token Strings, and the PushbackReader'smark
andreset
methods to implement rollback.Alternatively, pre-read the entire input file as a String and then implement the tokenizer by indexing the String and taking substrings.