Subversion Repositories Kolibri OS

Rev

Rev 6732 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1.  
  2.  
  3. struct TCollection{
  4.   int Count;//itemcunt
  5.   int Delta;//increase buffer Delta
  6.   int Items;//pointer to array of item pointers
  7.   int Limit;//currently allocated size (in elements) of Items list
  8.  
  9.   TCollection( int ALimit,ADelta );
  10.   ~TCollection( void );
  11.  
  12.   int  At( int Index );
  13.   void AtDelete( int Index );
  14.   void AtFree( int Index );
  15.   void AtInsert( int Index, Item );
  16.   void DeleteAll( void );
  17.   void Free( int Item );
  18.   void FreeAll( void );
  19.   void FreeItem( int Item );
  20.   void Insert( int Item );
  21.   void SetLimit( int ALimit );
  22. };
  23.  
  24. :int TCollection::At( int Index )
  25. {
  26.   return DSDWORD[ Index*sizeof(int)+Items ];
  27. }
  28.  
  29. :void TCollection::AtDelete( int Index )
  30. {
  31. //  if(Index<0)||(Index>=Count){/*Error(coIndexError);*/return;}
  32.   ECX = Count-Index-1;
  33.   EDI = Index*sizeof(int)+Items; ESI = EDI+4; $cld; $rep;$movsd;
  34.   Count --;
  35. }
  36.  
  37. :void TCollection::AtFree( int Index )
  38. {
  39.   int Item = At( Index);
  40.   AtDelete( Index );
  41.   FreeItem( Item );
  42. }
  43.  
  44. :void TCollection::AtInsert( int Index, Item )
  45. {
  46.   $pushfd
  47.   IF( Count == Limit )
  48.   {
  49.     SetLimit( Count+Delta );
  50.   }
  51.   ECX = Count-Index;
  52.   EDI = Count*sizeof(int)+Items;
  53.   ESI = EDI-4; $std $rep $movsd $cld
  54.   DSDWORD[ EDI ] = Item;
  55.   Count ++;
  56.   $popfd
  57. }
  58.  
  59. :void TCollection::DeleteAll()
  60. {
  61.   Count=0;
  62. }
  63.  
  64. :void TCollection::Free( int Item )
  65. {
  66.   Delete( Item );
  67.   FreeItem( Item );
  68. }
  69.  
  70. :void TCollection::FreeAll( void )
  71. {
  72.   int I;
  73.   FOR( I = 0; I < Count; I ++ )FreeItem( At(I) );
  74.   Count = 0;
  75. }
  76.  
  77. :void TCollection::FreeItem( int Item )
  78. {
  79.   IF( Item )free( Item );//+++Dispose(PObject(Item), Done);
  80. }
  81.  
  82. :void TCollection::Insert( int Item )
  83. {
  84.   AtInsert( Count, Item );
  85. }
  86.  
  87. :void TCollection::SetLimit( int ALimit )
  88. {
  89.   int AItems;
  90.  
  91.   IF( ALimit < Count )ALimit = Count;
  92.   IF( ALimit != Limit )
  93.   {
  94.     IF( !ALimit ) AItems = 0;
  95.     ELSE
  96.     {
  97.       AItems = malloc(ALimit*sizeof(int) );
  98.       IF( Count )&&( Items )
  99.       {
  100.         CopyMemory( AItems, Items, Limit*sizeof(int) );
  101.       }
  102.     }
  103.     IF( Limit )free( Items );
  104.     Items = AItems;
  105.     Limit = ALimit;
  106.   }
  107. }
  108.  
  109. :TCollection::TCollection( int ALimit, ADelta )
  110. {
  111.   Items = 0;
  112.   Count = 0;
  113.   Limit = 0;
  114.   Delta = ADelta;
  115.   SetLimit( ALimit );
  116.   return this;
  117. }
  118.  
  119. :TCollection::~TCollection( void )
  120. {
  121.   FreeAll();
  122.   SetLimit(0);
  123. }
  124.  
  125. struct TSortedCollection:TCollection
  126. {
  127.   int  comparemethod;
  128.   int  Duplicates;
  129.  
  130.   TSortedCollection( int ALimit, ADelta );
  131.  
  132.   int  Compare( int Key1, Key2 );
  133.   void Insert( int Item );
  134.   int  Search( int Key, Index );
  135. };
  136.  
  137. :int TSortedCollection::Compare( int Key1, Key2 )
  138. {
  139.   strcmp( Key1, Key2 );
  140. }
  141.  
  142. :TSortedCollection::TSortedCollection( int ALimit, ADelta )
  143. :TCollection( ALimit, ADelta );
  144. {
  145.   comparemethod=#Compare;
  146.   return this;
  147. }
  148.  
  149. :void TSortedCollection::Insert( int Item )
  150. {
  151.   int i;
  152.   IF( !Search(/*KeyOf(*/Item/*)*/,#i) ) || ( Duplicates )AtInsert( i, Item );
  153. }
  154.  
  155.  
  156. :int TSortedCollection::Search( int Key, Index )
  157. {
  158.   int L, H, I, C;
  159.   int S;
  160.  
  161.   S = 0;
  162.   L = 0;
  163.   H = Count-1;
  164.   WHILE( L <= H )
  165.   {
  166.     I = L+H >> 1;
  167.     ECX = I*sizeof(int)+Items;
  168.     comparemethod( this, /*KeyOf(*/DSDWORD[ECX]/*)*/, Key );
  169.     C = EAX;
  170.     IF( C < 0 ){ L = I+1; }
  171.     ELSE
  172.     {
  173.       H = I-1;
  174.       IF( !C ){
  175.         S = 1;
  176.         IF( !Duplicates )L = I;
  177.       }
  178.     }
  179.   }
  180.   DSDWORD[ Index ]=L;
  181.   return S;
  182. }
  183.