note
	description: "Routines that ought to be in class INTEGER"
	library: "Gobo Eiffel Kernel Library"
	copyright: "Copyright (c) 1999-2018, Eric Bezault and others"
	license: "MIT License"
	date: "$Date: 2019-02-07 22:54:15 +0000 (Thu, 07 Feb 2019) $"
	revision: "$Revision: 102807 $"

class 
	KL_INTEGER_ROUTINES

create 
	default_create

feature {NONE} -- Initialization

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

	Any_: KL_ANY_ROUTINES
			-- Routines that ought to be in class ANY
			-- (from KL_IMPORTED_ANY_ROUTINES)
		once
			create Result
		ensure -- from KL_IMPORTED_ANY_ROUTINES
			instance_free: class
			any_routines_not_void: Result /= Void
		end

	generating_type: TYPE [detachable KL_INTEGER_ROUTINES]
			-- 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

	Platform: KL_PLATFORM
			-- Platform-dependent properties
			-- (from KL_SHARED_PLATFORM)
		once
			create Result
		ensure -- from KL_SHARED_PLATFORM
			instance_free: class
			platform_not_void: Result /= Void
		end

	String_: KL_STRING_ROUTINES
			-- Routines that ought to be in class STRING
			-- (from KL_IMPORTED_STRING_ROUTINES)
		once
			create Result
		ensure -- from KL_IMPORTED_STRING_ROUTINES
			instance_free: class
			string_routines_not_void: Result /= Void
		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: KL_INTEGER_ROUTINES): 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_equal (other: KL_INTEGER_ROUTINES): BOOLEAN
			-- Is other attached to an object considered
			-- equal to current object?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		external
			"built_in"
		ensure -- from ANY
			symmetric: Result implies other ~ Current
			consistent: standard_is_equal (other) implies Result
		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: KL_INTEGER_ROUTINES): 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
	
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_even (an_int: INTEGER_32): BOOLEAN
			-- Is an_int an even integer?
		do
			Result := an_int \\ 2 = 0
		ensure
			instance_free: class
			definition: Result = (an_int \\ 2 = 0)
		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
	
feature -- Conversion

	to_character (an_int: INTEGER_32): CHARACTER_8
			-- Character whose code is an_int
		require
			an_int_large_enough: an_int >= Platform.Minimum_character_code
			an_int_small_enough: an_int <= Platform.Maximum_character_code
		do
			Result := an_int.to_character_8
		ensure
			instance_free: class
			valid_character_code: Result.code = an_int
		end

	to_decimal (an_int: INTEGER_32): STRING_8
			-- Decimal representation of an_int;
			-- Return a new string at each call.
		do
			create Result.make (10)
			append_decimal_integer (an_int, Result)
		ensure
			instance_free: class
			decimal_not_void: Result /= Void
			is_string: Any_.same_types (Result, "")
		end

	to_hexadecimal (an_int: INTEGER_32; uppercase: BOOLEAN): STRING_8
			-- Hexadecimal representation of an_int;
			-- Return a new string at each call.
		require
			an_int_positive: an_int >= 0
		do
			create Result.make (8)
			append_hexadecimal_integer (an_int, Result, uppercase)
		ensure
			instance_free: class
			hexadecimal_not_void: Result /= Void
			is_string: Any_.same_types (Result, "")
		end

	to_integer (an_int: INTEGER_32): INTEGER_32
			-- Return an_int;
			-- This can be used to force integer manifest constants to
			-- be of INTEGER type -- some versions of SmartEiffel use
			-- the smallest possible INTEGER_* type. A manifest array
			-- can thus be forced to be of type ARRAY [INTEGER].
		do
			Result := an_int
		ensure
			instance_free: class
			definition: Result = an_int
		end

	to_integer_8 (an_int: INTEGER_32): INTEGER_8
			-- Convert to INTEGER_8
		require
			an_int_large_enouh: an_int >= -128
			an_int_small_enouh: an_int <= 127
		do
			Result := an_int.to_integer_8
		ensure
			instance_free: class
		end

	to_octal (an_int: INTEGER_32): STRING_8
			-- Octal representation of an_int;
			-- Return a new string at each call.
		require
			an_int_positive: an_int >= 0
		do
			create Result.make (10)
			append_octal_integer (an_int, Result)
		ensure
			instance_free: class
			octal_not_void: Result /= Void
			is_string: Any_.same_types (Result, "")
		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: KL_INTEGER_ROUTINES)
			-- Update current object using fields of object attached
			-- to other, so as to yield equal objects.
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
			type_identity: same_type (other)
		external
			"built_in"
		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: KL_INTEGER_ROUTINES)
			-- 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: KL_INTEGER_ROUTINES
			-- 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: KL_INTEGER_ROUTINES)
			-- 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: KL_INTEGER_ROUTINES
			-- 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

	frozen twin: KL_INTEGER_ROUTINES
			-- 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 KL_INTEGER_ROUTINES
		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 KL_INTEGER_ROUTINES
			-- 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
	
feature -- Operation(s)

	bit_and (m, n: INTEGER_32): INTEGER_32
			-- Bitwise 'and' between m and n
		do
			Result := m.bit_and (n)
		ensure
			instance_free: class
		end

	bit_not (n: INTEGER_32): INTEGER_32
			-- Bitwise 'not' of n
		do
			Result := n.bit_not
		ensure
			instance_free: class
		end

	bit_or (m, n: INTEGER_32): INTEGER_32
			-- Bitwise 'or' between m and n
		do
			Result := m.bit_or (n)
		ensure
			instance_free: class
		end

	bit_shift_left (m, n: INTEGER_32): INTEGER_32
			-- m shifted n bits to left;
			-- Note: The new bits added on the right are 0.
		require
			thirty_two_bit_shift: 0 <= n and n < 32
		do
			Result := m.bit_shift_left (n)
		ensure
			instance_free: class
		end

	bit_shift_right (m, n: INTEGER_32): INTEGER_32
			-- m shifted n bits to right;
			-- Note: If the first bit of m is set (i.e. the sign bit), then
			-- the new bits added on the left are 1; otherwise they are 0.
		require
			thirty_two_bit_shift: 0 <= n and n < 32
		do
			Result := m.bit_shift_right (n)
		ensure
			instance_free: class
		end

	bit_xor (m, n: INTEGER_32): INTEGER_32
			-- Bitwise 'xor' between m and n
		do
			Result := m.bit_xor (n)
		ensure
			instance_free: class
		end

	div (x, n: INTEGER_32): INTEGER_32
			-- Integer division of x by n
			-- (Use ISO C99 specification)
		require
			divisible: n /= 0
			overflow: x = Platform.Minimum_integer implies n /= -1
		do
			Result := x // n
		ensure
			instance_free: class
			definition: Result * n + mod (x, n) = x
		end

	mod (x, n: INTEGER_32): INTEGER_32
			-- Reminder of the integer division of x by n
			-- (Use ISO C99 specification)
		require
			divisible: n /= 0
		do
			Result := x \\ n
		ensure
			instance_free: class
			definition1: n /= Platform.Minimum_integer implies Result.abs < n.abs
			definition2: n = Platform.Minimum_integer implies Result.abs <= Platform.Maximum_integer
			iso_c99: Result /= 0 implies Result.sign = x.sign
		end

	power (x, n: INTEGER_32): INTEGER_32
			-- Integer x to the power of n (x ^ n)
		require
			positive_n: n >= 0
		do
			if n = 0 then
				Result := 1
			elseif n = 1 then
				Result := x
			elseif x /= 0 then
				if is_even (n) then
					Result := power (x, n // 2)
					Result := Result * Result
				else
					Result := power (x, n - 1) * x
				end
			end
		ensure
			instance_free: class
			zero_power_n: x = 0 and n /= 0 implies Result = 0
			x_power_0: n = 0 implies Result = 1
			recursive_definition: n > 0 implies (Result = x * power (x, n - 1))
		end
	
feature -- Output

	append_decimal_integer (an_int: INTEGER_32; a_string: STRING_8)
			-- Append decimal representation of an_int to a_string.
			-- Note: works even when a_string is a UC_STRING.
		require
			a_string_not_void: a_string /= Void
		local
			i, k: INTEGER_32
		do
			if an_int = 0 then
				a_string.append_character ('0')
			elseif an_int < 0 then
				a_string.append_character ('-')
				k := - (an_int + 1)
				i := k // 10
				inspect k \\ 10
				when 0 then
					if i /= 0 then
						append_decimal_integer (i, a_string)
					end
					a_string.append_character ('1')
				when 1 then
					if i /= 0 then
						append_decimal_integer (i, a_string)
					end
					a_string.append_character ('2')
				when 2 then
					if i /= 0 then
						append_decimal_integer (i, a_string)
					end
					a_string.append_character ('3')
				when 3 then
					if i /= 0 then
						append_decimal_integer (i, a_string)
					end
					a_string.append_character ('4')
				when 4 then
					if i /= 0 then
						append_decimal_integer (i, a_string)
					end
					a_string.append_character ('5')
				when 5 then
					if i /= 0 then
						append_decimal_integer (i, a_string)
					end
					a_string.append_character ('6')
				when 6 then
					if i /= 0 then
						append_decimal_integer (i, a_string)
					end
					a_string.append_character ('7')
				when 7 then
					if i /= 0 then
						append_decimal_integer (i, a_string)
					end
					a_string.append_character ('8')
				when 8 then
					if i /= 0 then
						append_decimal_integer (i, a_string)
					end
					a_string.append_character ('9')
				when 9 then
					append_decimal_integer (i + 1, a_string)
					a_string.append_character ('0')
				end
			else
				k := an_int
				i := k // 10
				if i /= 0 then
					append_decimal_integer (i, a_string)
				end
				inspect k \\ 10
				when 0 then
					a_string.append_character ('0')
				when 1 then
					a_string.append_character ('1')
				when 2 then
					a_string.append_character ('2')
				when 3 then
					a_string.append_character ('3')
				when 4 then
					a_string.append_character ('4')
				when 5 then
					a_string.append_character ('5')
				when 6 then
					a_string.append_character ('6')
				when 7 then
					a_string.append_character ('7')
				when 8 then
					a_string.append_character ('8')
				when 9 then
					a_string.append_character ('9')
				end
			end
		ensure
			instance_free: class
		end

	append_hexadecimal_integer (an_int: INTEGER_32; a_string: STRING_8; uppercase: BOOLEAN)
			-- Append a hexadecimal representation of an_int to a_string.
			-- Note: works even when a_string is a UC_STRING.
		require
			an_int_positive: an_int >= 0
			a_string_not_void: a_string /= Void
		local
			k: INTEGER_32
		do
			if an_int = 0 then
				a_string.append_character ('0')
			else
				k := an_int // 16
				if k /= 0 then
					append_hexadecimal_integer (k, a_string, uppercase)
				end
				inspect an_int \\ 16
				when 0 then
					a_string.append_character ('0')
				when 1 then
					a_string.append_character ('1')
				when 2 then
					a_string.append_character ('2')
				when 3 then
					a_string.append_character ('3')
				when 4 then
					a_string.append_character ('4')
				when 5 then
					a_string.append_character ('5')
				when 6 then
					a_string.append_character ('6')
				when 7 then
					a_string.append_character ('7')
				when 8 then
					a_string.append_character ('8')
				when 9 then
					a_string.append_character ('9')
				when 10 then
					if uppercase then
						a_string.append_character ('A')
					else
						a_string.append_character ('a')
					end
				when 11 then
					if uppercase then
						a_string.append_character ('B')
					else
						a_string.append_character ('b')
					end
				when 12 then
					if uppercase then
						a_string.append_character ('C')
					else
						a_string.append_character ('c')
					end
				when 13 then
					if uppercase then
						a_string.append_character ('D')
					else
						a_string.append_character ('d')
					end
				when 14 then
					if uppercase then
						a_string.append_character ('E')
					else
						a_string.append_character ('e')
					end
				when 15 then
					if uppercase then
						a_string.append_character ('F')
					else
						a_string.append_character ('f')
					end
				end
			end
		ensure
			instance_free: class
		end

	append_octal_integer (an_int: INTEGER_32; a_string: STRING_8)
			-- Append octal representation of an_int to a_string.
			-- Note: works even when a_string is a UC_STRING.
		require
			an_int_positive: an_int >= 0
			a_string_not_void: a_string /= Void
		local
			k: INTEGER_32
		do
			if an_int = 0 then
				a_string.append_character ('0')
			else
				k := an_int // 8
				if k /= 0 then
					append_octal_integer (k, a_string)
				end
				inspect an_int \\ 8
				when 0 then
					a_string.append_character ('0')
				when 1 then
					a_string.append_character ('1')
				when 2 then
					a_string.append_character ('2')
				when 3 then
					a_string.append_character ('3')
				when 4 then
					a_string.append_character ('4')
				when 5 then
					a_string.append_character ('5')
				when 6 then
					a_string.append_character ('6')
				when 7 then
					a_string.append_character ('7')
				end
			end
		ensure
			instance_free: class
		end

	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

	out: STRING_8
			-- New string containing terse printable representation
			-- of current object
			-- (from ANY)
		do
			Result := tagged_out
		ensure -- from ANY
			out_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
		-- from ANY
	reflexive_equality: standard_is_equal (Current)
	reflexive_conformance: conforms_to (Current)

end -- class KL_INTEGER_ROUTINES

Generated by ISE EiffelStudio