STL Rope - when and where to use

后端 未结 5 1355
無奈伤痛
無奈伤痛 2020-11-30 02:57

I was wondering under what circumstances you would use a rope over another STL container?

相关标签:
5条回答
  • 2020-11-30 03:31

    Ropes are a scalable string implementation: they are designed for efficient operation that involve the string as a whole. Operations such as assignment, concatenation, and substring take time that is nearly independent of the length of the string. Unlike C strings, ropes are a reasonable representation for very long strings such as edit buffers or mail messages.

    Advantages:

    1. Much faster concatenation and substring operations involving long strings. Inserting a character in the middle of a 10 megabyte rope should take on the order of 10s of microseconds, even if a copy of the original is kept, e.g. as part of an edit history. In contrast, this would take on the order of a second for conventional "flat" string representation. The time required for concatenation can be viewed as constant for most applications. It is perfectly reasonable to use a rope as the representation of a file inside a text editor.

    2. Potentially much better space performance. Minor modifications of a rope can share memory with the original. Ropes are allocated in small chunks, significantly reducing memory fragmentation problems introduced by large blocks

    3. Assignment is simply a (possibly reference counted) pointer assignment. Unlike reference-counted copy-on-write implementations, this remains largely true even if one of the copies is subsequently slightly modified. It is very inexpensive to checkpoint old versions of a string, e.g. in an edit history.

    4. It is possible to view a function producing characters as a rope. Thus a piece of a rope may be a 100MByte file, which is read only when that section of the string is examined. Concatenating a string to the end of such a file does not involve reading the file. (Currently the implementation of this facility is incomplete.)

    https://wayback.archive.org/web/20130102093702/https://www.sgi.com/tech/stl/Rope.html

    0 讨论(0)
  • 2020-11-30 03:33

    The only bad thing with ropes is threads and misuse.

    Under Linux (and probably most other OSes) it is said that the thread-safety code is what makes ropes so much slower. So I just rip that code out (set a compiler def for threads-off), because I am using a single thread in an embedded platform.

    Otherwise, ropes are much faster than strings, have less likelihood of getting out of memory on large buffers, and are much faster for edits of large buffers; Such as removing a bad character in the middle of the Bible.

    This is due to the way in which a rope is interpreted as data. As a lot of little smaller 'strings' chained together via a linked-list to produce the final string.

    0 讨论(0)
  • 2020-11-30 03:42

    It is a non-standard alternative to string that handles large data sizes. See here for how it works.

    0 讨论(0)
  • 2020-11-30 03:48

    There is a lot of emphasis here on strings made up of characters, but rope is simply a 1D sequence with fast insertions and deletions (anywhere within the sequence).

    It seems a bit surprising that such a basic capability is rarely required for anything (other than strings). Where would I use a rope of integers? I don't know, because manipulating it requires the indices to come from somewhere.

    The best contrived real-world example would be where I'm making a UI to let the user view a dataset made up of thousands of images, and the user needs to be able to delete some of them and rearrange the order of the others.

    0 讨论(0)
  • 2020-11-30 03:49

    I wouldn't use it at all, but that's because I'm bit of an "easy portability" freak, and tend only to use bog-standard containers. The rope is part of SGI's STL implementation, and is not part of the C++ Standard.

    0 讨论(0)
提交回复
热议问题