linear.sa
Generated by gen_html_sa_files from ICSI. Contact gomes@icsi.berkeley.edu for details
-------------------------> GNU Sather - sourcefile <-------------------------
-- Copyright (C) 2000 by K Hopper, University of Waikato, New Zealand --
-- This file is part of the GNU Sather library. It is free software; you may --
-- redistribute and/or modify it under the terms of the GNU Library General --
-- Public License (LGPL) as published by the Free Software Foundation; --
-- either version 2 of the license, or (at your option) any later version. --
-- This library is distributed in the hope that it will be useful, but --
-- WITHOUT ANY WARRANTY without even the implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE. See Doc/LGPL for more details. --
-- The license text is also available from: Free Software Foundation, Inc., --
-- 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA --
--------------> Please email comments to <bug-sather@gnu.org> <--------------
abstract class $DIMS
abstract class $DIMS is
-- This abstraction covers all single dimension geometric classes.
-- Version 1.0 Jun 96. Copyright K Hopper, U of Waikato
-- Development History
-- -------------------
-- Date Who By Detail
-- ---- ------ ------
-- 11 Jun 96 kh Original
end ; -- $DIMS
immutable class LENGTH < $DIMS, $IS_EQ, $IS_LT{LENGTH}, $HASH,
immutable class LENGTH < $DIMS, $IS_EQ, $IS_LT{LENGTH}, $HASH,
$IMMUTABLE is
-- This class is provided to associate a linear dimension with a numeric
-- value. All internal values are maintained in micrometres, to simplify
-- and speed up all computation.
-- NOTE Lengths may not be negative! Zero is the lowest value!
-- Version 1.2 Jan 99. Copyright K Hopper, U of Waikato
-- Development History
-- -------------------
-- Date Who By Detail
-- ---- ------ ------
-- 9 Jan 97 kh Original extracted from FLT.
-- 6 Aug 97 kh Internal simplification for speed.
-- 11 Jan 99 kh added $IMMUTABLE sub-typing
include COMPARABLE ;
include BINARY ;
private const Microfactor : FLT := 1000.0 ;
const null : SAME := create(0.0) ;
private attr val : FLT ;
private flt : FLT is
-- This private routine returns the value of self as a floating point
-- number which is the length in micro-metres!
return self
end ;
build(
cursor : BIN_CURSOR
) : SAME
pre ~void(cursor)
and ~cursor.is_done
post true
is
-- This routine creates a new dimension object from the indicated binary
-- string.
return val(FLT::build(cursor))
end ;
create(
mag : FLT
) : SAME
pre (mag >= 0.0) is
-- This routine creates a new value as defined by the argument given
-- which defaults to a measurement in millimetres. The stored value is in
-- micrometres.
return val(mag * Microfactor)
end ;
create(
mag : FLT,
dim : UNITS
) : SAME
pre (mag >= 0.0) is
-- This routine creates a new value as defined by the arguments given.
-- The stored value is in micrometres.
return val(mag * dim.factor(UNITS::Millimetres) * Microfactor)
end ;
create(
mag : FLT,
dim : STR
) : SAME
pre (mag >= 0.0) is
-- This routine creates a new value as defined by the arguments given.
-- The stored value is in micrometres.
loc_units : UNITS := UNITS::create(dim) ;
if loc_units.is_nil then -- invalid anyway!
return null
else
return val(mag * loc_units.factor(UNITS::Millimetres) * Microfactor)
end
end ;
lgth : FLT is
-- This routine returns the value of self in millimetres (the default).
return val / Microfactor
end ;
lgth(
unit : UNITS
) : FLT is
-- This routine returns the value of self in the given units.
return val / (Microfactor * unit.factor(UNITS::Millimetres))
end ;
is_eq(
other : SAME
) : BOOL is
-- This routine returns true if and only if other and self have the same
-- value.
return val = other.val
end ;
is_lt(
other : SAME
) : BOOL is
-- This routine returns true if and only if self is less than other.
return val < other.val
end ;
plus(
other : SAME
) : SAME is
-- This routine adds together two measurements, to give the resulting
-- dimension.
return val(val + other.val)
end ;
minus(
other : SAME
) : SAME
pre (val >= other.val)
post result + other = self
is
-- This routine subtracts other from self provided that the result would
-- be non-negative.
return val(val - other.val)
end ;
times(
factor : FLT
) : SAME
pre factor >= 0.0
post result.val / factor = self.val
is
-- This routine multiplies the size by factor -- providing that
-- this is not less than zero!
return val(val * factor)
end ;
div(
factor : FLT
) : SAME
pre (factor > 0.0) is
-- This routine divides the size by factor -- providing that
-- this is greater than zero!
return val(val / factor)
end ;
div(
other : SAME
) : FLT is
-- This routine divides two dimensions producing a scale factor / ratio.
return self.val / other.val
end ;
binstr : BINSTR
pre true
post (result.size > 0)
is
-- This routine returns a binary string representation of self.
return val.binstr
end ;
hash : CARD is
-- This routine returns a hash value for the dimension.
return val.hash
end ;
nodim_str(
unit : UNITS,
lib : LIBCHARS
) : STR is
-- This provides a string representation in the unit specified using
-- the given repertoire and encoding.
loc_factor : FLT := Microfactor * unit.factor(UNITS::Millimetres) ;
return (val / loc_factor).str
end ;
str(
unit : UNITS,
lib : LIBCHARS
) : STR is
-- This provides a string representation in the unit specified using
-- the given repertoire and encoding.
return nodim_str(unit,lib) + unit.str(lib)
end ;
str(
lib : LIBCHARS
) : STR is
-- This provides a string representation in millimetres using
-- the given repertoire and encoding.
return str(UNITS::Millimetres,lib)
end ;
str(
unit : UNITS
) : STR is
-- This provides a string representation in the unit specified using
-- the current repertoire and encoding.
return str(unit,LIBCHARS::default)
end ;
str : STR is
-- This provides a string representation in the default unit of
-- millimetres using the current repertoire and encoding.
return str(UNITS::Millimetres,LIBCHARS::default)
end ;
end ; -- LENGTH