comp.lang.ada
 help / color / mirror / Atom feed
From: Georg Bauhaus <rm-host.bauhaus@maps.futureapps.de>
Subject: Re: Pascal Calling Convention
Date: Tue, 29 Mar 2011 01:47:34 +0200
Date: 2011-03-29T01:47:35+02:00	[thread overview]
Message-ID: <4d911e17$0$6776$9b4e6d93@newsspool3.arcor-online.net> (raw)
In-Reply-To: <d68b20bc-3aa5-4cc5-bd8e-a944dee20f24@a11g2000pri.googlegroups.com>

On 3/29/11 12:08 AM, Adam Beneschan wrote:
> On Mar 28, 2:26 pm, Hyman Rosen<hyro...@mail.com>  wrote:
>> On 3/28/2011 5:16 PM, Adam Beneschan wrote:
>>
>>> I hope your "favorite data structure" comment was sarcastic.  I would
>>> expect this code to behave incorrectly a large portion of the time;
>>> specifically, if I read the code right, once you add any element to
>>> the set, the program is likely to think 0 is in the set whether you've
>>> added it or not.
>>
>> I wrote the code from memory so it's possible I made a mistake,
>> but I don't think you're correct - you're likely not reading the
>> code right.
>
> OK, I've looked at it again and you may be right.  However, it's also
> not a relevant example; I don't see how any compiler could reasonably
> detect ...

Won't this example of a set be the kind of Ada program that Ada 2012
analyzers (of aspect annotations) are waiting for?

> On top of that, I don't see why anyone would prefer this over the
> trivial structure that just allocates N bytes, initializes them to 0,
> and sets a byte to 1 to add an element to the set, etc.

I think one can expand the example with little effort to become an
efficient hashed set/map, one that requires very little run-time
support in comparison to Ada.Containers.*, I should think.
(It might have start as one, sorry in case I haven't seen this,
or misrepresent it.)

A transliteration attempt follows, with these additions in mind.

pragma Profile(Ravenscar);
pragma Restrictions (No_Allocators);

generic
     type unsigned is mod <>;
     N : unsigned;  -- capacity, hash(d) < N!
     type t is private;  -- values in sets (maps)
     with function hash (d : t) return unsigned;
     -- must return the `k` parameter of set (map) operations
package Sets is

     type set is tagged private;
     function has(this : set; k : unsigned) return Boolean;
     procedure add(this : in out set; k : unsigned; data : t);
     procedure del(this : in out set; k : unsigned);
     procedure clr(this : in out set);

private
     type uptr is array (unsigned range <>) of unsigned;
     type dptr is array (unsigned range <>) of t;
     type set is tagged record
         n : unsigned := 0;
         d : dptr(0 .. Sets.N - 1);
         s : uptr(0 .. Sets.N - 1);
         -- `hash(d(s(k))) = k` if `d` has been stored at `k`
     end record;
end Sets;

package body Sets is

     function has(this : set; k : unsigned) return Boolean is
         n : unsigned renames this.n;
         s : uptr renames this.s;
         d : dptr renames this.d;
     begin
         return k < Sets.N and then s(k) < n and then hash(d(s(k))) = k;
     end has;

     procedure add(this : in out set; k : unsigned; data : t) is
         n : unsigned renames this.n;
         s : uptr renames this.s;
         d : dptr renames this.d;
     begin
         if k < Sets.N and then not this.has(k) then
             d(n) := data; s(k) := n; n := n + 1;
         end if;
     end add;

     procedure del(this : in out set; k : unsigned) is
         n : unsigned renames this.n;
         s : uptr renames this.s;
         d : dptr renames this.d;
     begin
         if this.has(k) then
             declare i : unsigned := s(k);
             begin n := n - 1; d(i) := d(n); s(hash(d(i))) := i;
             end;
         end if;
     end del;

     procedure clr(this : in out set) is
         n : unsigned renames this.n;
     begin
         n := 0;
     end clr;

end sets;




  reply	other threads:[~2011-03-28 23:47 UTC|newest]

Thread overview: 57+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-03-23 21:37 Pascal Calling Convention Shark8
2011-03-23 23:25 ` Yannick Duchêne (Hibou57)
2011-03-24  0:24   ` Randy Brukardt
2011-03-24  0:43     ` Yannick Duchêne (Hibou57)
2011-03-24  2:04       ` Shark8
2011-03-25 15:40         ` Yannick Duchêne (Hibou57)
     [not found]       ` <F8mdnYCca6tRJBfQnZ2dnUVZ_s-dnZ2d@earthlink.com>
2011-03-24 19:20         ` Keith Thompson
2011-03-25 16:04           ` Robert A Duff
2011-03-25 17:02             ` Hyman Rosen
2011-03-25 17:09               ` Robert A Duff
2011-03-25 17:35                 ` Hyman Rosen
2011-03-26 19:51                   ` Robert A Duff
2011-03-25 17:51             ` Keith Thompson
2011-03-26 20:46               ` Robert A Duff
2011-03-27  2:24                 ` Randy Brukardt
2011-03-28 15:41                   ` Adam Beneschan
2011-03-28 19:52                   ` Robert A Duff
2011-03-29  2:32                     ` Randy Brukardt
2011-03-29  6:06                       ` Shark8
2011-03-29 23:45                         ` Randy Brukardt
2011-03-29 19:19                       ` Robert A Duff
2011-03-30  0:02                         ` Randy Brukardt
2011-03-30 12:40                           ` Robert A Duff
2011-03-30 19:40                             ` Randy Brukardt
2011-03-30 20:56                               ` tmoran
2011-03-30 22:34                                 ` Robert A Duff
2011-03-31 21:00                                   ` Randy Brukardt
2011-03-28 20:29                 ` Hyman Rosen
2011-03-28 21:16                   ` Adam Beneschan
2011-03-28 21:26                     ` Hyman Rosen
2011-03-28 22:08                       ` Adam Beneschan
2011-03-28 23:47                         ` Georg Bauhaus [this message]
2011-03-29 12:23                           ` stefan-lucks
2011-03-29 13:10                             ` Hyman Rosen
2011-03-30 13:42                             ` Phil Clayton
2011-03-31  7:40                               ` Phil Clayton
2011-03-29  2:48                         ` Hyman Rosen
2011-03-29 18:30                           ` Robert A Duff
2011-03-29 23:25                             ` Adam Beneschan
2011-03-30 12:50                               ` Robert A Duff
2011-03-30 14:47                                 ` Adam Beneschan
2011-03-30 18:10                                   ` Robert A Duff
2011-03-29  3:01                         ` Hyman Rosen
2011-03-29 18:22                           ` Robert A Duff
2011-03-26 21:30           ` Florian Weimer
2011-03-27 16:18             ` Robert A Duff
2011-03-27 16:38               ` Florian Weimer
2011-03-27 16:56                 ` Robert A Duff
2011-03-24  2:15   ` Shark8
2011-03-24  0:38 ` ytomino
2011-03-24  2:23   ` Shark8
2011-03-24 21:29 ` Gautier write-only
2011-03-25 12:47 ` Marco
2011-03-25 15:38   ` Yannick Duchêne (Hibou57)
2011-03-26  8:39     ` ObjectAda [was: Pascal Calling Convention] Gautier write-only
2011-03-26 14:05       ` Marco
2011-03-26 21:58         ` Gautier write-only
replies disabled

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