Implement tmylist. setcapacity first.
I will think of the following soonCode:
Procedure tmylist. setcapacity (const value: integer); begin if fcapacity value then fcapacity: = value; end;
However, this is far from enough. The key is to allocate memory, such:
Reallocmem (array start point pointer, number of elements * element size );
The starting pointer of the array is flist; the number of elements is the setcapacity parameter: value; the element is the pointer, And the Win32 pointer is 4 bytes in size, so it can be written:
Reallocmem (flist, value * 4 );
Will the pointer in win64 be 4 bytes? It is more secure to write as follows:
Reallocmem (flist, value * sizeof (pointer ));
Rewrite the method:
Procedure tmylist. setcapacity (const value: integer); begin if fcapacity value then begin reallocmem (flist, value * sizeof (pointer); fcapacity: = value; end;
This is not enough. What if the value set by the user is greater than the maxlistsize of the list? If it is smaller than the current number of elements, it cannot be rewritten:
Procedure tmylist. setcapacity (const value: integer); begin if (value maxlistsize) Then exit; If fcapacity value then begin reallocmem (flist, value * sizeof (pointer); fcapacity: = value; end; end;
Inexplicably, exit will make people confused and throw an exception (the uses sysutils unit is required to use the exception class), for example:
If (value maxlistsize) then raise exception. Create ('invalid data ');
Specifically, let the exception tell you what the illegal data is:
If (value maxlistsize) then raise exception. createfmt ('invalid data: % d', [value]);
Tlist makes the thrown exception into an error method. For the moment, let's rewrite the method as follows:
Procedure tmylist. setcapacity (const value: integer); begin if (value maxlistsize) then raise exception. createfmt ('invalid data: % d', [value]); If fcapacity value then begin reallocmem (flist, value * sizeof (pointer); fcapacity: = value; end; end;
The tmylist. setcapacity method is complete. The complete code is:
Unit mylist; interfaceuses sysutils; {exception class exception declaration in sysutils unit} 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; beginend; Procedure tmylist. clear; beginend; Procedure tmylist. delete (Index: integer); beginend; destructor tmylist. destroy; begin clear; inherited; end; Procedure tmylist. setcapacity (const value: integer); begin if (value 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); beginend; end.