• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
slicer4ever

Thoughts on double link list strings?

14 posts in this topic

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?
0

Share this post


Link to post
Share on other sites
The STL has a class called [i]rope[/i] 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.

[url="http://www.sgi.com/tech/stl/index.html"]Clicky[/url] to STL.
[url="http://www.sgi.com/tech/stl/ropeimpl.html"]Clicky[/url] 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.
0

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.
0

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 [i]the[/i] 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.
0

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.

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?
[/quote]

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

Share this post


Link to post
Share on other sites
[quote name='alvaro' timestamp='1348063295' post='4981682']
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.
[/quote]
"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.
0

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.[/quote]
It sounds like you are talking about a linked list of [b]characters[/b] (not a rope, which is a linked list of [b]strings[/b]).

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.
1

Share this post


Link to post
Share on other sites
I googled a bit to find out how emacs does it and found [url="http://curiousprogrammer.wordpress.com/2010/09/28/emacs-buffer-datatype/"]this[/url]. The comment by The Stig at the end is particularly relevant.

There is some more detail [url="http://www.xemacs.org/Documentation/21.5/html/internals_25.html"]here[/url].

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
1

Share this post


Link to post
Share on other sites
[quote name='swiftcoder' timestamp='1348079870' post='4981775']
[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.[/quote]
It sounds like you are talking about a linked list of [b]characters[/b] (not a rope, which is a linked list of [b]strings[/b]).

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='slayemin' timestamp='1348071555' post='4981732']
[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?
[/quote]
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
0

Share this post


Link to post
Share on other sites
[b][/editted out - it [u]is[/u] offtopic, my bad. If curious, use the history button][/b] Edited by Servant of the Lord
0

Share this post


Link to post
Share on other sites
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?
0

Share this post


Link to post
Share on other sites
[quote name='alvaro' timestamp='1348117811' post='4981922']
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?
[/quote]

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?
0

Share this post


Link to post
Share on other sites
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.[list]
[*]easy to implement
[*]low memory usage
[*]inserting newlines is O(1)
[*]finding newlines is O(n) and needs to touch a lot of memory
[/list]
b) Maintain a dynamic array of pointers to nodes. The pointer at index [i]n [/i]shall point to the node containing the line with index [i]n[/i][list]
[*]higher memory usage
[*]inserting newlines is O(n)
[*]finding newlines is O(1)
[/list]
c) Combination of a) and b). Use pointers to find approximate location of your line (for example, pointer [i]n [/i]points to line [i]10 * n[/i]).[list]
[*]most difficult to implement
[*]otherwise, same characteristics as b)
[/list] Edited by GorbGorb
0

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites
[quote name='alvaro' timestamp='1348153289' post='4982053']
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.
[/quote]
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.

[quote name='GorbGorb' timestamp='1348137180' post='4981980']
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).
[/quote]
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=-)
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0