home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Environments / Small Eiffel 0.4.8 / lib_std / fixed_array.e < prev    next >
Encoding:
Text File  |  1997-04-13  |  6.2 KB  |  316 lines  |  [TEXT/ttxt]

  1. -- Part of SmallEiffel -- Read DISCLAIMER file -- Copyright (C) 
  2. -- Dominique COLNET and Suzanne COLLIN -- colnet@loria.fr
  3. --
  4. class FIXED_ARRAY[E]
  5.    -- 
  6.    -- Unlike ARRAY, the `lower' bound of a FIXED_ARRAY is frozen 
  7.    -- to 0. Thus, some memory is saved and looping toward `lower'
  8.    -- bound (which is 0) run a little bit faster.
  9.    --
  10.  
  11. inherit ARRAYED_COLLECTION[E];
  12.  
  13. creation 
  14.    make, with_capacity, from_collection
  15.  
  16. feature
  17.  
  18.    lower: INTEGER is 0;
  19.      -- Frozen lower bound.
  20.    
  21. feature -- Creation and modification :
  22.    
  23.    make(size: INTEGER) is
  24.      -- Make array with range [0 .. size - 1]. 
  25.      -- When size = 0 the array is empty.
  26.       require
  27.      size >= 0
  28.       do
  29.      if size = 0 then
  30.         upper := -1;
  31.      elseif capacity = 0 then
  32.         storage.calloc(size);
  33.         capacity := size;
  34.         upper := size - 1;
  35.      elseif capacity < size then
  36.         storage.realloc(storage,size);
  37.         capacity := size;
  38.         upper := size - 1;
  39.         clear_all;
  40.      else
  41.         upper := size - 1;
  42.         clear_all;
  43.      end;
  44.       ensure
  45.      upper = size - 1;
  46.      capacity >= old capacity;
  47.      all_cleared
  48.       end;
  49.  
  50.    with_capacity(needed_capacity: INTEGER) is
  51.      -- Create an empty array with at least `needed_capacity'.
  52.       do
  53.      if capacity < needed_capacity then
  54.         if capacity = 0 then
  55.            storage.malloc(needed_capacity);
  56.         else
  57.            storage.realloc(storage,needed_capacity);
  58.         end;
  59.         capacity := needed_capacity;
  60.      end;
  61.      upper := -1;
  62.       ensure
  63.      capacity >= needed_capacity;
  64.      empty
  65.       end;
  66.  
  67. feature -- Modification :
  68.  
  69.    resize(new_count: INTEGER) is
  70.            -- Resize the array. When `new_count' is greater than
  71.      -- `count', new positions are initialized with appropriate 
  72.      -- default values.
  73.       require
  74.      new_count >= 0
  75.       local
  76.      needed, i: INTEGER;
  77.      elt_default: like item;
  78.       do
  79.      if new_count <= count then
  80.         upper := new_count - 1;
  81.      else
  82.         needed := new_count;
  83.         if capacity < needed then
  84.            if capacity = 0 then
  85.           storage.calloc(needed);
  86.            else
  87.           storage.realloc(storage,needed);
  88.            end;
  89.            capacity := needed;
  90.         end;
  91.         from
  92.            needed := upper;
  93.            upper := new_count - 1;
  94.            i := upper;
  95.         until
  96.            i = needed
  97.         loop
  98.            put(elt_default,i);
  99.            i := i - 1;
  100.         end;
  101.      end;
  102.       ensure
  103.      count = new_count;
  104.      capacity >= old capacity
  105.       end;
  106.  
  107. feature 
  108.  
  109.    sub_array(low, up: INTEGER): like Current is
  110.       local
  111.      i1, i2: INTEGER;
  112.       do
  113.      from
  114.         i2 := up - low;
  115.         !!Result.with_capacity(i2 + 1);
  116.         Result.set_upper(i2);
  117.         i2 := low;
  118.         i1 := 0;
  119.      until
  120.         i2 > up
  121.      loop
  122.         Result.put(item(i2),i1);
  123.         i1 := i1 + 1;
  124.         i2 := i2 + 1;
  125.      end;
  126.       ensure then
  127.      Result.lower = 0
  128.       end;
  129.  
  130. feature -- Implementation of deferred :
  131.  
  132.    item, infix "@" (index: INTEGER): E is
  133.       do
  134.      Result := storage.item(index);
  135.       end;
  136.    
  137.    put(element: E; index: INTEGER) is
  138.       do
  139.      storage.put(element,index);
  140.       end;
  141.  
  142.    add_first(element: like item) is
  143.       do
  144.      if upper + 1 <= capacity - 1 then
  145.         upper := upper + 1;
  146.         move(0,upper - 1,1);
  147.      elseif capacity = 0 then
  148.         storage.malloc(2);
  149.         capacity := 2;
  150.         upper := 0
  151.      else
  152.         capacity := 2 * capacity;
  153.         storage.realloc(storage,capacity);
  154.         upper := upper + 1;
  155.         move(0,upper - 1,1);
  156.      end;
  157.      put(element,0);
  158.       end;
  159.  
  160.    add_last(element: like item) is
  161.       do
  162.      if upper + 1 <= capacity - 1 then
  163.         upper := upper + 1;
  164.      elseif capacity = 0 then
  165.         storage.malloc(2);
  166.         capacity := 2;
  167.         upper := 0
  168.      else
  169.         capacity := 2 * capacity;
  170.         storage.realloc(storage,capacity);
  171.         upper := upper + 1;
  172.      end;
  173.      put(element,upper);
  174.       end;
  175.  
  176.    count: INTEGER is
  177.       do
  178.      Result := upper + 1;
  179.       end;
  180.  
  181.    clear is
  182.       do
  183.      upper := -1;
  184.       ensure then
  185.      capacity = old capacity
  186.       end;
  187.  
  188.    copy(other: like Current) is
  189.      -- Copy `other' onto Current.
  190.       do
  191.      upper := other.upper;
  192.      if upper >= 0 then
  193.         if capacity = 0 then
  194.            capacity := upper + 1;
  195.            storage.malloc(capacity);
  196.         elseif capacity < upper + 1 then
  197.            capacity := upper + 1;
  198.            storage.realloc(storage,capacity);
  199.         end;
  200.         storage.copy_from(other.storage,upper);
  201.      end;
  202.       end;
  203.    
  204.    set_all_with(v: like item) is
  205.       do
  206.      storage.set_all_with(v,upper);
  207.       end;
  208.  
  209.    from_collection(model: COLLECTION[like item]) is
  210.       local
  211.      i1, i2, up: INTEGER;
  212.       do
  213.      from
  214.         with_capacity(model.count);
  215.         upper := model.count - 1;
  216.         i1 := 0;
  217.         i2 := model.lower;
  218.         up := model.upper;
  219.      until
  220.         i2 > up 
  221.      loop
  222.         put(model.item(i2),i1);
  223.         i1 := i1 + 1;
  224.         i2 := i2 + 1;
  225.      end;
  226.       end;
  227.    
  228.    is_equal(other: like Current): BOOLEAN is
  229.       do
  230.      if Current = other then
  231.         Result := true;
  232.      elseif upper = other.upper then
  233.         if upper >= 0 then
  234.            Result := storage.memcmp(other.storage,upper + 1);
  235.         else
  236.            Result := true;
  237.         end;
  238.      end;
  239.       end;
  240.  
  241.    all_cleared: BOOLEAN is
  242.       do
  243.      Result := storage.all_cleared(upper);
  244.       end;
  245.  
  246.    nb_occurrences(element: like item): INTEGER is
  247.       do
  248.      Result := storage.nb_occurrences(element,upper);
  249.       end;
  250.       
  251.    fast_nb_occurrences(element: E): INTEGER is
  252.       do
  253.      Result := storage.fast_nb_occurrences(element,upper);
  254.       end;
  255.  
  256.    index_of(element: like item): INTEGER is
  257.       do
  258.      Result := storage.index_of(element,upper);
  259.       end;
  260.  
  261.    fast_index_of(element: like item): INTEGER is
  262.       do
  263.      Result := storage.fast_index_of(element,upper);
  264.       end;
  265.  
  266.    slice(low, up: INTEGER): like Current is
  267.       local
  268.      i: INTEGER;
  269.       do
  270.      from
  271.         i := low;
  272.         !!Result.with_capacity(up - low + 1);
  273.      until
  274.         i > up
  275.      loop
  276.         Result.add_last(item(i));
  277.         i := i + 1;
  278.      end;
  279.       end;
  280.  
  281.    force(element: E; index: INTEGER) is
  282.       do
  283.      if index <= upper then
  284.         put(element,index);
  285.      else
  286.         resize(index + 1);
  287.         put(element,index);
  288.      end;
  289.       end;
  290.      
  291.    remove_first is
  292.       do
  293.      if upper = 0 then
  294.         storage.free;
  295.         capacity := 0;
  296.         upper := -1;
  297.      else
  298.         storage.remove_first(upper);
  299.         upper := upper - 1;
  300.      end;
  301.       ensure then
  302.      lower = old lower
  303.       end;
  304.  
  305.    remove(index: INTEGER) is
  306.       do
  307.      storage.remove(index,upper);
  308.      upper := upper - 1;
  309.       end;
  310.    
  311. invariant
  312.  
  313.    upper <= capacity;
  314.  
  315. end -- FIXED_ARRAY[E]
  316.