note
	description: "Simple implementation of coefficients using a native array of characters"
	library: "Gobo Eiffel Decimal Arithmetic Library"
	copyright: "Copyright (c) 2004-2012, Paul G. Crismer and others"
	license: "MIT License"
	date: "$Date: 2016-05-06 19:15:38 +0000 (Fri, 06 May 2016) $"
	revision: "$Revision: 98678 $"

class 
	MA_DECIMAL_COEFFICIENT_IMP

create 
	make,
	make_copy

feature {NONE} -- Initialization

	default_create
			-- Process instances of classes with no creation clause.
			-- (Default: do nothing.)
			-- (from ANY)
		do
		end

	make (a_capacity: INTEGER_32)
			-- Make with a_capacity.
		require -- from MA_DECIMAL_COEFFICIENT
			a_capacity_positive: a_capacity > 0
		do
			digits := Special_digits_.make_filled (0, a_capacity)
			capacity := a_capacity
			set_count (0)
		ensure -- from MA_DECIMAL_COEFFICIENT
			capacity_set: capacity >= a_capacity
			count_zero: count = 0
			lower_set: lower = 0
			upper_set: upper = capacity - 1
		end

	make_copy (other: like Current)
			-- Make a copy of other.
		require -- from MA_DECIMAL_COEFFICIENT
			other_not_void: other /= Void
		do
			make (other.capacity)
			copy (other)
		ensure -- from MA_DECIMAL_COEFFICIENT
			capacity_set: capacity >= other.capacity
			equal_other: is_equal (other)
		end
	
feature -- Access

	generating_type: TYPE [detachable MA_DECIMAL_COEFFICIENT_IMP]
			-- Type of current object
			-- (type of which it is a direct instance)
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			generating_type_not_void: Result /= Void
		end

	generator: STRING_8
			-- Name of current object's generating class
			-- (base class of the type of which it is a direct instance)
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			generator_not_void: Result /= Void
			generator_not_empty: not Result.is_empty
		end

	item (index: INTEGER_32): INTEGER_32
			-- Item at index
		require -- from MA_DECIMAL_COEFFICIENT
			valid_index: valid_index (index)
		do
			Result := digits.item (index).to_integer_32
		end

	msd_index: INTEGER_32
			-- Index of most significant (non-zero) digit
		local
			l_digits: like digits
		do
			Result := count
			from
				l_digits := digits
			until
				Result = 0 or else l_digits.item (Result - 1) /= 0
			loop
				Result := Result - 1
			end
			if Result > 0 then
				Result := Result - 1
			end
		ensure -- from MA_DECIMAL_COEFFICIENT
			msd_index_small_enough: Result < count
			msd_index_large_enough: Result >= 0
			index_of_msd_or_zero: Result > 0 implies item (Result) /= 0
		end

	subcoefficient (index_start, index_end: INTEGER_32): MA_DECIMAL_COEFFICIENT
			-- Subcoefficient made of digits in range [index_start..index_end]
		require -- from MA_DECIMAL_COEFFICIENT
			index_start_big_enough: index_start >= 0
			index_end_big_enough: index_end >= index_start
			index_end_small_enough: index_end <= count - 1
		local
			index: INTEGER_32
		do
			create {MA_DECIMAL_COEFFICIENT_IMP} Result.make (index_end - index_start + 1)
			from
				index := index_start
			until
				index > index_end
			loop
				Result.put (item (index), Result.count)
				index := index + 1
			end
		ensure -- from MA_DECIMAL_COEFFICIENT
			subcoefficient_not_void: Result /= Void
		end
	
feature {NONE} -- Access

	Integer_: KL_INTEGER_ROUTINES
			-- Routines that ought to be in class INTEGER
			-- (from KL_IMPORTED_INTEGER_ROUTINES)
		once
			create Result
		ensure -- from KL_IMPORTED_INTEGER_ROUTINES
			instance_free: class
			integer_routines_not_void: Result /= Void
		end
	
feature -- Measurement

	capacity: INTEGER_32
			-- Capacity

	count: INTEGER_32
			-- Number of decimal digits

	lower: INTEGER_32
			-- Lower index
		do
			Result := 0
		ensure -- from MA_DECIMAL_COEFFICIENT
			definition: Result = 0
		end

	upper: INTEGER_32
			-- Upper index
		do
			Result := capacity - 1
		ensure -- from MA_DECIMAL_COEFFICIENT
			definition: Result <= capacity - 1
		end
	
feature -- Comparison

	frozen deep_equal (a: detachable ANY; b: like arg #1): BOOLEAN
			-- Are a and b either both void
			-- or attached to isomorphic object structures?
			-- (from ANY)
		do
			if a = Void then
				Result := b = Void
			else
				Result := b /= Void and then a.is_deep_equal (b)
			end
		ensure -- from ANY
			instance_free: class
			shallow_implies_deep: standard_equal (a, b) implies Result
			both_or_none_void: (a = Void) implies (Result = (b = Void))
			same_type: (Result and (a /= Void)) implies (b /= Void and then a.same_type (b))
			symmetric: Result implies deep_equal (b, a)
		end

	frozen equal (a: detachable ANY; b: like arg #1): BOOLEAN
			-- Are a and b either both void or attached
			-- to objects considered equal?
			-- (from ANY)
		do
			if a = Void then
				Result := b = Void
			else
				Result := b /= Void and then a.is_equal (b)
			end
		ensure -- from ANY
			instance_free: class
			definition: Result = (a = Void and b = Void) or else ((a /= Void and b /= Void) and then a.is_equal (b))
		end

	frozen is_deep_equal (other: MA_DECIMAL_COEFFICIENT_IMP): BOOLEAN
			-- Are Current and other attached to isomorphic object structures?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		external
			"built_in"
		ensure -- from ANY
			shallow_implies_deep: standard_is_equal (other) implies Result
			same_type: Result implies same_type (other)
			symmetric: Result implies other.is_deep_equal (Current)
		end

	is_greater alias ">" (other: MA_DECIMAL_COEFFICIENT_IMP): BOOLEAN
			-- Is Current greater than other?
			-- (from MA_DECIMAL_COEFFICIENT)
		require -- from PART_COMPARABLE
			other_exists: other /= Void
		do
			Result := (three_way_comparison (other) = 1)
		ensure then -- from COMPARABLE
			definition: Result = (other < Current)
		end

	is_greater_equal alias ">=" (other: MA_DECIMAL_COEFFICIENT_IMP): BOOLEAN
			-- Is current object greater than or equal to other?
			-- (from COMPARABLE)
		require -- from PART_COMPARABLE
			other_exists: other /= Void
		do
			Result := not (Current < other)
		ensure then -- from COMPARABLE
			definition: Result = (other <= Current)
		end

	is_less alias "<" (other: MA_DECIMAL_COEFFICIENT_IMP): BOOLEAN
			-- Is Current less than other?
			-- (from MA_DECIMAL_COEFFICIENT)
		require -- from PART_COMPARABLE
			other_exists: other /= Void
		do
			Result := (three_way_comparison (other) = -1)
		ensure then -- from COMPARABLE
			asymmetric: Result implies not (other < Current)
		end

	is_less_equal alias "<=" (other: MA_DECIMAL_COEFFICIENT_IMP): BOOLEAN
			-- Is current object less than or equal to other?
			-- (from COMPARABLE)
		require -- from PART_COMPARABLE
			other_exists: other /= Void
		do
			Result := not (other < Current)
		ensure then -- from COMPARABLE
			definition: Result = ((Current < other) or (Current ~ other))
		end

	max (other: MA_DECIMAL_COEFFICIENT_IMP): MA_DECIMAL_COEFFICIENT_IMP
			-- The greater of current object and other
			-- (from COMPARABLE)
		require -- from COMPARABLE
			other_exists: other /= Void
		do
			if Current >= other then
				Result := Current
			else
				Result := other
			end
		ensure -- from COMPARABLE
			current_if_not_smaller: Current >= other implies Result = Current
			other_if_smaller: Current < other implies Result = other
		end

	min (other: MA_DECIMAL_COEFFICIENT_IMP): MA_DECIMAL_COEFFICIENT_IMP
			-- The smaller of current object and other
			-- (from COMPARABLE)
		require -- from COMPARABLE
			other_exists: other /= Void
		do
			if Current <= other then
				Result := Current
			else
				Result := other
			end
		ensure -- from COMPARABLE
			current_if_not_greater: Current <= other implies Result = Current
			other_if_greater: Current > other implies Result = other
		end

	frozen standard_equal (a: detachable ANY; b: like arg #1): BOOLEAN
			-- Are a and b either both void or attached to
			-- field-by-field identical objects of the same type?
			-- Always uses default object comparison criterion.
			-- (from ANY)
		do
			if a = Void then
				Result := b = Void
			else
				Result := b /= Void and then a.standard_is_equal (b)
			end
		ensure -- from ANY
			instance_free: class
			definition: Result = (a = Void and b = Void) or else ((a /= Void and b /= Void) and then a.standard_is_equal (b))
		end

	frozen standard_is_equal (other: MA_DECIMAL_COEFFICIENT_IMP): BOOLEAN
			-- Is other attached to an object of the same type
			-- as current object, and field-by-field identical to it?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		external
			"built_in"
		ensure -- from ANY
			same_type: Result implies same_type (other)
			symmetric: Result implies other.standard_is_equal (Current)
		end

	three_way_comparison (other: like Current): INTEGER_32
			-- Compare other; Result is [-1,0,+1] if [Current < other, Current = other, Current > other] respectively
		require -- from COMPARABLE
			other_exists: other /= Void
		local
			index, count_a, count_b, local_difference: INTEGER_32
			l_digits, l_other_digits: like digits
		do
			count_a := msd_index + 1
			count_b := other.msd_index + 1
			if count_a > count_b then
				Result := 1
			elseif count_a < count_b then
				Result := -1
			else
				from
					index := count_a - 1
					l_digits := digits
					l_other_digits := other.digits
				until
					index < 0 or else local_difference /= 0
				loop
					local_difference := l_digits.item (index) - l_other_digits.item (index).to_integer_32
					index := index - 1
				end
				Result := local_difference.sign
			end
		ensure -- from COMPARABLE
			equal_zero: (Result = 0) = (Current ~ other)
			smaller_negative: (Result = -1) = (Current < other)
			greater_positive: (Result = 1) = (Current > other)
		end
	
feature -- Status report

	conforms_to (other: ANY): BOOLEAN
			-- Does type of current object conform to type
			-- of other (as per Eiffel: The Language, chapter 13)?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		external
			"built_in"
		end

	is_one: BOOLEAN
			-- Is Current one?
			-- (from MA_DECIMAL_COEFFICIENT)
		do
			Result := (msd_index = 0 and then item (0) = 1)
		ensure -- from MA_DECIMAL_COEFFICIENT
			definition: Result = (msd_index = 0 and then item (0) = 1)
		end

	is_significant: BOOLEAN
			-- Has Current any non-zero digit?
			-- (from MA_DECIMAL_COEFFICIENT)
		do
			Result := not is_zero
		ensure -- from MA_DECIMAL_COEFFICIENT
			definition: Result = not is_zero
		end

	is_zero: BOOLEAN
			-- Is this coefficient only composed of zeros?
			-- (from MA_DECIMAL_COEFFICIENT)
		local
			i: INTEGER_32
		do
			Result := True
			from
				i := count - 1
			until
				i < 0
			loop
				if item (i) /= 0 then
					Result := False
					i := -1
				else
					i := i - 1
				end
			end
		end

	same_type (other: ANY): BOOLEAN
			-- Is type of current object identical to type of other?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		external
			"built_in"
		ensure -- from ANY
			definition: Result = (conforms_to (other) and other.conforms_to (Current))
		end

	valid_index (index: INTEGER_32): BOOLEAN
			-- Is index valid ?
			-- (from MA_DECIMAL_COEFFICIENT)
		do
			Result := (index >= 0 and then index < count)
		ensure -- from MA_DECIMAL_COEFFICIENT
			definition: Result = (index >= 0 and then index < count)
		end
	
feature -- Element change

	grow (a_capacity: INTEGER_32)
			-- Grow coefficient so that it can contain up to a_capacity digits.
		require -- from MA_DECIMAL_COEFFICIENT
			a_capacity_large_enough: a_capacity >= count
		local
			index, new_upper: INTEGER_32
			l_digits: like digits
		do
			new_upper := a_capacity - 1
			if a_capacity > capacity then
				digits := Special_digits_.aliased_resized_area_with_default (digits, 0, new_upper + 1)
				capacity := new_upper + 1
			end
			from
				index := count
				l_digits := digits
			until
				index > new_upper
			loop
				put (0, index)
				index := index + 1
			end
			set_count (a_capacity)
		ensure -- from MA_DECIMAL_COEFFICIENT
			new_capacity: capacity >= a_capacity
			adapted_count: count = a_capacity
		end

	put (v, index: INTEGER_32)
			-- Put v at index-th item.
		require -- from MA_DECIMAL_COEFFICIENT
			valid_index: index >= 0 and index < capacity
			valid_v: v >= 0 and v <= 9
		do
			digits.put (Integer_.to_integer_8 (v), index)
			if index > count - 1 then
				set_count (index + 1)
			end
		ensure -- from MA_DECIMAL_COEFFICIENT
			item_set: item (index) = v
			adapted_count: (index + 1 > old count) implies count = index + 1
		end

	set_from_substring (s: STRING_8; coefficient_begin, coefficient_end: INTEGER_32)
			-- Set from s, skip the decimal point if it is present.
		require -- from MA_DECIMAL_COEFFICIENT
			s_not_void: s /= Void
			coefficient_begin: coefficient_begin >= 1
			coefficient_end: coefficient_end <= s.count
			coefficient_end_ge_begin: coefficient_end >= coefficient_begin
		local
			i, k: INTEGER_32
			c: CHARACTER_8
			l_digits: like digits
		do
			if s.count > capacity then
				grow (s.count)
			end
			l_digits := digits
			from
				i := coefficient_end
				k := 0
			variant
					i
			until
				i < coefficient_begin
			loop
				c := s.item (i)
				inspect c
				when '0'..'9' then
					put (Integer_.to_integer_8 (c.code - ('0').code).to_integer_32, k)
					k := k + 1
				else
				end
				i := i - 1
			end
			set_count (k)
		end
	
feature -- Conversion

	out: STRING_8
			-- Terse printable representation
		local
			index: INTEGER_32
		do
			create Result.make (count)
			from
				index := count - 1
			until
				index < 0
			loop
				Result.append_string (item (index).out)
				index := index - 1
			end
		ensure -- from ANY
			out_not_void: Result /= Void
		end
	
feature -- Duplication

	frozen clone (other: detachable ANY): like other
		obsolete "Use `twin' instead. [2017-05-31]"
			-- Void if other is void; otherwise new object
			-- equal to other
			--
			-- For non-void other, clone calls copy;
			-- to change copying/cloning semantics, redefine copy.
			-- (from ANY)
		do
			if other /= Void then
				Result := other.twin
			end
		ensure -- from ANY
			instance_free: class
			equal: Result ~ other
		end

	copy (other: like Current)
			-- copy other into Current without aliasing.
		require -- from ANY
			other_not_void: other /= Void
			type_identity: same_type (other)
		local
			index, l_upper: INTEGER_32
			l_digits, l_other_digits: like digits
		do
			if Current /= other then
				l_upper := other.count - 1
				if digits = Void then
					make (l_upper + 1)
				elseif capacity < other.capacity then
					grow (other.capacity)
				end
				from
					index := 0
					l_digits := digits
					l_other_digits := other.digits
				until
					index > l_upper
				loop
					l_digits.put (l_other_digits.item (index), index)
					index := index + 1
				end
				set_count (index)
			end
		ensure -- from ANY
			is_equal: Current ~ other
		end

	frozen deep_clone (other: detachable ANY): like other
		obsolete "Use `deep_twin' instead. [2017-05-31]"
			-- Void if other is void: otherwise, new object structure
			-- recursively duplicated from the one attached to other
			-- (from ANY)
		do
			if other /= Void then
				Result := other.deep_twin
			end
		ensure -- from ANY
			instance_free: class
			deep_equal: deep_equal (other, Result)
		end

	frozen deep_copy (other: MA_DECIMAL_COEFFICIENT_IMP)
			-- Effect equivalent to that of:
			--		copy (other . deep_twin)
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		do
			copy (other.deep_twin)
		ensure -- from ANY
			deep_equal: deep_equal (Current, other)
		end

	frozen deep_twin: MA_DECIMAL_COEFFICIENT_IMP
			-- New object structure recursively duplicated from Current.
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			deep_twin_not_void: Result /= Void
			deep_equal: deep_equal (Current, Result)
		end

	frozen standard_clone (other: detachable ANY): like other
		obsolete "Use `standard_twin' instead. [2017-05-31]"
			-- Void if other is void; otherwise new object
			-- field-by-field identical to other.
			-- Always uses default copying semantics.
			-- (from ANY)
		do
			if other /= Void then
				Result := other.standard_twin
			end
		ensure -- from ANY
			instance_free: class
			equal: standard_equal (Result, other)
		end

	frozen standard_copy (other: MA_DECIMAL_COEFFICIENT_IMP)
			-- Copy every field of other onto corresponding field
			-- of current object.
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
			type_identity: same_type (other)
		external
			"built_in"
		ensure -- from ANY
			is_standard_equal: standard_is_equal (other)
		end

	frozen standard_twin: MA_DECIMAL_COEFFICIENT_IMP
			-- New object field-by-field identical to other.
			-- Always uses default copying semantics.
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			standard_twin_not_void: Result /= Void
			equal: standard_equal (Result, Current)
		end

	to_twin: like Current
			-- Cloned version of Current
		do
			create Result.make_copy (Current)
		ensure -- from MA_DECIMAL_COEFFICIENT
			twin_not_void: Result /= Void
			twin_not_current: Result /= Current
			tiwn_equal_current: Result.is_equal (Current)
		end

	frozen twin: MA_DECIMAL_COEFFICIENT_IMP
			-- New object equal to Current
			-- twin calls copy; to change copying/twinning semantics, redefine copy.
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			twin_not_void: Result /= Void
			is_equal: Result ~ Current
		end
	
feature -- Basic operations

	frozen as_attached: attached MA_DECIMAL_COEFFICIENT_IMP
		obsolete "Remove calls to this feature. [2017-05-31]"
			-- Attached version of Current.
			-- (Can be used during transitional period to convert
			-- non-void-safe classes to void-safe ones.)
			-- (from ANY)
		do
			Result := Current
		end

	frozen default: detachable MA_DECIMAL_COEFFICIENT_IMP
			-- Default value of object's type
			-- (from ANY)
		do
		end

	frozen default_pointer: POINTER
			-- Default value of type POINTER
			-- (Avoid the need to write p.default for
			-- some p of type POINTER.)
			-- (from ANY)
		do
		ensure -- from ANY
			instance_free: class
		end

	default_rescue
			-- Process exception for routines with no Rescue clause.
			-- (Default: do nothing.)
			-- (from ANY)
		do
		end

	frozen do_nothing
			-- Execute a null action.
			-- (from ANY)
		do
		ensure -- from ANY
			instance_free: class
		end

	integer_add (other: like Current)
			-- Integer add of other to Current.
		require -- from MA_DECIMAL_COEFFICIENT
			other_not_void: other /= Void
			same_count: count <= other.count
		local
			carry: INTEGER_32
			index: INTEGER_32
			digit: INTEGER_32
			l_digits, l_other_digits: like digits
		do
			from
				carry := 0
				index := 0
				l_digits := digits
				l_other_digits := other.digits
			until
				index = count
			loop
				carry := carry + l_digits.item (index).to_integer_32 + l_other_digits.item (index).to_integer_32
				digit := carry \\ 10
				l_digits.put (Integer_.to_integer_8 (digit), index)
				carry := carry // 10
				index := index + 1
			end
			if carry /= 0 then
				grow (count + 1)
				l_digits.put (Integer_.to_integer_8 (carry), index)
			end
		ensure -- from MA_DECIMAL_COEFFICIENT
			new_count: count >= old count
		end

	integer_multiply (a, b: like Current)
			-- Multiply a, b into Current.
		require -- from MA_DECIMAL_COEFFICIENT
			a_not_void: a /= Void
			b_not_void: b /= Void
			capacity_sufficient: capacity > a.count + b.count
		local
			i, j, carry, digit: INTEGER_32
			local_a, local_b: like Current
			l_digits: like digits
			l_b_count, l_a_count: INTEGER_32
			l_b_digits, l_a_digits: like digits
			i_plus_j: INTEGER_32
		do
			from
				i := 0
				l_digits := digits
			until
				i >= count
			loop
				l_digits.put (Integer_.to_integer_8 (0), i)
				i := i + 1
			end
			from
				if a.count > b.count then
					local_a := a
					local_b := b
				else
					local_a := b
					local_b := a
				end
				l_a_digits := local_a.digits
				l_b_digits := local_b.digits
				l_a_count := local_a.count
				l_b_count := local_b.count
				i := 0
			until
				i >= l_b_count
			loop
				digit := l_b_digits.item (i).to_integer_32
				carry := 0
				from
					j := 0
					i_plus_j := i + j
				until
					j >= l_a_count
				loop
					carry := carry + digit * l_a_digits.item (j).to_integer_32 + l_digits.item (i_plus_j).to_integer_32
					l_digits.put (Integer_.to_integer_8 (carry \\ 10), i_plus_j)
					carry := carry // 10
					j := j + 1
					i_plus_j := i_plus_j + 1
				end
				if carry > 0 then
					l_digits.put (Integer_.to_integer_8 (carry), i_plus_j)
				end
				i := i + 1
			end
			set_count (i + j)
			strip_leading_zeroes
		end

	integer_quick_add_msd (other, digits_count: INTEGER_32)
			-- Integer add of other (between 0 and 9) to Current,
			-- restricted to digits_count most significant digits.
		require -- from MA_DECIMAL_COEFFICIENT
			other_limits: other >= 0 and other <= 9
			digits_count: digits_count >= 0 and digits_count <= count
		local
			carry: INTEGER_32
			index: INTEGER_32
			digit: INTEGER_32
			l_digits: like digits
			start: INTEGER_32
			l_count: INTEGER_32
		do
			from
				l_digits := digits
				l_count := count
				carry := other
				start := count - digits_count
				index := start
			until
				index >= l_count
			loop
				carry := carry + l_digits.item (index).to_integer_32
				digit := carry \\ 10
				l_digits.put (Integer_.to_integer_8 (digit), index - start)
				carry := carry // 10
				index := index + 1
			end
			if carry /= 0 then
				l_digits.put (Integer_.to_integer_8 (carry), index - start)
				set_count (digits_count + 1)
			else
				set_count (digits_count)
			end
		ensure -- from MA_DECIMAL_COEFFICIENT
			new_count: count <= digits_count + 1
		end

	integer_quick_subtract_msd (other, digits_count: INTEGER_32)
			-- Integer subtract of other (between 0 and 9) to Current
			-- restricted to digits_count most significant digits.
		require -- from MA_DECIMAL_COEFFICIENT
			other_limits: other >= 0 and other <= 9
			digits_count: digits_count >= 0 and digits_count <= count
		local
			borrow: INTEGER_32
			index, l_count, start: INTEGER_32
			l_digits: like digits
		do
			from
				borrow := - other
				l_count := count
				l_digits := digits
				start := count - digits_count
				index := start
			until
				index = l_count
			loop
				borrow := borrow + l_digits.item (index).to_integer_32
				if borrow < 0 then
					l_digits.put (Integer_.to_integer_8 (10 + borrow), index - start)
					borrow := -1
				else
					l_digits.put (Integer_.to_integer_8 (borrow), index)
					borrow := 0
				end
				index := index + 1
			end
			strip_leading_zeroes
		ensure -- from MA_DECIMAL_COEFFICIENT
			new_count: count <= digits_count + 1
		end

	integer_subtract (other: like Current)
			-- Integer subtract of other to Current,
			-- provided that result will not be negative.
		require -- from MA_DECIMAL_COEFFICIENT
			other_not_void: other /= Void
			other_smaller: other <= Current
		local
			borrow: INTEGER_32
			index: INTEGER_32
			l_count: INTEGER_32
			l_digits, l_other_digits: like digits
		do
			l_digits := digits
			l_other_digits := other.digits
			from
				borrow := 0
				index := 0
				l_count := other.msd_index + 1
			until
				index = l_count
			loop
				borrow := borrow + l_digits.item (index).to_integer_32 - l_other_digits.item (index).to_integer_32
				if borrow < 0 then
					l_digits.put (Integer_.to_integer_8 (10 + borrow), index)
					borrow := -1
				else
					l_digits.put (Integer_.to_integer_8 (borrow), index)
					borrow := 0
				end
				index := index + 1
			end
			from
			until
				index = count
			loop
				borrow := borrow + l_digits.item (index).to_integer_32
				if borrow < 0 then
					l_digits.put (Integer_.to_integer_8 (10 + borrow), index)
					borrow := -1
				else
					l_digits.put (Integer_.to_integer_8 (borrow), index)
					borrow := 0
				end
				index := index + 1
			end
			strip_leading_zeroes
		end

	is_equal (other: like Current): BOOLEAN
			-- Are Current and other considered equal?
		require -- from ANY
			other_not_void: other /= Void
		local
			index: INTEGER_32
			l_digits, l_other_digits: like digits
		do
			if other.count = count then
				from
					index := count - 1
					l_digits := digits
					l_other_digits := other.digits
				variant
						index + 1
				until
					index < lower or else l_digits.item (index) /= l_other_digits.item (index)
				loop
					index := index - 1
				end
				Result := (index < lower)
			end
		ensure -- from ANY
			symmetric: Result implies other ~ Current
			consistent: standard_is_equal (other) implies Result
		ensure then -- from COMPARABLE
			trichotomy: Result = (not (Current < other) and not (other < Current))
		end

	keep_head (a_count: INTEGER_32)
			-- Keep head of 'a_count' digits.
		require -- from MA_DECIMAL_COEFFICIENT
			a_count_valid: a_count > 0 and a_count <= count
		local
			index: INTEGER_32
			l_digits: like digits
		do
			from
				index := a_count
				l_digits := digits
			until
				index > upper
			loop
				l_digits.put (Integer_.to_integer_8 (0), index)
				index := index + 1
			end
			set_count (a_count)
		ensure -- from MA_DECIMAL_COEFFICIENT
			adapted_count: count = a_count
			unchanged_capacity: capacity = old capacity
		end

	shift_left (a_count: INTEGER_32)
			-- Shift items left.
		require -- from MA_DECIMAL_COEFFICIENT
			a_count_greater_zero: a_count > 0
		local
			index: INTEGER_32
			l_digits: like digits
		do
			grow (count + a_count)
			from
				index := count - 1
				l_digits := digits
			until
				index < a_count
			loop
				l_digits.put (l_digits.item (index - a_count), index)
				index := index - 1
			end
			from
			until
				index < 0
			loop
				l_digits.put (Integer_.to_integer_8 (0), index)
				index := index - 1
			end
		ensure -- from MA_DECIMAL_COEFFICIENT
			adapted_count: count = old count + a_count
			zero_shifted: item (0) = 0
		end

	shift_right (a_count: INTEGER_32)
			-- Shift items right.
		require -- from MA_DECIMAL_COEFFICIENT
			a_count_greater_zero: a_count > 0
			a_count_less_count: a_count <= count
		local
			index: INTEGER_32
			l_digits: like digits
		do
			from
				index := a_count
				l_digits := digits
			until
				index >= count
			loop
				l_digits.put (l_digits.item (index), index - a_count)
				index := index + 1
			end
			from
				index := count - a_count
			until
				index >= count
			loop
				l_digits.put (Integer_.to_integer_8 (0), index)
				index := index + 1
			end
		ensure -- from MA_DECIMAL_COEFFICIENT
			adapted_count: count = old count
			zero_shifted: item (count - 1) = 0
		end
	
feature {MA_DECIMAL, MA_DECIMAL_COEFFICIENT} -- Basic operations

	strip_leading_zeroes
			-- Strip leading zeroes.
			-- (from MA_DECIMAL_COEFFICIENT)
		local
			index: INTEGER_32
		do
			index := msd_index
			set_count (index + 1)
		ensure -- from MA_DECIMAL_COEFFICIENT
			no_leading_zero: count > 1 implies item (count - 1) /= 0
		end
	
feature {MA_DECIMAL_COEFFICIENT_IMP} -- Implementation

	digits: SPECIAL [INTEGER_8]
			-- Array of digits (with the least significant first)

	Special_digits_: KL_SPECIAL_ROUTINES [INTEGER_8]
			-- Routines that ought to be in class SPECIAL
		once
			create Result
		ensure
			native_digits_array_routines_not_void: Result /= Void
		end
	
feature {MA_DECIMAL} -- Implementation

	set_count (a_count: INTEGER_32)
			-- Set count to a_count.
		require -- from MA_DECIMAL_COEFFICIENT
			a_count_small_enough: a_count <= capacity
			a_count_large_enough: a_count >= 0
		do
			count := a_count
		ensure -- from MA_DECIMAL_COEFFICIENT
			count_set: count = a_count
		end
	
feature -- Output

	Io: STD_FILES
			-- Handle to standard file setup
			-- (from ANY)
		once
			create Result
			Result.set_output_default
		ensure -- from ANY
			instance_free: class
			io_not_void: Result /= Void
		end

	print (o: detachable ANY)
			-- Write terse external representation of o
			-- on standard output.
			-- (from ANY)
		do
			if o /= Void then
				Io.put_string (o.out)
			end
		ensure -- from ANY
			instance_free: class
		end

	frozen tagged_out: STRING_8
			-- New string containing terse printable representation
			-- of current object
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			tagged_out_not_void: Result /= Void
		end
	
feature -- Platform

	Operating_environment: OPERATING_ENVIRONMENT
			-- Objects available from the operating system
			-- (from ANY)
		once
			create Result
		ensure -- from ANY
			instance_free: class
			operating_environment_not_void: Result /= Void
		end
	
feature {NONE} -- Retrieval

	frozen internal_correct_mismatch
			-- Called from runtime to perform a proper dynamic dispatch on correct_mismatch
			-- from MISMATCH_CORRECTOR.
			-- (from ANY)
		local
			l_msg: STRING_8
			l_exc: EXCEPTIONS
		do
			if attached {MISMATCH_CORRECTOR} Current as l_corrector then
				l_corrector.correct_mismatch
			else
				create l_msg.make_from_string ("Mismatch: ")
				create l_exc
				l_msg.append (generating_type.name)
				l_exc.raise_retrieval_exception (l_msg)
			end
		end
	
invariant
	digits_not_void: digits /= Void

		-- from MA_DECIMAL_COEFFICIENT
	count_less_or_equal_capacity: count <= capacity
	count_not_negative: count >= 0
	lower_upper_consistent: lower <= upper

		-- from COMPARABLE
	irreflexive_comparison: not (Current < Current)

		-- from ANY
	reflexive_equality: standard_is_equal (Current)
	reflexive_conformance: conforms_to (Current)

end -- class MA_DECIMAL_COEFFICIENT_IMP

Generated by ISE EiffelStudio