IdContainers.pas 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. {
  2. $Project$
  3. $Workfile$
  4. $Revision$
  5. $DateUTC$
  6. $Id$
  7. This file is part of the Indy (Internet Direct) project, and is offered
  8. under the dual-licensing agreement described on the Indy website.
  9. (http://www.indyproject.org/)
  10. Copyright:
  11. (c) 1993-2005, Chad Z. Hower and the Indy Pit Crew. All rights reserved.
  12. }
  13. {
  14. $Log$
  15. }
  16. {
  17. Rev 1.7 10/26/2004 11:08:10 PM JPMugaas
  18. Updated refs.
  19. Rev 1.6 28.09.2004 21:35:28 Andreas Hausladen
  20. Added TIdObjectList.Assign method for missing Delphi 5 TList.Assign
  21. Rev 1.5 1/4/2004 12:09:00 AM BGooijen
  22. Commented out Notify, this doesn't exist in DotNet, and doesn't do anything
  23. anyways
  24. Rev 1.4 3/13/2003 11:10:52 AM JPMugaas
  25. Fixed warning message.
  26. Rev 1.3 2/8/2003 04:33:34 AM JPMugaas
  27. Commented out a free statement in the TIdObjectList.Notify method because it
  28. was causing instability in some new IdFTPList code I was working on.
  29. Added a TStringList descendent object that implements a buble sort. That
  30. should require less memory than a QuickSort. This also replaces the
  31. TStrings.CustomSort because that is not supported in D4.
  32. Rev 1.2 2/7/2003 10:33:48 AM JPMugaas
  33. Added BoubleSort to TIdObjectList to facilitate some work.
  34. Rev 1.1 12/2/2002 04:32:30 AM JPMugaas
  35. Fixed minor compile errors.
  36. Rev 1.0 11/14/2002 02:16:14 PM JPMugaas
  37. Revision 1.0 2001-02-20 02:02:09-05 dsiders
  38. Initial revision
  39. }
  40. {********************************************************************}
  41. {* IdContainers.pas *}
  42. {* *}
  43. {* Provides compatibility with the Contnr.pas unit from *}
  44. {* Delphi 5 not found in Delphi 4. *}
  45. {* *}
  46. {* Based on ideas from the Borland VCL Contnr.pas interface. *}
  47. {* *}
  48. {********************************************************************}
  49. unit IdContainers;
  50. interface
  51. {$i IdCompilerDefines.inc}
  52. uses
  53. Classes,
  54. {$IFDEF HAS_UNIT_Generics_Collections}
  55. System.Generics.Collections
  56. {$ELSE}
  57. Contnrs
  58. {$ENDIF}
  59. ;
  60. type
  61. {$IFDEF HAS_GENERICS_TObjectList}
  62. TIdSortCompare<T: class> = function(AItem1, AItem2 : T): Integer;
  63. {$ELSE}
  64. TIdSortCompare = function(AItem1, AItem2 : TObject): Integer;
  65. {$ENDIF}
  66. {TIdObjectList}
  67. {$IFDEF HAS_GENERICS_TObjectList}
  68. TIdObjectList<T: class> = class(TObjectList<T>)
  69. public
  70. procedure BubbleSort(ACompare : TIdSortCompare<T>);
  71. procedure Assign(Source: TIdObjectList<T>);
  72. end;
  73. {$ELSE}
  74. TIdObjectList = class(TObjectList)
  75. public
  76. procedure BubbleSort(ACompare : TIdSortCompare);
  77. // This is a simplyfied Assign method that does only support the copy operation.
  78. procedure Assign(Source: TIdObjectList); reintroduce;
  79. end;
  80. {$ENDIF}
  81. TIdStringListSortCompare = function(List: TStringList; Index1, Index2: Integer): Integer;
  82. TIdBubbleSortStringList = class(TStringList)
  83. public
  84. procedure BubbleSort(ACompare: TIdStringListSortCompare); virtual;
  85. end;
  86. implementation
  87. {$IFDEF DCC_XE3_OR_ABOVE}
  88. uses
  89. System.Types;
  90. {$ENDIF}
  91. { TIdObjectList }
  92. {$IFDEF HAS_GENERICS_TObjectList}
  93. procedure TIdObjectList<T>.BubbleSort(ACompare: TIdSortCompare<T>);
  94. {$ELSE}
  95. procedure TIdObjectList.BubbleSort(ACompare: TIdSortCompare);
  96. {$ENDIF}
  97. var
  98. i, n, newn : Integer;
  99. begin
  100. n := Count;
  101. repeat
  102. newn := 0;
  103. for i := 1 to n-1 do begin
  104. if ACompare(Items[i-1], Items[i]) > 0 then begin
  105. Exchange(i-1, i);
  106. newn := i;
  107. end;
  108. end;
  109. n := newn;
  110. until n = 0;
  111. end;
  112. {$IFDEF HAS_GENERICS_TObjectList}
  113. procedure TIdObjectList<T>.Assign(Source: TIdObjectList<T>);
  114. {$ELSE}
  115. procedure TIdObjectList.Assign(Source: TIdObjectList);
  116. {$ENDIF}
  117. var
  118. I: Integer;
  119. begin
  120. // This is a simplyfied Assign method that does only support the copy operation.
  121. Clear;
  122. Capacity := Source.Capacity;
  123. for I := 0 to Source.Count - 1 do begin
  124. Add(Source[I]);
  125. end;
  126. end;
  127. { TIdBubbleSortStringList }
  128. procedure TIdBubbleSortStringList.BubbleSort(ACompare: TIdStringListSortCompare);
  129. var
  130. i, n, newn : Integer;
  131. begin
  132. n := Count;
  133. repeat
  134. newn := 0;
  135. for i := 1 to n-1 do begin
  136. if ACompare(Self, i-1, i) > 0 then begin
  137. Exchange(i-1, i);
  138. newn := i;
  139. end;
  140. end;
  141. n := newn;
  142. until n = 0;
  143. end;
  144. end.