Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Geometria - Generico.pas

Generico.pas

Caricato da: Poggi Marco
Scarica il programma completo

  1. unit generico;
  2.  
  3. {$mode objfpc}
  4. {$INTERFACES CORBA}
  5.  
  6. interface
  7. uses sysutils,eccezioni;
  8.  
  9. type generic tipoDato<Tipo>=record
  10.         cella:Tipo;
  11. end;
  12.  
  13. type generic ElementoLista<_T> = Interface
  14.         Function GetItem(AIndex : Integer) : _T;
  15.         Procedure SetItem(AIndex : Integer; AValue : _T);
  16.         Function GetCount : Integer;
  17.         procedure addItem(AValue : _T);
  18.         procedure Clear;
  19.         function lunghezza:integer;
  20.         Property Items [AIndex : Integer] : _T Read GetItem Write SetItem;
  21.         Property Count : Integer Read GetCount;
  22. end;
  23.  
  24. type generic Lista<Tipo>=class (TObject, specialize ElementoLista<Tipo>)
  25. private
  26.         elementi:array of Tipo;
  27.         caricati:integer;
  28.         Function GetItem(AIndex : Integer) : Tipo;
  29.         Procedure SetItem(AIndex : Integer; AValue : Tipo);
  30.         Function GetCount : Integer;
  31.         procedure raddoppiaMemoria;
  32. public
  33.         constructor crea();
  34.         constructor crea(dimensione:word);
  35.         constructor crea(dimensione:word; zero:Tipo);
  36.         procedure addItem(AValue : Tipo);
  37.         procedure Clear;
  38.         function lunghezza:integer;
  39.         Property Items [AIndex : Integer] : Tipo Read GetItem Write SetItem;
  40.         Property Count : Integer Read GetCount;
  41. end;
  42.  
  43.  
  44. implementation
  45.  
  46. constructor Lista.crea();
  47. begin
  48.         setLength(elementi, 10);
  49.         caricati:=0;
  50. end;
  51.  
  52. constructor Lista.crea(dimensione:word);
  53. begin
  54.         if dimensione < 5 then dimensione:=5;
  55.         if dimensione > 10000 then dimensione:=10000;
  56.         setLength(elementi, dimensione);
  57.         caricati:=0;
  58. end;
  59.  
  60. constructor Lista.crea(dimensione:word; zero:Tipo);
  61. var i:integer;
  62. begin
  63.         if dimensione < 5 then dimensione:=5;
  64.         if dimensione > 10000 then dimensione:=10000;
  65.         setLength(elementi, dimensione);
  66.         i:=0;
  67.         caricati:=dimensione;
  68.         while i < caricati do
  69.         begin
  70.                 elementi[i]:=zero;
  71.                 i:=i + 1;
  72.         end;
  73. end;
  74.  
  75. Function Lista.GetItem(AIndex : Integer) : Tipo;
  76. var esito:Tipo;
  77. begin
  78.         if (AIndex < caricati) and (AIndex >= 0) then
  79.         begin
  80.                 esito:=elementi[AIndex];
  81.         end
  82.         else
  83.                 raise FuoriRange.crea(Format('Impossibile accedere alla cella %d', [AIndex]));
  84.         GetItem:=esito;
  85. end;
  86.  
  87. procedure Lista.addItem(AValue : Tipo);
  88. begin
  89.         if caricati >= length(elementi) then
  90.                 raddoppiaMemoria;
  91.         elementi[caricati]:=AValue;
  92.         caricati:=caricati + 1;
  93. end;
  94.  
  95. procedure Lista.Clear;
  96. begin
  97.      caricati:=0;
  98. end;
  99.  
  100. Procedure Lista.SetItem(AIndex : Integer; AValue : Tipo);
  101. begin
  102.         if (AIndex < 0) or (AIndex >= caricati) then
  103.         begin
  104.                 raise FuoriRange.crea(Format('Impossibile accedere alla cella %d', [AIndex]));
  105.         end;
  106.         elementi[AIndex]:=AValue;
  107. end;
  108.  
  109. Function Lista.GetCount : Integer;
  110. begin
  111.         GetCount:=caricati;
  112. end;
  113.  
  114. function Lista.lunghezza:integer;
  115. begin
  116.         lunghezza:=length(elementi);
  117. end;
  118.  
  119. procedure Lista.raddoppiaMemoria;
  120. var nuova:integer;
  121. begin
  122.         nuova:=length(elementi) * 2;
  123.         if (nuova > 10000) or (nuova < 1) then
  124.         begin
  125.                 raise FuoriRange.crea('Impossibile allocare più di 10''000 elementi.');
  126.         end;
  127.         setLength(elementi, nuova);
  128. end;
  129.  
  130. begin
  131.  // non fa nulla
  132. end.