Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Principal > OOP
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

Grupo de Teaming del ClubDelphi

Respuesta
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 07-03-2008
xaguilars xaguilars is offline
Miembro
 
Registrado: jul 2007
Posts: 22
Poder: 0
xaguilars Va por buen camino
Lightbulb ¿Como mejorar mi TDictionary? (no es Delphi .NET)

Hola hace unos días hice esta implementación para simular un objeto de tipo Dictionary como en C#. He intentado hacerlo con TObject en vez de Variant, pero no me deja asignar directamente tipos primitivos como integer, etc. como sí me deja el tipo Variant.

¿Cambiaríais algo para que esté más optimizado? ¿Cuando en el clear hago los SetLength de nuevo a 0, que pasa con lo que había, se queda como basura en memoria?

Bueno gracias por las respuestas, aquí os dejo el código. Quien quiera usarlo lo puede usar.

Código Delphi [-]
unit uColecciones;
interface
uses Classes,Sysutils;
type
EKeyNotFound  = class(Exception);
EKeyConflict = class(Exception);
TKeys = array of string;
TItems = array of Variant;
TDictionary = class
   private
    _count:integer;
    _keys:TKeys;
    _items:TItems;
    function GetItem (Index:String):Variant;
    procedure SetItem (Index:String; Value:Variant);
   public
    property Items[Index:String]:Variant read GetItem write SetItem; default;
    procedure Add(key:string;item:Variant);
    procedure Remove(key:string);
    procedure Clear;
    function Count:Integer;
    function GetKeys:TKeys;
    function GetItems:TItems;
    constructor Create;
    function KeyExists (Key:String):boolean;
    function IndexOf (Key:String):Integer;
  end;
implementation

{ TDictionary }

procedure TDictionary.Add(key:string;item:Variant);
begin
   if KeyExists(key) then
   begin
    raise EKeyConflict.Create('La clave ya existe.');
    exit;
   end
   else
   begin
      inc(_count);
      SetLength(_keys,_count);
      SetLength(_items,_count);
      _keys[count-1]:=key;
      _items[count-1]:=item;
   end;
end;

procedure TDictionary.Clear;
begin
 SetLength(_keys,0);
 SetLength(_items,0);
 _count:=0;
end;

function TDictionary.Count: Integer;
begin
  result:=_count;
end;

constructor TDictionary.Create;
begin
 inherited;
 _count:=0;
end;

function TDictionary.GetItem(Index: String): Variant;
var i,j:integer;
begin
 if not KeyExists(Index) then
 begin
   raise EKeyNotFound.Create('Clave no encontrada.');
   exit;
 end
 else
 begin
 j:=-1;
 for i := 0 to _count - 1 do
   if _keys[i]=index then
   begin
     j:=i;
     break;
   end;
 end;
 result:=_items[j];  
end;

function TDictionary.GetItems: TItems;
begin
  result:=_items;
end;

function TDictionary.GetKeys: TKeys;
begin
  result:=_keys;
end;

function TDictionary.IndexOf(Key: String): Integer;
var i:integer;
begin
  for i := 0 to _count - 1 do
    if _keys[i]=key then
    begin
      result:=i;
      exit;
    end;
  result:=-1;
end;

function TDictionary.KeyExists(Key: String): boolean;
var i:integer;
begin
  for i := 0 to _count - 1 do
    if _keys[i]=key then
    begin
      result:=true;
      exit;
    end;
  result:=false;
end;

procedure TDictionary.Remove(Key:string);
var
 i,j:integer;
 ky:TKeys;
 it:TItems;
begin
 j:=IndexOf(Key);
 if j=-1 then
 begin
   raise EKeyNotFound.Create('Clave no encontrada.');
   exit;
 end
 else
 begin
   for i := 0 to _count - 1 do
    if i<>j then
    begin
      SetLength(ky,Length(ky)+1);
      SetLength(it,Length(it)+1);
      ky[Length(ky)-1]:=_keys[i];
      it[Length(it)-1]:=_items[i];
    end;
    dec(_count);
    _keys:=ky;
    _items:=it;
 end;
end;

procedure TDictionary.SetItem(Index: String; Value:Variant);
var j:integer;
begin
 j:=IndexOf(Index);
 if j=-1 then
 begin
   raise EKeyNotFound.Create('Clave no encontrada.');
   exit;
 end
 else
 begin
   _items[j]:=Value;
 end;
end;

end.
Responder Con Cita
Respuesta



Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
Utilidades para mejorar el IDE de Delphi martinzcr Varios 1 14-09-2007 12:43:40
Como mejorar velocidad en una Vista cesar_picazo Varios 2 27-01-2007 13:02:08
¿como mejorar la visualizacion de imagenes de texto? Alfredo Gráficos 2 24-05-2006 17:35:57
Como mejorar la velocidad en este caso.... Alfredo Varios 4 02-09-2005 22:45:11
Cómo hago para mejorar la rapidéz en actualizaciones ? ramygo Firebird e Interbase 3 12-06-2003 09:37:22


La franja horaria es GMT +2. Ahora son las 09:56:17.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi
Copyright 1996-2007 Club Delphi