C# 中绳索的公开实现?

发布于 2024-08-13 14:14:16 字数 118 浏览 3 评论 0原文

C# 中是否有 Rope 数据结构的公共实现?

Is there a public implementation of the Rope data structure in C#?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

鱼忆七猫命九 2024-08-20 14:14:16

我不知道 Rope 实现(尽管可能有一个!),但是如果您只是在进行串联之后,StringBuilder 将完成这项工作。

I'm not aware of a Rope implementation (though there probably is one!), but if you're only after doing concatenation, StringBuilder will do the job.

踏雪无痕 2024-08-20 14:14:16

就其价值而言,这是一个不可变的 Java 实现。您可能可以在不到一个小时的时间内将其转换为 C#。

For what its worth, here is an immutable Java implementation. You could probably convert it to C# in less than an hour.

淡莣 2024-08-20 14:14:16

Wintellect Power Collections(一个 C# 数据结构库)中的 BigList 类在某种程度上类似于绳索:
http://docs.pushtechnology.com/docs/4.5 .7/dotnet/externalclient/html/class_wintellect_1_1_power_collections_1_1_big_list_3_01_t_01_4.html

我测量了它的性能,它在“字符串插入开始”中表现得很好:

const int InsertCount = 150000;

var startTime = DateTime.Now;
var ropeOfChars = new BigList<char>();
for (int i = 0; i < InsertCount; i++)
{
    ropeOfChars.Insert(0, (char)('a' + (i % 10)));
}
Console.WriteLine("Rope<char> time: {0}", DateTime.Now - startTime);

startTime = DateTime.Now;
var stringBuilder = new StringBuilder();
for (int i = 0; i < InsertCount; i++)
{
    stringBuilder.Insert(0, (char)('a' + (i % 10)));
}
Console.WriteLine("StringBuilder time: {0}", DateTime.Now - startTime);

结果:

Rope<char> time: 00:00:00.0468740
StringBuilder time: 00:00:05.1471300

但在“字符串插入中间”中表现不佳:

const int InsertCount = 150000;

var startTime = DateTime.Now;
var ropeOfChars = new BigList<char>();
for (int i = 0; i < InsertCount; i++)
{
    ropeOfChars.Insert(ropeOfChars.Count / 2, (char)('a' + (i % 10)));
}
Console.WriteLine("Rope<char> time: {0}", DateTime.Now - startTime);

startTime = DateTime.Now;
var stringBuilder = new StringBuilder();
for (int i = 0; i < InsertCount; i++)
{
    stringBuilder.Insert(stringBuilder.Length / 2, (char)('a' + (i % 10)));
}
Console.WriteLine("StringBuilder time: {0}", DateTime.Now - startTime);

结果:

Rope<char> time: 00:00:15.0229452
StringBuilder time: 00:00:04.7812553

我不确定这是否是一个错误或低效的实现,但“rope of chars”预计比 C# 中的 StringBuilder 更快。

您可以从 NuGet 安装 Power Collections:

Install-Package XAct.Wintellect.PowerCollections

The BigList<T> class from Wintellect Power Collections (a C# data structure library) is somehow similar to rope:
http://docs.pushtechnology.com/docs/4.5.7/dotnet/externalclient/html/class_wintellect_1_1_power_collections_1_1_big_list_3_01_t_01_4.html

I measured its performance and it performs pretty well in "start of string inserts":

const int InsertCount = 150000;

var startTime = DateTime.Now;
var ropeOfChars = new BigList<char>();
for (int i = 0; i < InsertCount; i++)
{
    ropeOfChars.Insert(0, (char)('a' + (i % 10)));
}
Console.WriteLine("Rope<char> time: {0}", DateTime.Now - startTime);

startTime = DateTime.Now;
var stringBuilder = new StringBuilder();
for (int i = 0; i < InsertCount; i++)
{
    stringBuilder.Insert(0, (char)('a' + (i % 10)));
}
Console.WriteLine("StringBuilder time: {0}", DateTime.Now - startTime);

Results:

Rope<char> time: 00:00:00.0468740
StringBuilder time: 00:00:05.1471300

But it performs not well in "middle of string inserts":

const int InsertCount = 150000;

var startTime = DateTime.Now;
var ropeOfChars = new BigList<char>();
for (int i = 0; i < InsertCount; i++)
{
    ropeOfChars.Insert(ropeOfChars.Count / 2, (char)('a' + (i % 10)));
}
Console.WriteLine("Rope<char> time: {0}", DateTime.Now - startTime);

startTime = DateTime.Now;
var stringBuilder = new StringBuilder();
for (int i = 0; i < InsertCount; i++)
{
    stringBuilder.Insert(stringBuilder.Length / 2, (char)('a' + (i % 10)));
}
Console.WriteLine("StringBuilder time: {0}", DateTime.Now - startTime);

Results:

Rope<char> time: 00:00:15.0229452
StringBuilder time: 00:00:04.7812553

I am not sure if this is a bug or unefficient implementation, but "rope of chars" is expected to be faster that StringBuilder in C#.

You can install Power Collections from NuGet:

Install-Package XAct.Wintellect.PowerCollections
烂人 2024-08-20 14:14:16

我的同事在此处用 C# 编写了 Rope 实现。

也可以作为 NuGet 包 FlatlinerDOA.Rope 提供。

My colleague has written a Rope implementation in C# here.

Also available as NuGet package FlatlinerDOA.Rope.

弱骨蛰伏 2024-08-20 14:14:16

这里是 C# 中 Ropes 的公共实现,基于上面列出的不可变 java 实现。请注意,您不会获得与 java 版本相同的多态性优势,因为字符串无法继承,并且 C# 中本身不存在 CharSequence。

Here is a public implementation of Ropes in C#, based on the immutable java implementation listed above. Note that you won't get the same polymorphism benefits as the java version because strings can't be inherited and CharSequence doesn't exist natively in C#.

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