home *** CD-ROM | disk | FTP | other *** search
- {
-
- flex.dem
- 4-25-90
-
- Copyright 1990
- John W. Small
- All rights reserved
-
- PSW / Power SoftWare
- P.O. Box 10072
- McLean, Virginia 22102 8072
- (703) 759-3838
-
- }
-
- program flexDemo;
-
- uses crt, flex;
-
- const
-
- iarray : array[1..10] of integer = (
- 1,2,3,4,5,6,7,8,9,10);
-
- type
-
- iarrayType = array[1..maxInt] of integer;
-
- var Q, S: FlexList;
- i, r : integer;
- iptr : ^integer;
- line : string;
- iaptr : ^iarrayType;
-
- {$F+}
- function CompareIntegers(var buf1, buf2) : integer; {$F-}
- var i1 : integer absolute buf1;
- i2 : integer absolute buf2;
- begin
- if i1 < i2 then
- CompareIntegers := -1
- else if i1 > i2 then
- CompareIntegers := 1
- else
- CompareIntegers := 0
- end;
-
- begin
- clrscr;
- writeln('It''s best to have a printed copy of flex.pas to read');
- writeln('along with this demo. Press ''Enter'' to continue.');
- readln;
- clrscr;
- writeln('Anytime your Turbo Pascal 5.5 application requires a');
- writeln('stack, queue, or linked list, simply include "flex"');
- writeln('in its "uses" clause. Next declare your stack, queue,');
- writeln('or list as a variable of type "FlexList". FlexList');
- writeln('is really a generic hybrid stack-queue-list-array');
- writeln('data structure. Your FlexList variable can be');
- writeln('initialized to store any type of data.');
- writeln;
- writeln('For example:');
- writeln;
- writeln(' var Q : FlexList;');
- writeln(' i, r : integer;');
- writeln;
- writeln(' begin');
- writeln(' Q.init(sizeof(r));');
- writeln;
- writeln('In the above example, r could have been any type, record');
- writeln('or object, with any number of fields of any type.');
- writeln;writeln('Press ''Enter'' to continue ... ');
- readln;
- clrscr;
- writeln('The FlexList object has over 30 methods for accessing your');
- writeln('"list" as a stack, queue, list, or an array');
- writeln('interchangeably! You can push, pop, insert, delete,');
- writeln('sort, store, recall, or find, to name but just a few');
- writeln('operations. The complement of FlexList methods allows you');
- writeln('to access your list''s data by value (copy) or by');
- writeln('reference (pointer), as well as to move nodes directly');
- writeln('between lists.');
- writeln;
- writeln('Consider the following code segment.');
- writeln;
- writeln(' { Insert 10 random integers into a queue. }');
- writeln;
- writeln(' for i := 1 to 10 do begin');
- writeln(' r := random(10000);');
- writeln(' write(r:8);');
- writeln(' Q.insQ(r)');
- writeln(' end;');
- writeln;
- writeln('Press ''Enter'' to execute the above code segment.');
- readln;
- Q.init(sizeof(r));
- for i := 1 to 10 do begin
- r := random(10000);
- write(r:8);
- Q.insQ(r)
- end;
- writeln;
- writeln('Press ''Enter'' to continue ... ');
- readln;
- clrscr;
- writeln('Next, consider this code segment: ');
- writeln;
- writeln(' {$F+}');
- writeln(' function CompareIntegers(var buf1, buf2) : integer; {$F-}');
- writeln(' var i1 : integer absolute buf1; i2 : integer absolute buf2;');
- writeln(' begin');
- writeln(' if i1 < i2 then');
- writeln(' CompareIntegers := -1');
- writeln(' else if i1 > i2 then');
- writeln(' CompareIntegers := 1');
- writeln(' else');
- writeln(' CompareIntegers := 0');
- writeln(' end;');
- writeln;
- writeln(' { Sort the queue and pop the results. }');
- writeln;
- writeln(' Q.sort(CompareIntegers); ');
- writeln(' Q.pop(r);');
- writeln(' while Q.ok do begin');
- writeln(' write(r:8); Q.pop(r) end;');
- writeln;
- writeln('Press ''Enter'' again to execute this code.');
- readln;
- clrscr;
- writeln('First the random integers again ... ');
- writeln;
- while Q.next(r) do
- write(r:8);
- writeln;
- writeln('And the sorted results ... ');
- writeln;
- Q.sort(CompareIntegers);
- Q.pop(r);
- while Q.ok do begin
- write(r:8);
- Q.pop(r);
- end;
- writeln;
- writeln;
- writeln('FlexList methods that perform sorts or searches take a procedure');
- writeln('type parameter. This "pointer" is to the compare function which');
- writeln('compares the data stored in FlexList nodes to determine the sorted');
- writeln('ascending order. Your compare function decides which fields to');
- writeln('compare!');
- writeln;
- writeln('Press ''Enter'' to continue ... ');
- readln;
- clrscr;
- writeln('All FlexList methods ending in ''D'' access the data in your');
- writeln('list by pointer. Again, consider the following code.');
- writeln;
- writeln(' var iptr : ^integer;');
- writeln(' ...');
- writeln(' for i := 1 to 10 do begin');
- writeln(' write(i:8); Q.push(i) end;');
- writeln(' while Q.nextD(pointer(iptr)) do');
- writeln(' write(iptr^:8);');
- writeln;
- writeln('Press ''Enter'' to view the results.');
- readln;
- writeln('First the order of integers pushed onto the stack ...');
- writeln;
- for i := 1 to 10 do begin
- write(i:8);
- Q.push(i)
- end;
- writeln;
- writeln('And then reading from the top of stack back ...');
- writeln;
- while Q.nextD(pointer(iptr)) do
- write(iptr^:8);
- writeln;
- writeln;
- writeln('Press ''Enter'' to continue ... ');
- readln;
- clrscr;
- writeln('Let''s read across that last stack again but this time');
- writeln('using FlexList''s array access methods! And the code ...');
- writeln;
- writeln(' for i := 1 to 10 do begin');
- writeln(' Q.recall(r,i); write(r:8) end;');
- writeln;
- writeln('And of course the results ...');
- writeln;
- for i := 1 to 10 do begin
- Q.recall(r,i); write(r:8) end;
- writeln;
- writeln;
- writeln('Press ''Enter'' to continue ... ');
- readln;
- clrscr;
- writeln('All FlexList methods ending in ''N'' operate on FlexNodes instead of the');
- writeln('data in the nodes. These come in handy when you are writing queuing');
- writeln('network simulations and the like, where you want to be constantly moving');
- writeln('nodes between queues. A multitasking OS simulation is a good example');
- writeln('where you move PCBs (process control blocks) between the run, ready,');
- writeln('blocked, and swapped out queues. Let''s see, that last queue with 10');
- writeln('integers is still in memory, we''ll work on that one. Consider the');
- writeln('following code ...');
- writeln;
- writeln(' var S : FlexList;');
- writeln(' ...');
- writeln(' S.init(sizeof(integer));');
- writeln(' while S.ok do S.pushN(Q.popN);');
- writeln(' while S.Prev(i) do write(i:8);');
- writeln(' Q.done; S.done;');
- writeln;
- writeln('And the results ...');
- writeln;
- S.init(sizeof(integer));
- while S.ok do S.pushN(Q.popN);
- while S.Prev(i) do write(i:8);
- Q.done; S.done;
- writeln;
- writeln('Wow! We popped one stack into the other. That should have reversed the');
- writeln('order. But wait, we then read the list backward with the prev() method.');
- writeln('That''s right, everything''s okay. Press ''Enter'' to continue ... ');
- readln;
- clrscr;
- writeln('You may have noticed Q.done and S.done in the last example. Whenever you');
- writeln('are done with a FlexList your should call this method to cleanup. If you were');
- writeln('really watching closely during the last few examples, you probably wondered');
- writeln('about how prev() and nextD() knew where in the list they were. All FlexLists');
- writeln('maintain a pointer to the current node. Stack and queue methods don''t');
- writeln('disturb this setting. If the current is popped, however, the current node');
- writeln('becomes undefined just as it is after FlexList initialization. Next and prev');
- writeln('"walk" the current pointer across the list, making the current become');
- writeln('undefined once each cycle at which time these methods return false. This');
- writeln('is how I was able to control looping through the lists in the previous');
- writeln('examples. Array access methods, i.e. recall and store, set the current');
- writeln('pointer to the last node accessed. The next array access first determines');
- writeln('whether the front, current, or rear pointer is closest to the requested');
- writeln('node and then traverses from the closest pointer across the links to the');
- writeln('requested node, making it the new current node. Both methods call the');
- writeln('mkcur method to perform this operation. FlexList methods, get and put, also');
- writeln('work with the current node. The insert method inserts after the current');
- writeln('node making the new node current, while del, deletes the current node');
- writeln('making the previous node current.');
- writeln;
- writeln('Press ''Enter'' to continue ...');
- readln;
- clrscr;
- writeln('Let''s see insert and del in action and this time with something other');
- writeln('than integers! Consider the following code and then start your input.');
- writeln;
- writeln(' Q.init(sizeof(line));');
- writeln(' while Q.nodes < 3 do begin');
- writeln(' write(''Enter String : ''); readln(line); Q.ins(line) end;');
- writeln;
- Q.init(sizeof(line));
- while Q.nodes < 3 do begin
- write('Enter String : '); readln(line); Q.ins(line) end;
- writeln;
- writeln('The last node is now the current one and we''ll start deleting them.');
- writeln('Since deleting makes the previous node the new current node, successive');
- writeln('deletes will walk across the list from the rear to the front.');
- writeln('The following code will now be executed.');
- writeln;
- writeln(' Q.del(line);');
- writeln(' while Q.ok do begin writeln(line); Q.del(line) end;');
- writeln;
- Q.del(line);
- while Q.ok do begin writeln(line); Q.del(line) end;
- writeln;
- writeln('Press ''Enter'' to continue ...');
- readln;
- clrscr;
- writeln('Sometimes you want to work with a list, other times it''s more convenient');
- writeln('to work with an array. Although Flexlist allows this chameleon behavior,');
- writeln('your application may progress in stages that favor a list at one point');
- writeln('and an array at another. The FlexList object has methods for converting a');
- writeln('conventional array into a FlexList object or a FlexList object into a dynamic');
- writeln('array thus allowing you to optimize the performance of your application.');
- writeln('You can think of the FlexList method, unpack, as exploding a conventional');
- writeln('array into a FlexList. Consider the following example.');
- writeln;
- writeln(' const iarray : array[1..10] of integer = (1,2,3,4,5,6,7,8,9,10);');
- writeln(' ...');
- writeln(' Q.unpack(sizeof(integer),10,iarray);');
- writeln(' while Q.next(i) do write(i:8);');
- writeln;
- writeln('And the results ...');
- writeln;
- Q.unpack(sizeof(integer),10,iarray);
- while Q.next(i) do write(i:8);
- writeln;
- writeln('Press ''Enter'' to continue ...');
- readln;
- clrscr;
- writeln('Think of the FlexList method, pack, as imploding a FlexList into a dynamic');
- writeln('array. Consider the following code and the previous list of integers.');
- writeln;
- writeln(' type iarrayType = array[1..maxInt] of integer;');
- writeln(' ...');
- writeln(' iaptr := Q.pack;');
- writeln(' for i := 1 to Q.nodes do');
- writeln(' write(iaptr^[i]:8);');
- writeln;
- writeln('And the results of its execution ...');
- writeln;
- iaptr := Q.pack;
- for i := 1 to Q.nodes do
- write(iaptr^[i]:8);
- writeln;
- writeln;
- writeln('The FlexList method, packPtrs, simply creates a nil terminated array of');
- writeln('packed pointers which point to the data areas of all the FlexList nodes.');
- writeln('Do you remember how FlexList methods ending in ''D'' worked with pointers?');
- writeln('These are the same pointers all packed into a dynamic array. You can');
- writeln('quickly zip around a FlexList''s nodes using these pointers to modify data.');
- writeln('When your application is finished this phase of processing it discards');
- writeln('the array of pointers.');
- writeln;
- writeln('Press ''Enter'' to continue ...');
- readln;
- clrscr;
- writeln('Since FlexList is an object, you can derive your own objects from FlexList.');
- writeln('Why would you want too? Suppose that you need a place to store data');
- writeln('pertaining to the whole list? You can declare a new object, derived');
- writeln('from FlexList, that contains variables for this data. Thus you can store');
- writeln('data associated with the list, in the list!');
- writeln;
- writeln('Your application''s code size won''t continue to grow when you add new');
- writeln('types of lists either, since the FlexList object is generic, able to store');
- writeln('any type of data, record or object. Since FlexLists are initialized at');
- writeln('run time, the data they hold or even their creation can be specified');
- writeln('at run time thus allowing your application new dimensions of adaptibility');
- writeln('to user inputs. And with FlexList you can quickly construct');
- writeln('lists of lists or other composite structures. FlexList will save you');
- writeln('hours of coding time, code space, money and headaches!');
- writeln;
- writeln('Press ''Enter'' to continue ...');
- readln;
- clrscr;
- writeln('This demo, flex.int, and flex.tpu are offered as shareware, meaning try');
- writeln('before you buy. If you find FlexList useful and are using it in your');
- writeln('applications, a registration fee of $20 is required. Upon registration');
- writeln('you will be sent manual, source code and the latest examples');
- writeln('programs on a DOS formatted 5 1/4" disk.');
- writeln;
- writeln('Copyright 1990, John W. Small, All right reserved');
- writeln;
- writeln('PSW / Power SoftWare');
- writeln('P.O. Box 10072');
- writeln('McLean, Virginia 22102 8072');
- writeln('(703) 759-3838');
- writeln;
- writeln('PolyList, a close cousin of FlexList with nodes that are polymorhpic');
- writeln('objects, is now available for Turbo Pascal 5.5. Call or write or');
- writeln('check your favorite BBS for details.');
- writeln;
- writeln('Press enter to quit.');
- readln;
- end.