Jump to content

  • Log In with Google      Sign In   
  • Create Account


Thoughts on double link list strings?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
14 replies to this topic

#1 slicer4ever   Crossbones+   -  Reputation: 3226

Like
0Likes
Like

Posted 19 September 2012 - 05:34 AM

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?
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 7801

Like
0Likes
Like

Posted 19 September 2012 - 05:50 AM

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.

#3 Álvaro   Crossbones+   -  Reputation: 12020

Like
0Likes
Like

Posted 19 September 2012 - 08:01 AM

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.

#4 Brother Bob   Moderators   -  Reputation: 7801

Like
0Likes
Like

Posted 19 September 2012 - 08:30 AM

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.

#5 slayemin   Members   -  Reputation: 2055

Like
2Likes
Like

Posted 19 September 2012 - 10:19 AM

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!"

Eric Nevala

Currently a self-employed indie game dev


#6 Bregma   Crossbones+   -  Reputation: 4773

Like
0Likes
Like

Posted 19 September 2012 - 12:25 PM

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.
Stephen M. Webb
Professional Free Software Developer

#7 swiftcoder   Senior Moderators   -  Reputation: 9672

Like
1Likes
Like

Posted 19 September 2012 - 12:37 PM

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.

Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#8 Álvaro   Crossbones+   -  Reputation: 12020

Like
1Likes
Like

Posted 19 September 2012 - 12:59 PM

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, 19 September 2012 - 01:02 PM.


#9 slicer4ever   Crossbones+   -  Reputation: 3226

Like
0Likes
Like

Posted 19 September 2012 - 06:23 PM


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.


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:


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!"

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, 19 September 2012 - 06:32 PM.

Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

#10 Servant of the Lord   Crossbones+   -  Reputation: 17376

Like
0Likes
Like

Posted 19 September 2012 - 10:50 PM

[/editted out - it is offtopic, my bad. If curious, use the history button]

Edited by Servant of the Lord, 19 September 2012 - 11:39 PM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

[Fly with me on Twitter] [Google+] [My broken website]

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                       [Need free cloud storage? I personally like DropBox]

Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal


#11 Álvaro   Crossbones+   -  Reputation: 12020

Like
0Likes
Like

Posted 19 September 2012 - 11:10 PM

I'm not sure if I am the one being accused of not knowing what the STL is. In fact, I do know. I just thought that the statement that rope is part of the STL, although technically correct, might mislead people into thinking that it is part of the C++ standard library, because of the ambiguity Servant of the Lord described.

Anyway, the original topic is much more interesting than this side discussion. Has anybody know what other data structure serious text editors use? Is this contiguous-buffer-with-a-gap method the standard approach?

#12 slicer4ever   Crossbones+   -  Reputation: 3226

Like
0Likes
Like

Posted 20 September 2012 - 04:14 AM

Anyway, the original topic is much more interesting than this side discussion. Has anybody know what other data structure serious text editors use? Is this contiguous-buffer-with-a-gap method the standard approach?


Context is one of my favorites, and the source code is available to download, so it might not be a bad idea for me to check it out, although to be fair, i'm not particularly looking for heavy duty editor solutions, since most common usage of my UI Text editor is for changing some one line piece of information, or writing a description for something.


I was thinking that a link list of characters would be much faster on insertion/removal/modifications to the editor, since it comes down to changing but a few pointers around to do anything to the list. but i'm not certain if the overhead of cache corherency loss makes it worth it. perhaps in an enivronment like an stand-alone editor it wouldn't matter for cache corherency, since your not trying to run at any particular frame rate, but i'm not really sure if the overhead of insertion for my current method would outweigh the cost of what i lose by using a link list?
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

#13 GorbGorb   Members   -  Reputation: 112

Like
0Likes
Like

Posted 20 September 2012 - 04:33 AM

If you really have to optimize your solution, I'd suggest this approach:

Organizing memory
Use fixed size memory blocks of chars as nodes in your rope. This allows you to pool allocate those blocks, minimizing calls to new and malloc.
When you run out of allocated blocks, allocate a bigger buffer. Don't simply memcpy your blocks to the new location but push_back each character in your new structure, filling up each block completely, starting at the beginning of the buffer (thus making the doubly linked list cache friendly again, in case you want to traverse it).

Finding insert position
For inserting text into the string, I assume you need to know where a line starts. I see multiple solutions for this:
a) Store the number of newlines a node contains in the node (and keep that number updated). Iterate over nodes to find the right block.
  • easy to implement
  • low memory usage
  • inserting newlines is O(1)
  • finding newlines is O(n) and needs to touch a lot of memory
b) Maintain a dynamic array of pointers to nodes. The pointer at index n shall point to the node containing the line with index n
  • higher memory usage
  • inserting newlines is O(n)
  • finding newlines is O(1)
c) Combination of a) and b). Use pointers to find approximate location of your line (for example, pointer n points to line 10 * n).
  • most difficult to implement
  • otherwise, same characteristics as b)

Edited by GorbGorb, 20 September 2012 - 04:39 AM.


#14 Álvaro   Crossbones+   -  Reputation: 12020

Like
1Likes
Like

Posted 20 September 2012 - 09:01 AM

A more practical balance between a wasteful doubly-linked list of characters and a complicated rope could be std::list<std::string> (a list of lines). That's probably where I would start, until it becomes clear that it's not good enough.

#15 slicer4ever   Crossbones+   -  Reputation: 3226

Like
0Likes
Like

Posted 23 September 2012 - 02:40 AM

A more practical balance between a wasteful doubly-linked list of characters and a complicated rope could be std::list<std::string> (a list of lines). That's probably where I would start, until it becomes clear that it's not good enough.

yes, a link list of char lines is probably a pretty decent comprise, fast new line insertion, but same char speed insertion as my current implementation i do like it.

If you really have to optimize your solution, I'd suggest this approach:

Organizing memory
Use fixed size memory blocks of chars as nodes in your rope. This allows you to pool allocate those blocks, minimizing calls to new and malloc.
When you run out of allocated blocks, allocate a bigger buffer. Don't simply memcpy your blocks to the new location but push_back each character in your new structure, filling up each block completely, starting at the beginning of the buffer (thus making the doubly linked list cache friendly again, in case you want to traverse it).

if i'm pushing back into an array, arn't i pretty much memcpy'ing one array into another, because each char is in an array i can't see how i maintain the double link list for individual characters, but i might be misunderstanding what your saying?.


I think in the end, i'll probably use alvaro's implementation when i do get around to rebuilding the component, thanks for the helpful input guys=-)
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS