Xref: utzoo comp.emacs:3695 comp.editors:217
Path: utzoo!utgpu!water!watmath!clyde!bellcore!rutgers!njin!princeton!udel!rochester!cornell!blandy
From: blandy@marduk.cs.cornell.edu (Jim Blandy)
Newsgroups: comp.emacs,comp.editors
Subject: Buffer data structures
Summary: What's your favorite?
Keywords: data structures, buffers
Message-ID: <18612@cornell.UUCP>
Date: 26 Jun 88 18:13:10 GMT
Sender: nobody@cornell.UUCP
Reply-To: blandy@cs.cornell.edu (Jim Blandy)
Followup-To: comp.emacs
Organization: Cornell Univ. CS Dept, Ithaca NY
Lines: 30

Okay everyone - I'm sick and tired of the old linked-list-of-lines
and array-of-pointers-to-lines tricks - I've been trying to think of
a new, creative data structure where insertion is free, block copies
are minimal, memory isn't wasted, and the buffalos roam free (or are
they GNUs?)  What's the best data structure you've come up with
(or heard of) for representing a text buffer?  Does it implement true
markers (you set them, they stay with their text as things move around)?
If a big file shrinks, will the space be returned to the free pool?

My favorite is a combination of the 'gap-in-the-middle' approach
and the linked list approach.  In the gap-in-the-middle approach,
the buffer is one contiguous block of memory,  where the unused
portion acts as a gap for insertion and deletion; you move the gap
by shifting characters from one side of it to the other.  Once you've
got the gap where you want it, insertion is as easy as storing the
characters and shrinking the gap; to delete, enlarge the gap to enclose
the characters you want gone.  (I got this approach from an editor
published in some computer magazine WAY BACK.  Byte, maybe.)  The
problem comes when the gap gets filled in; if you've got the whole address
space to yourself, you just tell the user "tough luck," but if you're
in a multitasking environment, you need to ask for more space;  I'd
make a linked list of blocks, each with its own gap.  If a block
is empty, you free its space.

Okay, now I've given you my favorite data structure.  Feel free to use
it if you like.  Now what's your favorite?  If you'd get fired for
revealing it, don't bother, but if you can, post a quick description.
--
Jim Blandy - blandy@crnlcs.bitnet
"insects were insects when man was just a burbling whatsit."  - archie