Algorithm
12-31-2002, 01:31 AM
Update: Added .slice() method.
The CharList class implements a string as a doubly linked list, so that its constituent characters can be changed or moved about with ease, and without having to process the entire string. It's ideal for complex parsing, as well as for simple encryption (text scrambling and the like).
CharList is open-source freeware, so use it as you like. I welcome any comments, criticisms, or improvements you may have to offer.
A brief summary of the class's properties follows.
_________________________________________________
CharList(txt) -- Constructor. If something other than a string is passed to it, it will be converted to a string and processed.
.Node(chr, [next=null], [prev=null]) -- Subclass constructor. Holds a single character and pointers to neighbors.
.Node.chr -- The character held by the node.
.Node.next -- The following node.
.Node.prev -- The preceding node.
.Node.go(i) -- Finds the node with (i) distance from the current node. Example: node.go(-2) == node.prev.prev
.Node.toString() -- Returns the value of chr.
.head -- Beginning of the list.
.tail -- End of the list.
.append(txt, [node=tail]) -- Inserts the given text, node, or CharList into the list after the specified node.
.find(chr, [node=head]) -- Finds the first instance of chr in the list, starting with node and moving forward. Returns the node found.
.findLast(chr, [node=tail]) -- Finds the first instance of chr in the list, starting with node and moving backward. Returns the node found.
.indexOf(node) -- Returns the index of the given node, or -1 if it is not found.
.length() -- Returns the length of the list. This value is calculated dynamically, so it is advised not to employ it directly in loops.
.nodeAt(idx) -- Returns the node at the specified index, or null if no such node exists.
.prepend(txt, [node=head]) -- Inserts the given text, node, or CharList into the list before the specified node.
.remove(node) -- Removes the specified node from the list.
.slice(start, end) -- Returns a substring of the list, beginning at the given node or index and continuing until the end node or index is encountered.
.substr(start, length) -- Returns a substring of the list, beginning at the given node or index.
.toString() -- Rebuilds the list into a string form.
_________________________________________________
Enjoy!
The CharList class implements a string as a doubly linked list, so that its constituent characters can be changed or moved about with ease, and without having to process the entire string. It's ideal for complex parsing, as well as for simple encryption (text scrambling and the like).
CharList is open-source freeware, so use it as you like. I welcome any comments, criticisms, or improvements you may have to offer.
A brief summary of the class's properties follows.
_________________________________________________
CharList(txt) -- Constructor. If something other than a string is passed to it, it will be converted to a string and processed.
.Node(chr, [next=null], [prev=null]) -- Subclass constructor. Holds a single character and pointers to neighbors.
.Node.chr -- The character held by the node.
.Node.next -- The following node.
.Node.prev -- The preceding node.
.Node.go(i) -- Finds the node with (i) distance from the current node. Example: node.go(-2) == node.prev.prev
.Node.toString() -- Returns the value of chr.
.head -- Beginning of the list.
.tail -- End of the list.
.append(txt, [node=tail]) -- Inserts the given text, node, or CharList into the list after the specified node.
.find(chr, [node=head]) -- Finds the first instance of chr in the list, starting with node and moving forward. Returns the node found.
.findLast(chr, [node=tail]) -- Finds the first instance of chr in the list, starting with node and moving backward. Returns the node found.
.indexOf(node) -- Returns the index of the given node, or -1 if it is not found.
.length() -- Returns the length of the list. This value is calculated dynamically, so it is advised not to employ it directly in loops.
.nodeAt(idx) -- Returns the node at the specified index, or null if no such node exists.
.prepend(txt, [node=head]) -- Inserts the given text, node, or CharList into the list before the specified node.
.remove(node) -- Removes the specified node from the list.
.slice(start, end) -- Returns a substring of the list, beginning at the given node or index and continuing until the end node or index is encountered.
.substr(start, length) -- Returns a substring of the list, beginning at the given node or index.
.toString() -- Rebuilds the list into a string form.
_________________________________________________
Enjoy!