home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 3 / AACD03.BIN / AACD / Programming / sofa / archive / SmallEiffel.lha / SmallEiffel / lib_std / arrayed_collection.e < prev    next >
Text File  |  1999-06-05  |  3KB  |  123 lines

  1. -- This file is  free  software, which  comes  along  with  SmallEiffel. This
  2. -- software  is  distributed  in the hope that it will be useful, but WITHOUT 
  3. -- ANY  WARRANTY;  without  even  the  implied warranty of MERCHANTABILITY or
  4. -- FITNESS  FOR A PARTICULAR PURPOSE. You can modify it as you want, provided
  5. -- this header is kept unaltered, and a notification of the changes is added.
  6. -- You  are  allowed  to  redistribute  it and sell it, alone or as a part of 
  7. -- another product.
  8. --          Copyright (C) 1994-98 LORIA - UHP - CRIN - INRIA - FRANCE
  9. --            Dominique COLNET and Suzanne COLLIN - colnet@loria.fr 
  10. --                       http://SmallEiffel.loria.fr
  11. --
  12. deferred class ARRAYED_COLLECTION[E]
  13.    -- 
  14.    -- Common root for ARRAY[E] and FIXED_ARRAY[E].
  15.    --
  16.  
  17. inherit COLLECTION[E];
  18.  
  19. feature {ARRAYED_COLLECTION}
  20.    
  21.    storage: NATIVE_ARRAY[E];
  22.          -- Internal access to storage location.
  23.    
  24. feature
  25.    
  26.    capacity: INTEGER;
  27.          -- Internal storage capacity in number of item.
  28.    
  29.    upper: INTEGER;
  30.          -- Upper index bound.
  31.    
  32. feature 
  33.  
  34.    sub_array(low, up: INTEGER): like Current is
  35.       require
  36.          valid_index(low);
  37.          valid_index(up);
  38.          low <= up
  39.       deferred
  40.       ensure
  41.          same_type(Result);
  42.          Result.count = (up - low + 1);
  43.          Result.lower = low or Result.lower = 0
  44.       end;
  45.  
  46. feature -- Implementation of deferred :
  47.  
  48.    first: like item is
  49.       do
  50.          Result := storage.item(0);
  51.       end;
  52.  
  53.    last: like item is
  54.       do
  55.          Result := item(upper);
  56.       end;
  57.  
  58.    add(element: like item; index: INTEGER) is
  59.       do
  60.          if index = upper + 1 then
  61.             add_last(element);
  62.          else
  63.             add_last(element);
  64.             move(index,upper - 1,1);
  65.             put(element,index);
  66.          end;
  67.       end;
  68.  
  69.    remove_last is
  70.       do
  71.          upper := upper - 1;
  72.       end;
  73.    
  74.    replace_all(old_value, new_value: like item) is
  75.       do
  76.          storage.replace_all(old_value,new_value,count - 1);
  77.       end;
  78.    
  79.    fast_replace_all(old_value, new_value: like item) is
  80.       do
  81.          storage.fast_replace_all(old_value,new_value,count - 1);
  82.       end;
  83.  
  84. feature -- The Guru section :
  85.  
  86.    frozen free is
  87.       obsolete "Since release -0.81, the Garbage Collector is %
  88.                %implemented. This feature will be soon removed."
  89.       do
  90.       end;
  91.    
  92. feature -- Interfacing with C :
  93.    
  94.    to_external: POINTER is
  95.          -- Gives C access into the internal `storage' of the ARRAY.
  96.          -- Result is pointing the element at index `lower'.
  97.          -- 
  98.          -- NOTE: do not free/realloc the Result. Resizing of the array 
  99.          --       can makes this pointer invalid. 
  100.       require
  101.          not empty
  102.       do
  103.          Result := storage.to_pointer;
  104.       ensure
  105.          Result.is_not_null
  106.       end;
  107.  
  108. feature {ARRAYED_COLLECTION}
  109.  
  110.    set_upper(new_upper: like upper) is
  111.       do
  112.          upper := new_upper;
  113.       end;
  114.  
  115. invariant
  116.  
  117.    capacity >= (upper - lower + 1);
  118.  
  119.    capacity > 0 implies storage.is_not_null;
  120.  
  121. end -- ARRAYED_COLLECTION[E]
  122.  
  123.