Jump to content
  • Advertisement
Sign in to follow this  
slicer4ever

Thoughts on double link list strings?

This topic is 2101 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hey good people of GameDev, i've been toying with the idea of using a double link list to contain a character array for fast insertion/change/modification of text with a text editor.

currently my ui text entry input stores each line, and rebuilds the line when their's a change on a particular line, this is sufficient for my general needs, but i was thinking of using linked list's to store text, it'd also make cursor tracking much easier since it'd be just containing a pointer to the current char container.

so, any thoughts on the subject from anyone's who tried something similar?

Share this post


Link to post
Share on other sites
Advertisement
The STL has a class called rope that I believe is a container for just this purpose. You can read bit on it and download the source to see how they are handling it.

Clicky to STL.
Clicky to some discussion about their rope implementation.

On your particular idea; overall, I think it's a reasonable idea. You may want to consider how you actually split your text buffer into sub-buffers though. For example, splitting by lines may result in too small sub strings and doesn't play well with wrapping of long lines into multiple lines. Consider, for example, splitting at paragraphs, or explicit new-lines, instead.

Share this post


Link to post
Share on other sites
I am not sure what "the STL" means these days, but rope is not part of the standard C++ library. g++ has it as a deprecated extension.

Share this post


Link to post
Share on other sites
I consider SGI's library to be more or less the STL, although it's not really the STL to be correct. There are some other variants also. But otherwise, no, the rope class did not become a part of the C++ standard library.

Share this post


Link to post
Share on other sites

hey good people of GameDev, i've been toying with the idea of using a double link list to contain a character array for fast insertion/change/modification of text with a text editor.

currently my ui text entry input stores each line, and rebuilds the line when their's a change on a particular line, this is sufficient for my general needs, but i was thinking of using linked list's to store text, it'd also make cursor tracking much easier since it'd be just containing a pointer to the current char container.

so, any thoughts on the subject from anyone's who tried something similar?


Does what you're currently doing work?
Is this premature optimization?
Does the alternative add complexity or reduce complexity?
How maintainable is your alternative solution?
Whats the cost vs. benefit ratio for this effort?

I think I'm leaning towards "Don't fix it if it ain't broke!"

Share this post


Link to post
Share on other sites

I am not sure what "the STL" means these days, but rope is not part of the standard C++ library. g++ has it as a deprecated extension.

"The STL" was a C++ library developed by Alexander Stepanov when he was working at Silicon Graphics (SGI) and at Hewlett-Packard (HP). Some parts of it were adopted and modified to form a small part of the C++ standard library some 15 or 20 years ago (closer to 20 now -- although the standard was officially finalized on November 17, 1997, the containers and algorithms had been a part of the draft standard for several years before that). People who refer to the C++ standard library as "the STL" are simply showing their ignorance and perhaps are deserving of your pity.

The "rope" class mentioned above was and is a part of the STL. Witness where the links go. You will likely not find a lot of technical support from SGI these days, nor really from the GNU port of the code that appears as an extension to the GCC standard library. Matt Austern did the original SGI implementation and contributed it to libstdc++-v3.

Share this post


Link to post
Share on other sites

hey good people of GameDev, i've been toying with the idea of using a double link list to contain a character array for fast insertion/change/modification of text with a text editor.

It sounds like you are talking about a linked list of characters (not a rope, which is a linked list of strings).

Are you really sure you want to absorb the cost of 2x pointers (8x2 = 16 bytes on 64-bit) per character? That's 1600% memory overhead. All to save a handful of string manipulations when an individual line changes.

Keep in mind that above and beyond the storage overhead, you are going to throw your cache coherency to hell and gone, and iterating over these strings (for example, to print them/output them to screen) is going to be dramatically more expensive.

Share this post


Link to post
Share on other sites
I googled a bit to find out how emacs does it and found this. The comment by The Stig at the end is particularly relevant.

There is some more detail here.

EDIT: For people too lazy to click on the links, what they do is use a contiguous array of characters with a gap somewhere in the middle. Insertions are done by moving the gap to the right place and writing at the beginning of the gap (so typing several contiguous characters is fast). If an edit happens in a new place, the gap needs to be moved first (apparently, this is fairly fast although it's O(n)). If the length of the gap becomes 0, the buffer is resized, and the gap is now at the end of the buffer. Edited by alvaro

Share this post


Link to post
Share on other sites

[quote name='slicer4ever' timestamp='1348054459' post='4981639']
hey good people of GameDev, i've been toying with the idea of using a double link list to contain a character array for fast insertion/change/modification of text with a text editor.

It sounds like you are talking about a linked list of characters (not a rope, which is a linked list of strings).

Are you really sure you want to absorb the cost of 2x pointers (8x2 = 16 bytes on 64-bit) per character? That's 1600% memory overhead. All to save a handful of string manipulations when an individual line changes.

Keep in mind that above and beyond the storage overhead, you are going to throw your cache coherency to hell and gone, and iterating over these strings (for example, to print them/output them to screen) is going to be dramatically more expensive.
[/quote]

yes, this is what i was talking about, rope sounds interesting, but I think it's a bit overkill for what i was thinking.

I realize the memory overhead is pretty big, but compared to modern ram size's, it's pretty much a non-issue unless I work on devices that have a small amount of ram.

I hadn't thought about losing cache coherency with the setup(I really need to learn more about optimizing against cpu cache's so i can look ahead for these potential mistakes). as for outputing, my initial thought about outputing to something that reaquires a character array(such as to the console), is that the linked list string(which just to be clear, is a link list of characters), would contain a function which write's the characters into a char buffer, which is provided by the application.

as for rendering, i would have expected it to be only slightly slower than my current method, since either way I have to iterate over the string, and generate the vertices/uv coord's from my font.

edit: just to be clear, cache coherency is where the processing can detect that it's receiving an array of elements, so it can store the array in it's local cache for faster processing correct?(also, doesn't this mean that anything that uses link lists is pretty much vulnerable to this?)

which if the latter is true, then much of my current work has been with link lists, so i'd hope i'm not creating poorly built programs=-(.

edit2:

[quote name='slicer4ever' timestamp='1348054459' post='4981639']
hey good people of GameDev, i've been toying with the idea of using a double link list to contain a character array for fast insertion/change/modification of text with a text editor.
currently my ui text entry input stores each line, and rebuilds the line when their's a change on a particular line, this is sufficient for my general needs, but i was thinking of using linked list's to store text, it'd also make cursor tracking much easier since it'd be just containing a pointer to the current char container.
so, any thoughts on the subject from anyone's who tried something similar?

Does what you're currently doing work?
Is this premature optimization?
Does the alternative add complexity or reduce complexity?
How maintainable is your alternative solution?
Whats the cost vs. benefit ratio for this effort?
I think I'm leaning towards "Don't fix it if it ain't broke!"
[/quote]
non of this is likely to go into code at the moment, i'm more just toying around with some potential ideas to use in the next iteration of my engine, but that won't be until i finish my current game, and wanted to get some opinion's on the idea. Edited by slicer4ever

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!