ANY ARRAY3 NONE
class interface ARRAY3[E]
   --
   -- General prurpose, resizable, three dimensional array.
   --

creation
   make (line_min, line_max, column_min, column_max, depth_min, depth_max: INTEGER)
      -- Reset all bounds line_minimum / line_maximum / column_minimum
      -- column_maximum / depth_min and depth_max using arguments as
      -- new values. All elements are set to the default value of type E.
      require
         line_min <= line_max;
         column_min <= column_max;
         depth_min <= depth_max
      ensure
         line_minimum = line_min;
         line_maximum = line_max;
         column_minimum = column_min;
         column_maximum = column_max;
         depth_minimum = depth_min;
         depth_maximum = depth_max

   copy (other: like Current)
      require
         same_dynamic_type(other)
      ensure
         is_equal(other)

   from_collection3 (model: COLLECTION3[E])
      --  Uses model to initialize Current.
      require
         model /= Void
      ensure
         lower1 = model.lower1;
         lower2 = model.lower2;
         lower3 = model.lower3;
         count1 = model.count1;
         count2 = model.count2;
         count3 = model.count3

   from_collection (contents: COLLECTION[E]; line_min, line_max, column_min, column_max, depth_min, depth_max: INTEGER)
      --  Reset all bounds using line_min, line_max, column_min,
      --  column_max, depth_min, and depth_max.
      --  Copy all elements of contents, line by line into Current.
      require
         line_min <= line_max;
         column_min <= column_max;
         depth_min <= depth_max;
         contents.count = (line_max - line_min + 1) * (column_max - column_min + 1) * (depth_max - depth_min + 1)
      ensure
         line_minimum = line_min;
         line_maximum = line_max;
         column_minimum = column_min;
         column_maximum = column_max;
         depth_minimum = depth_min;
         depth_maximum = depth_max;
         count = contents.count

   from_model (model: COLLECTION[COLLECTION[COLLECTION[E]]])
      -- The model is used to fill line by line the COLLECTION3.
      -- Assume all sub-collections of have the same indexing.
      require
         model /= Void
      ensure
         line_minimum = model.lower;
         line_maximum = model.upper;
         column_minimum = model.first.lower;
         column_maximum = model.first.upper;
         depth_minimum = model.first.first.lower;
         depth_maximum = model.first.first.upper;
         count1 = model.count;
         count2 > 0 implies count2 = model.first.count;
         count3 > 0 implies count3 = model.first.first.count

feature(s) from SAFE_EQUAL
   test (e1, e2: E): BOOLEAN
      -- In order to avoid run-time type errors, feature safe_equal calls
      -- is_equal only when e1 and e2 have exactly the same dynamic
      -- type. Furthermore, this feature avoids argument passing from some
      -- expanded type to the corresponding reference type (no automatic
      -- allocation of some reference type during the comparison).

   safe_equal (e1, e2: E): BOOLEAN
      -- In order to avoid run-time type errors, feature safe_equal calls
      -- is_equal only when e1 and e2 have exactly the same dynamic
      -- type. Furthermore, this feature avoids argument passing from some
      -- expanded type to the corresponding reference type (no automatic
      -- allocation of some reference type during the comparison).

feature(s) from COLLECTION3
   -- Indexing:

   lower1: INTEGER
      -- Lower index bound for dimension 1.

   lower2: INTEGER
      -- Lower index bound for dimension 2.

   lower3: INTEGER
      -- Lower index bound for dimension 3.

   line_minimum: INTEGER
      -- Equivalent of lower1.

   column_minimum: INTEGER
      -- Equivalent of lower2.

   depth_minimum: INTEGER
      -- Equivalent of lower3.

   upper1: INTEGER
      -- Upper index bound for dimension 1.

   upper2: INTEGER
      -- Upper index bound for dimension 2.

   upper3: INTEGER
      -- Upper index bound for dimension 3.

   line_maximum: INTEGER
      -- Equivalent of upper1.

   column_maximum: INTEGER
      -- Equivalent of upper2.

   depth_maximum: INTEGER
      -- Equivalent of upper3.

feature(s) from COLLECTION3
   -- Reading:

   item (line, column, depth: INTEGER): E
      require
         valid_index(line,column,depth)

feature(s) from COLLECTION3
   -- Writing:

   put (element: E; line, column, depth: INTEGER)
      require
         valid_index(line,column,depth)
      ensure
         item(line,column,depth) = element

   force (x: E; line, column, depth: INTEGER)
      -- Put element at position (line,column,depth).
      -- Collection is resized first when (line,column,depth)
      -- is not inside current bounds.
      -- New bounds are initialized with default values.
      require
         True
      require else
         line >= 0;
         column >= 0;
         depth >= 0
      ensure
         item(line,column,depth) = x;
         count >= old count

feature(s) from COLLECTION3
   -- Index validity:

   valid_line (line: INTEGER): BOOLEAN
      ensure
         Result = (lower1 <= line and line <= upper1)

   valid_index1 (line: INTEGER): BOOLEAN
      ensure
         Result = (lower1 <= line and line <= upper1)

   valid_column (column: INTEGER): BOOLEAN
      ensure
         Result = (lower2 <= column and column <= upper2)

   valid_index2 (column: INTEGER): BOOLEAN
      ensure
         Result = (lower2 <= column and column <= upper2)

   valid_depth (depth: INTEGER): BOOLEAN
      ensure
         Result = (lower3 <= depth and depth <= upper3)

   valid_index3 (depth: INTEGER): BOOLEAN
      ensure
         Result = (lower3 <= depth and depth <= upper3)

   valid_index (line, column, depth: INTEGER): BOOLEAN
      ensure
         Result = (valid_line(line) and valid_column(column) and valid_depth(depth))

feature(s) from COLLECTION3
   -- Counting:

   count1: INTEGER
      -- Size of the first dimension.
      ensure
         Result = upper1 - lower1 + 1

   line_count: INTEGER
      -- Equivalent of count1.

   count2: INTEGER
      -- Size of the second dimension.
      ensure
         Result = upper2 - lower2 + 1

   column_count: INTEGER

   count3: INTEGER
      -- Size of the third dimension.
      ensure
         Result = upper3 - lower3 + 1

   depth_count: INTEGER

   count: INTEGER
      -- Total number of elements.
      ensure
         Result = line_count * column_count * depth_count

feature(s) from COLLECTION3
   swap (line1, column1, depth1, line2, column2, depth2: INTEGER)
      -- Swap the element at index (line1,column1,depth1)
      -- with the element at index (line2,column2,depth2).
      require
         valid_index(line1,column1,depth1);
         valid_index(line2,column2,depth2)
      ensure
         item(line1,column1,depth1) = old item(line2,column2,depth2);
         item(line2,column2,depth2) = old item(line1,column1,depth1);
         count = old count

   set_all_with (element: E)
      -- Set all item with value v.
      ensure
         count = old count

   clear_all
      -- Set all items to default values.
      ensure
         count = old count;
         all_default

feature(s) from COLLECTION3
   -- Creating or initializing:

   from_collection3 (model: COLLECTION3[E])
      --  Uses model to initialize Current.
      require
         model /= Void
      ensure
         lower1 = model.lower1;
         lower2 = model.lower2;
         lower3 = model.lower3;
         count1 = model.count1;
         count2 = model.count2;
         count3 = model.count3

   from_model (model: COLLECTION[COLLECTION[COLLECTION[E]]])
      -- The model is used to fill line by line the COLLECTION3.
      -- Assume all sub-collections of have the same indexing.
      require
         model /= Void
      ensure
         line_minimum = model.lower;
         line_maximum = model.upper;
         column_minimum = model.first.lower;
         column_maximum = model.first.upper;
         depth_minimum = model.first.first.lower;
         depth_maximum = model.first.first.upper;
         count1 = model.count;
         count2 > 0 implies count2 = model.first.count;
         count3 > 0 implies count3 = model.first.first.count

feature(s) from COLLECTION3
   -- Looking and comparison:

   all_default: BOOLEAN
      -- Do all items have their type's default value?

   is_equal (other: COLLECTION3[E]): BOOLEAN
      -- Do both collections have the same lower1, lower2, lower3, upper1, upper2 and
      -- upper3, and items?
      -- The basic = is used for comparison of items.
      -- See also is_equal_map.
      require
         other /= Void
      ensure
         generating_type = other.generating_type implies Result = other.is_equal(Current)

   is_equal_map (other: COLLECTION3[E]): BOOLEAN
      -- Do both collections have the same lower1, lower2, lower3, upper1, upper2 and upper3,
      -- and items?
      -- Feature is_equal is used for comparison of items.
      -- See also is_equal.

feature(s) from COLLECTION3
   -- Printing:

   fill_tagged_out_memory
      -- Append a viewable information in tagged_out_memory in
      -- order to affect the behavior of out, tagged_out, etc.

feature(s) from COLLECTION3
   -- Miscellaneous features:

   occurrences (elt: E): INTEGER
      -- Number of occurrences using equal.
      -- See also fast_occurrences to choose the apropriate one.
      ensure
         Result >= 0

   fast_occurrences (elt: E): INTEGER
      -- Number of occurrences using =.
      -- See also occurrences to choose the apropriate one.
      ensure
         Result >= 0

   has (x: E): BOOLEAN
      -- Search if a element x is in the array using equal.
      -- See also fast_has to choose the apropriate one.

   fast_has (x: E): BOOLEAN
      --  Search if a element x is in the array using =.

   replace_all (old_value, new_value: E)
      -- Replace all occurrences of the element old_value by new_value
      -- using equal for comparison.
      -- See also fast_replace_all to choose the apropriate one.
      ensure
         count = old count;
         occurrences(old_value) = 0

   fast_replace_all (old_value, new_value: E)
      -- Replace all occurrences of the element old_value by new_value
      -- using operator = for comparison.
      -- See also replace_all to choose the apropriate one.
      ensure
         count = old count;
         fast_occurrences(old_value) = 0

   sub_collection3 (line_min, line_max, column_min, column_max, depth_min, depth_max: INTEGER): like Current
      -- Create a new object using selected area of Current.
      require
         valid_index(line_min,column_min,depth_min);
         valid_index(line_max,column_max,depth_max)
      ensure
         Result.lower1 = line_min;
         Result.lower2 = column_min;
         Result.lower3 = depth_min;
         Result.upper1 = line_max;
         Result.upper2 = column_max;
         Result.upper3 = depth_max;
         Result /= Void

   set_area (element: E; line_min, line_max, column_min, column_max, depth_min, depth_max: INTEGER)
      -- Set all the elements of the selected area rectangle with element.
      require
         valid_index(line_min,column_min,depth_min);
         valid_index(line_max,column_max,depth_max)
      ensure
         count = old count

feature(s) from ARRAY3
   -- Creation / modification:

   make (line_min, line_max, column_min, column_max, depth_min, depth_max: INTEGER)
      -- Reset all bounds line_minimum / line_maximum / column_minimum
      -- column_maximum / depth_min and depth_max using arguments as
      -- new values. All elements are set to the default value of type E.
      require
         line_min <= line_max;
         column_min <= column_max;
         depth_min <= depth_max
      ensure
         line_minimum = line_min;
         line_maximum = line_max;
         column_minimum = column_min;
         column_maximum = column_max;
         depth_minimum = depth_min;
         depth_maximum = depth_max

   from_collection (contents: COLLECTION[E]; line_min, line_max, column_min, column_max, depth_min, depth_max: INTEGER)
      --  Reset all bounds using line_min, line_max, column_min,
      --  column_max, depth_min, and depth_max.
      --  Copy all elements of contents, line by line into Current.
      require
         line_min <= line_max;
         column_min <= column_max;
         depth_min <= depth_max;
         contents.count = (line_max - line_min + 1) * (column_max - column_min + 1) * (depth_max - depth_min + 1)
      ensure
         line_minimum = line_min;
         line_maximum = line_max;
         column_minimum = column_min;
         column_maximum = column_max;
         depth_minimum = depth_min;
         depth_maximum = depth_max;
         count = contents.count

feature(s) from ARRAY3
   -- Resizing:

   resize (line_min, line_max, column_min, column_max, depth_min, depth_max: INTEGER)
      -- Resize bounds of the Current array
      require
         line_max >= line_min;
         column_max >= column_min;
         depth_max >= depth_min
      ensure
         line_minimum = line_min;
         line_maximum = line_max;
         column_minimum = column_min;
         column_maximum = column_max;
         depth_minimum = depth_min;
         depth_maximum = depth_max

feature(s) from ARRAY3
   --  Looking and comparison:

   copy (other: like Current)
      require
         same_dynamic_type(other)
      ensure
         is_equal(other)


invariant

    count1 = upper1 - lower1 + 1;

    count2 = upper2 - lower2 + 1;

    count3 = upper3 - lower3 + 1;

    capacity >= count;

end of ARRAY3[E]