Implement the tmylist. Add function.
The add function in tlist uses a grow method. The principle is that the more elements, the more memory will be prepared for the future. Here we omit the memory reserved for four elements;
The add function in tlist also triggers a notify method, which should be prepared for their subclass (it is estimated that it is used to stimulate an event), and no more.
Function tmylist. add (item: pointer): integer; begin {if the pre-applied memory is used up, apply for four more elements} If fcount = fcapacity then setcapacity (fcapacity + 4 ); {Add is added at the end, and the pointer is assigned to the array} flist ^ [fcount]: = item; {function return} result: = fcount; {total number of elements + 1} Inc (fcount); end;
Then implement the tmylist. Delete process.
Previously, the error handling is simplified to an exception, and the touch on the notify method is also omitted.
The use of system. Move, can refer to: http://www.cnblogs.com/del/archive/2008/03/27/1126226.html
Here, we need to pay attention to the following issue: When a tlist deletes an element (its element is a pointer), it does not release the object pointed to by the pointer, but is evicted from the list;
To free up objects at the same time, use the tobjectlist class under the contnrs unit.
Procedure tmylist. delete (Index: integer); begin {if the given index does not meet the requirements, an exception is thrown.} If (Index = fcount) then raise exception. createfmt ('invalid index: % d', [Index]); {the deletion process is to move all elements following the current element forward to a position} If Index
The tmylist. setcount method is also implemented.
Previously, I did not think that the Count attribute is writable. This can be tough. For example, if there are already 100 elements, If you want count: = 1, delete the next 99 elements!
Another thing I don't understand is: for example, if there are already 100 elements and count: = 200, isn't the last 100 elements even filled with null characters? I think it is inappropriate. But this is the case now.
Procedure tmylist. setcount (const value: integer); var I: integer; begin {if the parameter is invalid, an exception is thrown.} If (value maxlistsize) then raise exception. createfmt ('invalid data: % d', [value]); {apply immediately if the reserved memory is insufficient.} If value> fcapacity then setcapacity (value ); {if the parameter is greater than the total number of elements, do not assign a value.} {if the parameter is smaller than the total number of elements, delete extra elements.} If value> fcount then fillchar (flist ^ [fcount], (value-fcount) * sizeof (pointer), 0) else for I: = fcount-1 downto value do Delete (I); {Total number of new elements} fcount: = value; end;
There is also a tmylist. Clear method.
Because the release of objects in the list is much simpler.
procedure TMyList.Clear;begin SetCount(0); SetCapacity(0);end;
So far, all declared methods have been implemented, and the tmylist class should be used together.
The source code is as follows:
Unit mylist; interfaceuses sysutils; const maxlistsize = maxint Div 16; Type ppointerlist = ^ tpointerlist; tpointerlist = array [0 .. maxlistsize-1] of pointer; tmylist = Class (tobject) Private flist: ppointerlist; fcount: integer; fcapacity: integer; Procedure setcapacity (const value: integer ); procedure setcount (const value: integer); Public destructor destroy; override; function add (item: pointer): integer; Procedure clear; Procedure Delete (Index: integer); property capacity: integer read fcapacity write setcapacity; property count: integer read fcount write setcount; property list: ppointerlist read flist; end; implementation {tmylist} function tmylist. add (item: pointer): integer; begin if fcount = fcapacity then setcapacity (fcapacity + 4); flist ^ [fcount]: = item; Result: = fcount; INC (fcount); end; Procedure tmylist. clear; begin setcount (0); setcapacity (0); end; Procedure tmylist. delete (Index: integer); begin if (Index = fcount) then raise exception. createfmt ('invalid index: % d', [Index]); If index maxlistsize) then raise exception. createfmt ('invalid data: % d', [value]); If fcapacity value then begin reallocmem (flist, value * sizeof (pointer); fcapacity: = value; end; end; Procedure tmylist. setcount (const value: integer); var I: integer; begin if (value maxlistsize) then raise exception. createfmt ('invalid data: % d', [value]); If value> fcapacity then setcapacity (value); If value> fcount then fillchar (flist ^ [fcount], (value-fcount) * sizeof (pointer), 0) else for I: = fcount-1 downto value do Delete (I); fcount: = value; end.