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 interface
	MA_DECIMAL_COEFFICIENT_IMP

create 
	make (a_capacity: INTEGER_32)
			-- Make with a_capacity.
		require -- from MA_DECIMAL_COEFFICIENT
			a_capacity_positive: a_capacity > 0
		ensure -- from MA_DECIMAL_COEFFICIENT
			capacity_set: capacity >= a_capacity
			count_zero: count = 0
			lower_set: lower = 0
			upper_set: upper = capacity - 1

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

feature -- Access

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

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

	item (index: INTEGER_32): INTEGER_32
			-- Item at index
		require -- from MA_DECIMAL_COEFFICIENT
			valid_index: valid_index (index)

	msd_index: INTEGER_32
			-- Index of most significant (non-zero) digit
		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

	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
		ensure -- from MA_DECIMAL_COEFFICIENT
			subcoefficient_not_void: Result /= Void
	
feature -- Measurement

	capacity: INTEGER_32
			-- Capacity

	count: INTEGER_32
			-- Number of decimal digits

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

	upper: INTEGER_32
			-- Upper index
		ensure -- from MA_DECIMAL_COEFFICIENT
			definition: Result <= capacity - 1
	
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)
		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)

	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)
		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))

	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
		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)

	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
		ensure then -- from COMPARABLE
			definition: Result = (other < Current)

	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
		ensure then -- from COMPARABLE
			definition: Result = (other <= Current)

	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
		ensure then -- from COMPARABLE
			asymmetric: Result implies not (other < Current)

	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
		ensure then -- from COMPARABLE
			definition: Result = ((Current < other) or (Current ~ other))

	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
		ensure -- from COMPARABLE
			current_if_not_smaller: Current >= other implies Result = Current
			other_if_smaller: Current < other implies Result = other

	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
		ensure -- from COMPARABLE
			current_if_not_greater: Current <= other implies Result = Current
			other_if_greater: Current > other implies Result = other

	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)
		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))

	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
		ensure -- from ANY
			same_type: Result implies same_type (other)
			symmetric: Result implies other.standard_is_equal (Current)

	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
		ensure -- from COMPARABLE
			equal_zero: (Result = 0) = (Current ~ other)
			smaller_negative: (Result = -1) = (Current < other)
			greater_positive: (Result = 1) = (Current > other)
	
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

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

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

	is_zero: BOOLEAN
			-- Is this coefficient only composed of zeros?
			-- (from MA_DECIMAL_COEFFICIENT)

	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
		ensure -- from ANY
			definition: Result = (conforms_to (other) and other.conforms_to (Current))

	valid_index (index: INTEGER_32): BOOLEAN
			-- Is index valid ?
			-- (from MA_DECIMAL_COEFFICIENT)
		ensure -- from MA_DECIMAL_COEFFICIENT
			definition: Result = (index >= 0 and then index < count)
	
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
		ensure -- from MA_DECIMAL_COEFFICIENT
			new_capacity: capacity >= a_capacity
			adapted_count: count = a_capacity

	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
		ensure -- from MA_DECIMAL_COEFFICIENT
			item_set: item (index) = v
			adapted_count: (index + 1 > old count) implies count = index + 1

	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
	
feature -- Conversion

	out: STRING_8
			-- Terse printable representation
		ensure -- from ANY
			out_not_void: Result /= Void
	
feature -- Duplication

	copy (other: like Current)
			-- copy other into Current without aliasing.
		require -- from ANY
			other_not_void: other /= Void
			type_identity: same_type (other)
		ensure -- from ANY
			is_equal: Current ~ other

	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
		ensure -- from ANY
			deep_equal: deep_equal (Current, other)

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

	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)
		ensure -- from ANY
			is_standard_equal: standard_is_equal (other)

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

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

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

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

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

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

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

	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
		ensure -- from MA_DECIMAL_COEFFICIENT
			new_count: count >= old count

	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

	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
		ensure -- from MA_DECIMAL_COEFFICIENT
			new_count: count <= digits_count + 1

	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
		ensure -- from MA_DECIMAL_COEFFICIENT
			new_count: count <= digits_count + 1

	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

	is_equal (other: like Current): BOOLEAN
			-- Are Current and other considered equal?
		require -- from ANY
			other_not_void: other /= Void
		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))

	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
		ensure -- from MA_DECIMAL_COEFFICIENT
			adapted_count: count = a_count
			unchanged_capacity: capacity = old capacity

	shift_left (a_count: INTEGER_32)
			-- Shift items left.
		require -- from MA_DECIMAL_COEFFICIENT
			a_count_greater_zero: a_count > 0
		ensure -- from MA_DECIMAL_COEFFICIENT
			adapted_count: count = old count + a_count
			zero_shifted: item (0) = 0

	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
		ensure -- from MA_DECIMAL_COEFFICIENT
			adapted_count: count = old count
			zero_shifted: item (count - 1) = 0
	
feature -- Output

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

	print (o: detachable ANY)
			-- Write terse external representation of o
			-- on standard output.
			-- (from ANY)
		ensure -- from ANY
			instance_free: class

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

	Operating_environment: OPERATING_ENVIRONMENT
			-- Objects available from the operating system
			-- (from ANY)
		ensure -- from ANY
			instance_free: class
			operating_environment_not_void: Result /= Void
	
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