Skip to main content
Inside a Text Editor

Ever since my college days, after dabbling with vi and a few other editors, I always had an yearning to create my own. Now, I am still stuck with XEmacs and jEdit and had a chance to compile / study the sources and documentation of EMACS and a free editor component called Scintilla.

Until now, I was under the the belief that text editors used a doubly linked list to represent the text in memory. The advantages of this approach being insertions and deletions are much more easier which is just a matter of just un-linking a node off the list. But the shortcomming is that they tend to fragment memory with each node or line take a bit of memory.

The other alternative approach is to have a dynamic array which is a contiguous space of memory and can sometimes be directly written off to a file. The disadvantages are that insertion and deletion are costly and you need to reallocate quite frequently.

While goint throug the source and documentation of text editors, I chanced upon the Buffer Gap algorithm that provides the benefits of a contiguous space and yet minimizes the impact of insertions or deletions.

The algorithm is simple where the text is represented as an array of characters. There is a gap in the array that can be moved or repositioned. Every time a few new characters are inserted, the gap is moved to the location where the characters are to be inserted and it is just a matter of using the gap as a buffer to insert the new set of characters. The end result is, the gap size has reduced. Deletion is a similar affair where the gap is simply extended to include the area that needs to be deleted.

Wikipedia has a short description of the same algorithm.

Comments

Shoban Jayaraj said…
I had created my one generic version of the buffer gap algorithm. But also chanced upon a similar implementation at http://www.codeproject.com/KB/recipes/GenericGapBuffer.aspx just now...

Popular posts from this blog

Battle of Wesnoth Been on the lookout for a free turn based strategy game and chanced upon the Battle of Wesnoth . Despite it being an open source game (meaning, you get the source), it was incredibly polished akin to any of the other turn based strategy game (Alpha Centauri), be it the background score or the graphics or the tutorials. The game itself is set in a period similar to the D&D or nethack era. For the film buffs, if you have read or seen the Lord of the Rings, you would probably be able to relate to the clans that populate the game world. The game play, as with any turn based strategy game requires background information on each of the units that you own, their strengths and weaknesses and a lot of planning (a kin to chess, but with a lot more parameters) where factors like day - night cycles are taken into account (e.g, humans fight well during the day, but the orcs are better during the night). It is encouraged to keep your older units as they gain experience and beco
Helmet and seat-belt mandated at TN Today morning, I was pleasantly surprised to see most bikers (along with pillion riders) traveling with their helmets on as TN geared itself from the 1st of June to make it mandatory to wear helmets for bikers and seat belts (for the front seat passengers) for car commuters. The FM radio RJs had been conducting on-the-road commentaries, catching helmet-less riders seeking justification on why they had not yet acquired one (the helmet) yet. The shamed few who got caught eventually were presented with a helmet. One the way (near Tirumangalam), I couldn't help notice people flocking near put-up helmet shops on the sidewalks, trying to acquire the license to ride a bike! Let's hope the momentum carries on...

WiFi atlast

After numerous hours of tweaking, I resolved the problem with my WG311v3 NetGear PCI card. The problem? The router was too far away! Now that I am online using my Linux box, here are a few screen-shots of my desktop! The next time your supported WiFi card acts up, you know what to do... For those inquisitive lot, I am currently using the XP drivers with ndiswrapper.