lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 2010-03-01 17:35, Gavin Wraith wrote:
If Lua is to be embedded in such an application
it would be nice to use the memory allocation that comes with Lua.
In that case one might use two lists, one indexing forward from the first
character to the last before the cursor, the other indexing backwards
from the last character of the text to the one immediately following the
cursor. Moving the cursor then means sloshing top elements from one
list to the other.

For WordGrinder I stored a simple array of paragraphs, each paragraph being an array of words. The cursor was a (paragraph, word, offset) tuple.

Redrawing the screen involves flowing the current paragraph, drawing it in the right place, and then working up and down until we reach the edges of the screen (yes, that's why the cursor is always in the middle). As paragraph caches its flowed data and only updates it on demand, to avoid the cost of having to reflow the entire document if the width of the screen changes.

Performance is adequate but not brilliant --- it's not usable on low-end machines (266MHz ARM, 300MHz MIPS). I was really aiming at machines on the order of my 600MHz eee, which it works fine on.

Why did I do it like that? I decided against lists because in Lua, being interpreted, you can consider any built-in operation to be O(1). So insertion into an array is cheap. (Particularly since the paragraph/word structure means the array sizes are quite small.) Plus, arrays are far more natural in Lua than lists, and the navigation code was going to ba a huge mess of special cases anyway, so I wanted to keep things straightforward.

Well, that's my excuse. The *real* reason is that I violated my cardinal rule and failed to read the literature on how to do it properly. But don't tell anyone...

--
┌─── dg@cowlark.com ───── http://www.cowlark.com ─────
│
│ life←{ ↑1 ⍵∨.^3 4=+/,¯1 0 1∘.⊖¯1 0 1∘.⌽⊂⍵ }
│ --- Conway's Game Of Life, in one line of APL