What you're asking for is a basic tree structure where each node can have any
number of sub-nodes. There should be two separate representations for this data
structure, one for use at run-time and one for saving to disk. The two
structures may well be the same structure, it depends on interoperability with
different applications etc.
Example: You may use XML to save data to disk. XML is very well suited to
handle structured data is an "interoperability format" - you may open the XML
file with some other programs. That doesn't mean you should use data in XML
format at run-time! XML is a text-based format (those in it's DOM form it might
be stored as a tree, not as text) and adding/deleting/changing things might not
be fast enough for run-time use.
Example(2): You may use plain text to save data to disk! Just indent every
heading with an correct number of tab chars. It can be done and it's easier for
humans to read the text. XML is supposed to be human-readable also, but I'd
never attempt reading an Word document saved as XML using Notepad!
As for the run-time structure, you'll probably need to create a purpose-built
data structure that's optimized for the kind of operations you'll do: Add,
Remove, Move, Change hierarchy level. If you need this to be fast you'll need
to forgo ready-made components like ClientDataSet and XML-stuff. Here's a short
snippet of code that shows you how it could be done. The code is long for an
email, skip to the end of the code for some extra comments:
<code>
unit UTextDataStructures;
interface
uses Contnrs;
type
TNodeOfText = class; // forward declaration so I can make a
// list of TNodeOfText before I finish
// declaring TNodeOfText
// List of text-nodes
TNodeOfTextList = class(TObjectList)
private
function GetNode(i: Integer): TNodeOfText;
public
procedure Add(N:TNodeOfText); // this is design to add type-safety to our
list
property Node[i:Integer]:TNodeOfText read GetNode;default; // this provides
easy access
end;
// Basic node of text; This is actually an tree!
TNodeOfText = class
public
BlockOfText:string;
SubNodes:TNodeOfTextList;
constructor Create;
destructor Destroy;override;
end;
implementation
{ TNodeOfTextList }
procedure TNodeOfTextList.Add(N: TNodeOfText);
begin
inherited Add(N);
end;
function TNodeOfTextList.GetNode(i: Integer): TNodeOfText;
begin
Result := TNodeOfText(Items[i]);
end;
{ TNodeOfText }
constructor TNodeOfText.Create;
begin
SubNodes := TNodeOfTextList.Create;
end;
destructor TNodeOfText.Destroy;
begin
SubNodes.Free;
inherited;
end;
end.
</code>
At first sight the code in this example is very simple (only 55 lines of code).
It provides you with everything you need to work with the hierarchy of text
headings, no matter how many levels it goes down. It doesn't need to explicitly
represent the hierarchy level because you can determine the level by walking
the tree from the first node to the given node. If you need to do this often
you may add a node for the "parent" so you can walk the tree from any node to
its parent. I would not add an explicit number for the hierarchy level because
moving an whole tree from one place to another would require re-numbering
everything in the moved tree.
Using the structure should be easy. If you need more information please ask.
--
Cosmin Prund
> -----Mesaj original-----
> De la: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] În
> numele Alan Colburn
> Trimis: Wednesday, September 24, 2008 3:18 AM
> Către: Borland's Delphi Discussion List
> Subiect: Data Structure for Sorting
>
> I wasn't really sure what to put in the subject line ... I'm trying to
> write
> a class which would let me mimic basic outlining features. As you know,
> an
> outline--the kind you used to use in school--has headings, sub-
> headings, and
> sometimes even third or fourth level sub-headings (sub-sub-headings and
> sub-sub-sub-headings). A basic OutlineEntry class, I would think, would
> have
> fields representing the text in an outline entry + fields representing
> the
> text's position within an outline hierarchy:
>
> I. Heading One
> --a. Sub-Heading One
> ----1. A second level subheading
> ----2. Another second level subheading
>
> My question is what data structure (and/or associated components)
> should I
> use to model an outline, given that users have to be able to insert new
> entries, delete entries, move entries up/down/in/out, etc. while
> updating
> all the positions for the rest of the OutlineEntry objects? It seems
> like
> there will be a lot of sorting going on. It also seems like there's
> probably
> some kind of ClientDataSet or XML "stuff" out there with build in
> methods
> that ought to be well suited for what I'm thinking about ... but I
> really
> have no idea where to start!
>
> Thanks, as always -- Al
>
> _______________________________________________
> Delphi mailing list -> [email protected]
> http://lists.elists.org/cgi-bin/mailman/listinfo/delphi
_______________________________________________
Delphi mailing list -> [email protected]
http://lists.elists.org/cgi-bin/mailman/listinfo/delphi