comp.lang.ada
 help / color / mirror / Atom feed
* Looking for generic hash function
@ 1996-08-16  0:00 Robert Grandy
  1996-08-16  0:00 ` Robert Dewar
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Robert Grandy @ 1996-08-16  0:00 UTC (permalink / raw)



Hello,

I am looking for a function that will return a random-type integer based
on a generic parameter. What I have in mind is:

generic 
  type data_type is private;
function get_data_id return Integer;

The function would return a pretty much random Integer based on its
generic parameter. I was thinking of using the 'size attribute, but
does anyone have any other suggestions.

Thanks,

Robert Grandy
4th year Computer Science 
Technical University of Nova Scotia





^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Looking for generic hash function
  1996-08-16  0:00 Looking for generic hash function Robert Grandy
  1996-08-16  0:00 ` Robert Dewar
@ 1996-08-16  0:00 ` Stephen & Tammy House
  1996-08-19  0:00 ` Robert I. Eachus
  2 siblings, 0 replies; 5+ messages in thread
From: Stephen & Tammy House @ 1996-08-16  0:00 UTC (permalink / raw)
  To: Robert Grandy


Robert Grandy wrote:
> 
> Hello,
> 
> I am looking for a function that will return a random-type integer based
> on a generic parameter. What I have in mind is:
> 
> generic
>   type data_type is private;
> function get_data_id return Integer;

You could try taking in one step further with:
generic
  type data_type is private;
  type id is (<>);
function data_id (d : data_type) return id;

function data_id (d : data_type) return id is
  type integers is array (integer range <>) of integer;
  local_d : constant data_type := d;
  --alias I integer array to same location as local_d, basically
begin
  return --normalize sum of all I into id'range
end data_id;

I don't remember all the syntax of alises, and I'd have to work out the
conversion into the id'range, but this should work for big or small
data_type structures.  Of course, if data_type'size < integer'size,
you'll need to have an alternate plan going.  You might make a local
integer type based upon min(data_type'size, standard.integer'size) to
avoid this problem.




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Looking for generic hash function
  1996-08-16  0:00 Looking for generic hash function Robert Grandy
@ 1996-08-16  0:00 ` Robert Dewar
  1996-08-16  0:00 ` Stephen & Tammy House
  1996-08-19  0:00 ` Robert I. Eachus
  2 siblings, 0 replies; 5+ messages in thread
From: Robert Dewar @ 1996-08-16  0:00 UTC (permalink / raw)



Robert G. said

"The function would return a pretty much random Integer based on its
generic parameter. I was thinking of using the 'size attribute, but
does anyone have any other suggestions."

Why not use the bits 
of a default initialized variable of the type?





^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Looking for generic hash function
  1996-08-16  0:00 Looking for generic hash function Robert Grandy
  1996-08-16  0:00 ` Robert Dewar
  1996-08-16  0:00 ` Stephen & Tammy House
@ 1996-08-19  0:00 ` Robert I. Eachus
  1996-08-21  0:00   ` Stephen & Tammy House
  2 siblings, 1 reply; 5+ messages in thread
From: Robert I. Eachus @ 1996-08-19  0:00 UTC (permalink / raw)



In article <4v23jd$mg9@News.Dal.Ca> grandyre@newton.ccs.tuns.ca (Robert Grandy) writes:

  > The function would return a pretty much random Integer based on its
  > generic parameter. I was thinking of using the 'size attribute, but
  > does anyone have any other suggestions.

   This is one case where I wouldn't complain about someone converting
a value of type System.Address to an Integer (assuming Ada 95):

  with System.Storage_Elements;
  function get_data_id return Integer is 
    Foo: Integer;
  begin
    return Integer(System.Storage_Elements.To_Integer(Foo'ADDRESS));
  exception
    when others =>
      return Integer(System.Storage_Elements.To_Integer(Foo'ADDRESS)
         mod System.Storage_Elements.Integer_Address(Integer'LAST));
      -- this return statement can only raise an exception when the
      -- previous one does not.
  end get_data_id;

    Note that, if Integer is 32-bits then every instance will create
its own return value.  And if all instances are at library level and
one per type, then the value returned for a particular type will be
unique within the partition.  If this is not what you are looking for
there are other variants.


					Robert I. Eachus

with Standard_Disclaimer;
use  Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...
--

					Robert I. Eachus

with Standard_Disclaimer;
use  Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Looking for generic hash function
  1996-08-19  0:00 ` Robert I. Eachus
@ 1996-08-21  0:00   ` Stephen & Tammy House
  0 siblings, 0 replies; 5+ messages in thread
From: Stephen & Tammy House @ 1996-08-21  0:00 UTC (permalink / raw)
  To: Robert I. Eachus


Robert I. Eachus wrote:
> 
> In article <4v23jd$mg9@News.Dal.Ca> grandyre@newton.ccs.tuns.ca (Robert Grandy) writes:
> 
>   > The function would return a pretty much random Integer based on its
>   > generic parameter. I was thinking of using the 'size attribute, 

Most hash functions are used to map a record into a structure (read
array).  So the data_type from the example I left before should be the
key to the record being stored/retrieved.  Your hash will make a nice
random number generator, but if the record needed is ever passed by
value through the system, you'll never find it.




^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~1996-08-21  0:00 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-08-16  0:00 Looking for generic hash function Robert Grandy
1996-08-16  0:00 ` Robert Dewar
1996-08-16  0:00 ` Stephen & Tammy House
1996-08-19  0:00 ` Robert I. Eachus
1996-08-21  0:00   ` Stephen & Tammy House

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox