tuples.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> <--------------
-- NOTE The TUP classes defined in this file or elsewhere are a fundamental
-- value constructoe defined in the Sather languagefor any number of
-- components of differing classes/types. It may be thought of as
-- providing a record constructor facility for Sather - but this is
-- a purely logical notion which may be implemented in any manner.
immutable class TUP{T1} < $HASH, $STR
immutable class TUP{T1} < $HASH, $STR is
-- This immutable class implements a tuple of a single element.
-- Version 1.2 Nov 98. Copyright K Hopper, U of Waikato
-- Development History
-- -------------------
-- Date Who By Detail
-- ---- ------ ------
-- 12 Apr 94 bg Original
-- 4 Apr 97 kh Changed for INT to CARD
-- 6 Nov 98 kh Refined, added pre/post conditions.
include COMPARABLE ;
include COMPARE{T1} ;
attr t1 : T1 ;
create(
val1 : T1
) : SAME is
-- This creation routine returns the single-valued tuple.
return t1(val1)
end ;
is_eq(
other : SAME
) : BOOL is
-- This predicate returns true if and only if the component of self and
-- other are equal.
return elt_eq(t1,other.t1)
end ;
hash : CARD
pre true -- irrespective of value
post true -- irrespective of result
is
-- This routine returns a hash value computed from the hash value of
-- the component. The component must, therefore, either be of a reference
-- type or have a hash routine.
return elt_hash(t1)
end ;
str(
lib : LIBCHARS
) : STR
pre ~void(lib)
post (result.size >= 3)
is
-- This routine returns a string representation of the tuple using
-- the given repertoire and encoding.
res : FSTR := FSTR::create(lib).push(lib.Left_Bracket.char) ;
loc_t1 : T1 := t1 ;
typecase loc_t1
when $STR then
res := res + loc_t1.str
else
res := res + lib.Asterisk.char
end ;
res := res.push(lib.Right_Bracket.char) ;
return res.str
end ;
str : STR
pre true
post (result.size >= 3)
is
-- This routine returns a string representation of the tuple using
-- the default repertoire and encoding.
return str(LIBCHARS::default)
end ;
end ; -- TUP{T1}
immutable class TUP{T1,T2} < $HASH, $STR
immutable class TUP{T1,T2} < $HASH, $STR is
-- This immutable class implements a tuple of two elements.
-- Version 1.2 Nov 98. Copyright K Hopper, U of Waikato
-- Development History
-- -------------------
-- Date Who By Detail
-- ---- ------ ------
-- 12 Apr 94 bg Original
-- 4 Apr 97 kh Changed for INT to CARD
-- 6 Nov 98 kh Revised string and added pre/post conds.
include COMPARABLE ;
include COMPARE{T1} ;
include COMPARE{T2}
elt_eq -> elt_eq2,
elt_lt -> elt_lt2,
elt_hash -> elt_hash2,
elt_nil -> elt_nil2,
is_elt_nil -> is_elt_nil2 ;
attr t1 : T1 ;
attr t2 : T2 ;
create(
val1 : T1,
val2 : T2
) : SAME is
-- This routine creates a two-component tuple with the given component
-- values.
return t1(val1).t2(val2)
end ;
is_eq(
other : SAME
) : BOOL is
-- This predicate returns true if and only if both of the components of
-- self and other are equal.
if ~elt_eq(t1,other.t1) then
return false
elsif ~elt_eq2(t2,other.t2) then
return false
else
return true
end
end ;
hash : CARD
pre true -- irrespective of value
post true -- irrespective of result
is
-- This routine returns a simple hash value computed from the component
-- hash values of the tuple.
hash_res : NUM_BITS := NUM_BITS::create(elt_hash(t1)) ;
hash_res2 : NUM_BITS := NUM_BITS::create(elt_hash2(t2)) ;
res : NUM_BITS := hash_res.convolve(hash_res2) ;
return res.card
end ;
str(
lib : LIBCHARS,
separator : CHAR
) : STR
pre ~void(lib)
and (separator /= lib.Null.char)
post (result.size >= 5)
is
-- This routine returns a string representation of the tuple using
-- the given repertoire and encoding, elements being separated by the given
-- character.
res : FSTR := FSTR::create(lib).push(lib.Left_Bracket.char) ;
loc_t1 : T1 := t1 ;
typecase loc_t1
when $STR then
res := res + loc_t1.str
else
res := res + lib.Asterisk.char
end ;
res := res.push(separator) ;
loc_t2 : T2 := t2 ;
typecase loc_t2
when $STR then
res := res + loc_t2.str
else
res := res + lib.Asterisk.char
end ;
res := res.push(lib.Right_Bracket.char) ;
return res.str
end ;
str(
lib : LIBCHARS
) : STR
pre ~void(lib)
post (result.size >= 5)
is
-- This routine returns a string representation of the tuple using
-- the given repertoire and encoding, elements being separated by the given
-- character.
loc_sep : CHAR ;
if lib.Decimal_Mark = lib.Comma then
loc_sep := lib.Fullstop.char
else
loc_sep := lib.Comma.char
end ;
return str(lib,loc_sep)
end ;
str : STR
pre true
post (result.size >= 5)
is
-- This routine returns a string representation of the tuple using
-- the default repertoire and encoding.
return str(LIBCHARS::default)
end ;
end ; -- TUP{T1,T2}
immutable class TUP{T1,T2,T3} < $HASH, $STR
immutable class TUP{T1,T2,T3} < $HASH, $STR is
-- This immutable class implements a tuple of three elements.
-- Version 1.2 Bov 98. Copyright K Hopper, U of Waikato
-- Development History
-- -------------------
-- Date Who By Detail
-- ---- ------ ------
-- 12 Apr 94 bg Original
-- 4 Apr 97 kh Changed for INT to CARD
-- 6 Nov 98 kh Revised str, added pre/post conds.
include COMPARABLE ;
private include COMPARE{T1} ;
private include COMPARE{T2}
elt_eq -> elt_eq2,
elt_lt -> elt_lt2,
elt_hash -> elt_hash2,
elt_nil -> elt_nil2,
is_elt_nil -> is_elt_nil2 ;
private include COMPARE{T3}
elt_eq -> elt_eq3,
elt_lt -> elt_lt3,
elt_hash -> elt_hash3,
elt_nil -> elt_nil3,
is_elt_nil -> is_elt_nil3 ;
attr t1 : T1 ;
attr t2 : T2 ;
attr t3 : T3 ;
create(
val1 : T1,
val2 : T2,
val3 : T3
) : SAME is
-- This routine creates a new tuple with the component values given.
return t1(val1).t2(val2).t3(val3)
end ;
is_eq(
other : SAME
) : BOOL is
-- This routine returns true if and only if all of the componenets of
-- self are equal to the corresponding component of other.
if ~elt_eq(t1,other.t1) then
return false
elsif ~elt_eq2(t2,other.t2) then
return false
elsif ~elt_eq3(t3,other.t3) then
return false
else
return true
end
end ;
hash : CARD
pre true -- irrespective of value
post true -- irrespective of result
is
-- This routine returns a simple hash value computed from the component
-- hash values of the tuple.
hash_res : NUM_BITS := NUM_BITS::create(elt_hash(t1)) ;
return hash_res.convolve(NUM_BITS::create(elt_hash2(t2)).convolve(
NUM_BITS::create(elt_hash3(t3)))).card
end ;
str(
lib : LIBCHARS,
separator : CHAR
) : STR
pre ~void(lib)
and (separator /= lib.Null.char)
post (result.size >= 7)
is
-- This routine returns a string representation of the tuple using
-- the given repertoire and encoding, elements being separated by the given
-- character.
res : FSTR := FSTR::create(lib).push(lib.Left_Bracket.char) ;
loc_t1 : T1 := t1 ;
typecase loc_t1
when $STR then
res := res + loc_t1.str
else
res := res + lib.Asterisk.char
end ;
res := res.push(separator) ;
loc_t2 : T2 := t2 ;
typecase loc_t2
when $STR then
res := res + loc_t2.str
else
res := res + lib.Asterisk.char
end ;
res := res.push(separator) ;
loc_t3 : T3 := t3 ;
typecase loc_t3
when $STR then
res := res + loc_t3.str
else
res := res + lib.Asterisk.char
end ;
res := res.push(lib.Right_Bracket.char) ;
return res.str
end ;
str(
lib : LIBCHARS
) : STR
pre ~void(lib)
post (result.size >= 7)
is
-- This routine returns a string representation of the tuple using
-- the given repertoire and encoding, elements being separated by the given
-- character.
loc_sep : CHAR ;
if lib.Decimal_Mark = lib.Comma then
loc_sep := lib.Fullstop.char
else
loc_sep := lib.Comma.char
end ;
return str(lib,loc_sep)
end ;
str : STR
pre true
post (result.size >= 7)
is
-- This routine returns a string representation of the tuple using
-- the default repertoire and encoding.
return str(LIBCHARS::default)
end ;
end ; -- TUP{T1,T2,T3}
immutable class TUP{T1,T2,T3,T4} < $HASH, $STR
immutable class TUP{T1,T2,T3,T4} < $HASH, $STR is
-- This immutable class implements a tuple of four elements.
-- Version 1.2 Nov 98. Copyright K Hopper, U of Waikato
-- Development History
-- -------------------
-- Date Who By Detail
-- ---- ------ ------
-- 12 Apr 94 bg Original
-- 4 Apr 97 kh Changed for INT to CARD
-- 6 Nov 98 kh Revised str, added pre/post conds.
include COMPARABLE ;
private include COMPARE{T1} ;
private include COMPARE{T2}
elt_eq -> elt_eq2,
elt_lt -> elt_lt2,
elt_hash -> elt_hash2,
elt_nil -> elt_nil2,
is_elt_nil -> is_elt_nil2 ;
private include COMPARE{T3}
elt_eq -> elt_eq3,
elt_lt -> elt_lt3,
elt_hash -> elt_hash3,
elt_nil -> elt_nil3,
is_elt_nil -> is_elt_nil3 ;
private include COMPARE{T4}
elt_eq -> elt_eq4,
elt_lt -> elt_lt4,
elt_hash -> elt_hash4,
elt_nil -> elt_nil4,
is_elt_nil -> is_elt_nil4 ;
attr t1 : T1 ;
attr t2 : T2 ;
attr t3 : T3 ;
attr t4 : T4 ;
create(
val1 : T1,
val2 : T2,
val3 : T3,
val4 : T4
) : SAME is
-- This routine returns a four component tuple with the given component
-- values.
return t1(val1).t2(val2).t3(val3).t4(val4)
end ;
is_eq(
other : SAME
) : BOOL is
-- This routine returns true if and only if corresponding components
-- of self and other are equal.
if ~elt_eq(t1,other.t1) then
return false
elsif ~elt_eq2(t2,other.t2) then
return false
elsif ~elt_eq3(t3,other.t3) then
return false
elsif ~elt_eq4(t4,other.t4) then
return false
else
return true
end
end ;
hash : CARD
pre true -- irrespective of value
post true -- irrespective of result
is
-- This routine returns a simple hash value computed from the component
-- hash values of the tuple.
hash_res : NUM_BITS := NUM_BITS::create(elt_hash(t1)) ;
return hash_res.convolve(NUM_BITS::create(elt_hash2(t2)).convolve(
NUM_BITS::create(elt_hash3(t3)).convolve(
NUM_BITS::create(elt_hash4(t4))))).card
end ;
str(
lib : LIBCHARS,
separator : CHAR
) : STR
pre ~void(lib)
and (separator /= lib.Null.char)
post (result.size >= 9)
is
-- This routine returns a string representation of the tuple using
-- the given repertoire and encoding, elements being separated by the given
-- character.
res : FSTR := FSTR::create(lib).push(lib.Left_Bracket.char) ;
loc_t1 : T1 := t1 ;
typecase loc_t1
when $STR then
res := res + loc_t1.str
else
res := res + lib.Asterisk.char
end ;
res := res.push(separator) ;
loc_t2 : T2 := t2 ;
typecase loc_t2
when $STR then
res := res + loc_t2.str
else
res := res + lib.Asterisk.char
end ;
res := res.push(separator) ;
loc_t3 : T3 := t3 ;
typecase loc_t3
when $STR then
res := res + loc_t3.str
else
res := res + lib.Asterisk.char
end ;
res := res.push(separator) ;
loc_t4 : T4 := t4 ;
typecase loc_t4
when $STR then
res := res + loc_t4.str
else
res := res + lib.Asterisk.char
end ;
res := res.push(lib.Right_Bracket.char) ;
return res.str
end ;
str(
lib : LIBCHARS
) : STR
pre ~void(lib)
post (result.size >= 9)
is
-- This routine returns a string representation of the tuple using
-- the given repertoire and encoding, elements being separated by the given
-- character.
loc_sep : CHAR ;
if lib.Decimal_Mark = lib.Comma then
loc_sep := lib.Fullstop.char
else
loc_sep := lib.Comma.char
end ;
return str(lib,loc_sep)
end ;
str : STR
pre true
post (result.size >= 9)
is
-- This routine returns a string representation of the tuple using
-- the default repertoire and encoding.
return str(LIBCHARS::default)
end ;
end ; -- TUP{T1,T2,T3,T4}