StringBuilder 与 Ropes

发布于 2024-11-06 20:09:48 字数 434 浏览 1 评论 0原文

早上好,

我正在编写一个语言解析器,并且正在寻找用于回滚缓存的最佳结构,该结构当前执行以下操作:

  • 当从流中请求新字符时,该字符被添加到缓存中,以防回滚被要求。
  • 当请求回滚时,返回到缓存中的某个点,以便当请求另一个字符时,它会从那里获取它。
  • 找到令牌后,删除回滚缓存中直到当前位置的所有内容。

简而言之,我很想知道您认为哪种数据结构是最佳数​​据结构:

  • 优先级 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

想念有你 2024-11-13 20:09:48

如果我是您,对于这样的专门用途以及可能的性能和资源限制,我会从基元实现我自己的缓冲区。我认为适应现有的结构会更麻烦。当然,如果不影响的话,我会尽量遵循众所周知的相关接口,例如 CharSequenceAppendableList > 等

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.

把时间冻结 2024-11-13 20:09:48

我怀疑 StringBuilder 和 PushbackReader 的组合会给你你所需要的。使用StringBuilder积累字符并创建token String,使用PushbackReader的markreset方法实现回滚。

或者,将整个输入文件作为字符串预读取,然后通过索引字符串并获取子字符串来实现分词器。

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's mark and reset 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文