home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 3 / AACD03.BIN / AACD / Programming / sofa / archive / SmallEiffel.lha / SmallEiffel / lib_std / collection2.e < prev    next >
Text File  |  1999-06-05  |  12KB  |  429 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 COLLECTION2[E]
  13.    -- 
  14.    -- Abstract definition of a 2 dimensional collection of elements
  15.    -- of type E. 
  16.    -- 
  17.    -- The SmallEiffel standard library (lib_std) provides two
  18.    -- implementations : ARRAY2[E] and FIXED_ARRAY2[E].
  19.    -- All implementations have exactly the same behavior. Switching 
  20.    -- from one implementation to another only change the memory used
  21.    -- and the execution time.
  22.    --
  23.  
  24. inherit
  25.    ANY
  26.       undefine copy, is_equal
  27.       redefine fill_tagged_out_memory
  28.       end;
  29.    
  30. feature -- Indexing :
  31.    
  32.    lower1, lower2: INTEGER is
  33.          -- Lower index bounds.
  34.       deferred
  35.       end;
  36.    
  37.    frozen line_minimum: INTEGER is
  38.          -- Equivalent of `lower1'.
  39.       do
  40.          Result := lower1;
  41.       end;
  42.    
  43.    frozen column_minimum: INTEGER is
  44.          -- Equivalent of `lower2'.
  45.       do
  46.          Result := lower2;
  47.       end;
  48.    
  49.    upper1, upper2: INTEGER is
  50.          -- Upper index bounds.
  51.       deferred
  52.       end;
  53.    
  54.    frozen line_maximum: INTEGER is
  55.          -- Equivalent of `upper1'.
  56.       do
  57.          Result := upper1;
  58.       end;
  59.  
  60.    frozen column_maximum: INTEGER is
  61.          -- Equivalent of `upper2'.
  62.       do
  63.          Result := upper2;
  64.       end;
  65.    
  66. feature -- Reading :
  67.    
  68.    item(line, column: INTEGER): E is
  69.       require
  70.          valid_index(line,column);
  71.       deferred
  72.       end;
  73.    
  74. feature -- Writing :
  75.    
  76.    put(element: like item; line, column: INTEGER) is
  77.       require
  78.          valid_index(line,column);
  79.       deferred
  80.       ensure
  81.          item(line,column) = element
  82.       end;
  83.  
  84.    force(element: like item; line, column: INTEGER) is
  85.          -- Put `element' at position (`line',`column'). Collection is
  86.          -- resized first when (`line',`column') is not inside current
  87.          -- bounds. New bounds are initialized with default values.
  88.       require
  89.          line >= 0;
  90.          column >= 0
  91.       deferred
  92.       ensure
  93.          item(line,column) = element;
  94.          count >= old count;
  95.       end;
  96.  
  97. feature -- Index validity :
  98.    
  99.    frozen valid_line, valid_index1(line: INTEGER): BOOLEAN is
  100.       do
  101.          Result := lower1 <= line and then line <= upper1;
  102.       ensure
  103.          Result = (lower1 <= line and line <= upper1)
  104.       end;
  105.    
  106.    frozen valid_column, valid_index2(column: INTEGER): BOOLEAN is
  107.       do
  108.          Result := lower2 <= column and then column <= upper2;
  109.       ensure
  110.          Result = (lower2 <= column and column <= upper2)
  111.       end;
  112.    
  113.    frozen valid_index(line, column: INTEGER): BOOLEAN is
  114.       do
  115.          Result := ((lower1 <= line) and then (line <= upper1) 
  116.                     and then
  117.                     (lower2 <= column) and then (column <= upper2)); 
  118.       ensure
  119.          Result = (valid_line(line) and valid_index2(column))
  120.       end;
  121.  
  122. feature -- Counting :
  123.  
  124.    count1: INTEGER is
  125.          -- Size of the first dimension.
  126.       deferred
  127.       ensure
  128.          Result = upper1 - lower1 + 1;
  129.       end;
  130.    
  131.    frozen line_count: INTEGER is
  132.          -- Equivalent of `count1'.
  133.       do
  134.          Result := count1;
  135.       end;
  136.    
  137.    count2: INTEGER is
  138.          -- Size of the second dimension.
  139.       deferred
  140.       ensure
  141.          Result = upper2 - lower2 + 1;
  142.       end;
  143.    
  144.    frozen column_count: INTEGER is
  145.       do
  146.          Result := count2;
  147.       end;
  148.    
  149.    count: INTEGER is
  150.          -- Total number of elements.
  151.       deferred
  152.       ensure
  153.          Result = line_count * column_count
  154.       end;
  155.  
  156. feature 
  157.    
  158.    swap(line1, column1, line2, column2: INTEGER) is
  159.          -- Swap the element at index (`line1',`column1') with the
  160.          -- the element at index (`line2',`column2').
  161.       require
  162.          valid_index(line1,column1);
  163.          valid_index(line2,column2)
  164.       deferred
  165.       ensure
  166.          item(line1,column1) = old item(line2,column2);
  167.          item(line2,column2) = old item(line1,column1);
  168.          count = old count
  169.       end;
  170.    
  171.    set_all_with(v: like item) is
  172.          -- Set all item with value `v'.
  173.       deferred
  174.       ensure
  175.          count = old count
  176.       end;
  177.    
  178.    frozen clear_all is
  179.          -- Set all items to default values.
  180.       local
  181.          value: like item;
  182.       do
  183.          set_all_with(value);
  184.       ensure
  185.          count = old count;
  186.       end;
  187.  
  188. feature -- Creating or initializing :
  189.  
  190.    from_collection2(model: COLLECTION2[like item]) is
  191.          --  Uses `model' to initialize Current.
  192.       require
  193.          model /= Void
  194.       deferred
  195.       ensure
  196.          count1 = model.count1;
  197.          count2 = model.count2
  198.       end;
  199.  
  200.    from_model(model: COLLECTION[COLLECTION[E]]) is
  201.          -- The `model' is used to fill line by line Current.
  202.          -- Assume all sub-collections of `model' have the same
  203.          -- number of lines.
  204.       require
  205.          model /= Void
  206.       deferred
  207.       ensure
  208.          count1 = model.count;
  209.          count2 > 0 implies count2 = model.first.count 
  210.       end;
  211.  
  212. feature -- Looking and comparison :
  213.    
  214.    all_cleared: BOOLEAN is
  215.          -- Are all items set to default values ?
  216.       deferred
  217.       end;
  218.  
  219.    same_as(other: COLLECTION2[E]): BOOLEAN is
  220.          -- Unlike `is_equal', this feature can be used to compare
  221.          -- distinct implementation of COLLECTION2.
  222.       require
  223.          other /= Void
  224.       deferred
  225.       ensure
  226.          Result implies standard_same_as(other)
  227.       end;
  228.  
  229. feature -- Printing :
  230.    
  231.    frozen fill_tagged_out_memory is
  232.       local
  233.          line, column: INTEGER;
  234.          v: like item;
  235.       do
  236.          tagged_out_memory.append("lower1: "); 
  237.          lower1.append_in(tagged_out_memory);
  238.          tagged_out_memory.append(" upper1: "); 
  239.          upper1.append_in(tagged_out_memory);
  240.          tagged_out_memory.append(" lower2: "); 
  241.          lower2.append_in(tagged_out_memory);
  242.          tagged_out_memory.append(" upper2: "); 
  243.          upper2.append_in(tagged_out_memory);
  244.          tagged_out_memory.append(" [%N");
  245.          from
  246.             line := lower1;
  247.          until
  248.             line > upper1
  249.                or else
  250.             tagged_out_memory.count > 4096
  251.          loop
  252.             tagged_out_memory.append("line ");
  253.             line.append_in(tagged_out_memory);
  254.             tagged_out_memory.append("%T: ");
  255.             from
  256.                column := lower2;
  257.             until
  258.                column > upper2
  259.             loop
  260.                v := item(line,column);
  261.                if v = Void then
  262.                   tagged_out_memory.append("Void");
  263.                else
  264.                   v.out_in_tagged_out_memory;
  265.                end;
  266.                tagged_out_memory.extend(' ');
  267.                column := column + 1;
  268.             end;
  269.             tagged_out_memory.extend('%N');
  270.             line := line + 1;
  271.          end;
  272.          if valid_line(line) then
  273.             tagged_out_memory.append("......%N"); 
  274.          end;
  275.       end;
  276.  
  277. feature -- Miscellaneous features :
  278.  
  279.    nb_occurrences(elt: E): INTEGER is
  280.          -- Number of occurrences using `equal'.
  281.          -- See also `fast_nb_occurrences' to chose
  282.          -- the apropriate one.
  283.       deferred
  284.       ensure
  285.          Result >= 0;
  286.       end;
  287.    
  288.    fast_nb_occurrences(elt: E): INTEGER is
  289.          -- Number of occurrences using `='.
  290.       deferred
  291.       ensure
  292.          Result >= 0;
  293.       end;
  294.  
  295.    has(x: like item): BOOLEAN is
  296.          -- Search if a element x is in the array using `equal'.
  297.          -- See also `fast_has' to chose the apropriate one.
  298.       deferred
  299.       end;
  300.    
  301.    fast_has(x: like item): BOOLEAN is
  302.          --  Search if a element x is in the array using `='.
  303.       deferred
  304.       end;
  305.    
  306.    replace_all(old_value, new_value: like item) is
  307.          -- Replace all occurences of the element `old_value' by `new_value' 
  308.          -- using `equal' for comparison.
  309.          -- See also `fast_replace_all' to choose the apropriate one.
  310.       deferred
  311.       ensure
  312.          count = old count;
  313.          nb_occurrences(old_value) = 0
  314.       end;
  315.    
  316.    fast_replace_all(old_value, new_value: like item) is
  317.          -- Replace all occurences of the element `old_value' by `new_value' 
  318.          -- using operator `=' for comparison.
  319.          -- See also `replace_all' to choose the apropriate one.
  320.       deferred
  321.       ensure
  322.          count = old count;
  323.          fast_nb_occurrences(old_value) = 0
  324.       end;
  325.  
  326.    sub_collection2(line_min, line_max, 
  327.                    column_min, column_max: INTEGER): like Current is
  328.          -- Create a new object using selected area of `Current'.
  329.       require
  330.          valid_index(line_min,column_min);
  331.          valid_index(line_min,column_min)
  332.       deferred
  333.       ensure
  334.          Result /= Void
  335.       end;
  336.  
  337.    set_area(element: like item; 
  338.             line_min, line_max, column_min, column_max: INTEGER) is
  339.          -- Set all the elements of the selected area rectangle with `element'.
  340.       require
  341.          valid_index(line_min,line_max); 
  342.          valid_index(column_min,column_max)
  343.       local
  344.          line, column : INTEGER;
  345.       do
  346.          from
  347.             line := line_min;
  348.          until
  349.             line > line_max
  350.          loop
  351.             from
  352.                column := column_min
  353.             until
  354.                column > column_max
  355.             loop
  356.                put(element,line,column);
  357.                column := column + 1;
  358.             end;
  359.             line := line + 1;
  360.          end;
  361.       ensure
  362.          count = old count;
  363.       end;
  364.  
  365. feature {COLLECTION2} -- For `same_as' implementation :
  366.  
  367.    frozen standard_same_as(other: COLLECTION2[E]): BOOLEAN is
  368.       require
  369.          generating_type /= other.generating_type
  370.       local
  371.          line, column: INTEGER;
  372.       do
  373.          if lower1 /= other.lower1 then
  374.          elseif upper1 /= other.upper1 then
  375.          elseif lower2 /= other.lower2 then
  376.          elseif upper2 /= other.upper2 then
  377.          else
  378.             from
  379.                Result := true;
  380.                line := upper1;
  381.             until
  382.                not Result or else line < lower1
  383.             loop
  384.                from
  385.                   column := upper2;
  386.                until
  387.                   not Result or else column < lower2
  388.                loop
  389.                   Result := equal_like(item(line,column),other.item(line,column));
  390.                   column := column - 1;
  391.                end;
  392.                line := line - 1;
  393.             end;
  394.          end;
  395.       end;
  396.  
  397.    same_as_array2(other: ARRAY2[E]): BOOLEAN is
  398.       require
  399.          other /= Void
  400.       deferred
  401.       end;
  402.    
  403.    same_as_fixed_array2(other: FIXED_ARRAY2[E]): BOOLEAN is
  404.       require
  405.          other /= Void
  406.       deferred
  407.       end;
  408.    
  409. feature {NONE}
  410.    
  411.    frozen equal_like(e1, e2: like item): BOOLEAN is
  412.          -- Note: this feature is called to avoid calling `equal'
  413.          -- on expanded types (no automatic conversion to 
  414.          -- corresponding reference type).
  415.       do
  416.          if e1.is_basic_expanded_type then
  417.             Result := e1 = e2;
  418.          elseif e1.is_expanded_type then
  419.             Result := e1.is_equal(e2);
  420.          elseif e1 = e2 then
  421.             Result := true;
  422.          elseif e1 = Void or else e2 = Void then
  423.          else
  424.             Result := e1.is_equal(e2);
  425.          end;
  426.       end;
  427.  
  428. end -- COLLECTION2[E]
  429.