Learning the implementation of the tlist class [5]

Source: Internet
Author: User
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.
 
  
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.