comp.lang.ada
 help / color / mirror / Atom feed
From: stefan-lucks@see-the.signature
Subject: Re: Pascal Calling Convention
Date: Tue, 29 Mar 2011 14:23:49 +0200
Date: 2011-03-29T14:23:49+02:00	[thread overview]
Message-ID: <Pine.LNX.4.64.1103291415240.11824@medsec1.medien.uni-weimar.de> (raw)
In-Reply-To: <4d911e17$0$6776$9b4e6d93@newsspool3.arcor-online.net>

On Tue, 29 Mar 2011, Georg Bauhaus wrote:

> A transliteration attempt follows, with these additions in mind.

Very nice, indeed. Here is a bare minimum version, which is closer to the 
original proposal. Note that in Ada we don't need to perform the explicit 
check for K < N, unlike C++.

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

generic
    type Elements is mod <>;
package USets is

    type Set is tagged private;

    function Has(This  : Set; K : Elements) return Boolean;
    -- K has been stored in This

    procedure Add(This : in out Set; K: Elements);
      -- Insert K into This. Don't do anything if K is already there.

    procedure Del(This : in out Set; K: Elements);
      -- Remove K from This. Don't do anything if K isn't there.

    procedure clr(This : in out Set);
      -- Set This to the empty set.

private
   type A_Type is array (Elements) of Elements;
   subtype Counter_Type is Integer range 0 .. Integer(Elements'Last)+1;
    type Set is tagged record
        N    : Counter_Type := 0;
        S, D : A_Type; -- if I is in Set then D(S(I))=I
    end record;
end USets;

---------

package body USets is

   function Has(This : Set; K: Elements) return Boolean is
      N: Counter_Type renames This.N;
      S: A_Type renames This.S;
      D: A_Type renames This.D;
   begin
      return Integer(S(K)) < N and then D(S(K))=K;
   end Has;

   procedure Add(This : in out Set; K: Elements) is
      N: Counter_Type renames This.N;
      S: A_Type renames This.S;
      D: A_Type renames This.D;
   begin
      if not This.Has(K) then
         D(Elements(N)) := K; S(K) := Elements(N); -- now D(S(K))=K;
         N := N + 1;
      end if;
   end Add;

   procedure Del(This : in out Set; K: Elements) is
      N: Counter_Type renames This.N;
      S: A_Type renames This.S;
      D: A_Type renames This.D;
   begin
      if This.Has(K) then
         declare
            I: Elements := S(K);
         begin
            N := N - 1;
            D(I) := D(Elements(N)); S(D(I)) := I;
         end;
      end if;
   end Del;

   procedure Clr(This : in out Set) is
      N : Counter_Type renames This.N;
   begin
      N := 0;
   end Clr;
end USets;

---------

The original C++ from Hyman Rosen was

template <unsigned N>
class set
{
    unsigned n;    // number of members in set
    unsigned d[N]; // members are in d[0..n-1]
    unsigned s[N]; // d[s[k]] == k if k is a member
    // unspecified elements of d and s are arbitrary

public:
    set() : n(0) { }
    bool has(unsigned k)
        { return k < N && s[k] < n && d[s[k]] == k; }
    void add(unsigned k)
        { if (k < N && !has(k)) { d[n] = k; s[k] = n++; }
    void del(unsigned k)
        { if (has(k)) { unsigned i = s[k]; d[i] = d[--n]; s[d[i]] = i; }
    void clr() { n = 0; }
};



-- 
------ Stefan Lucks   --  Bauhaus-University Weimar  --   Germany  ------
               Stefan dot Lucks at uni minus weimar dot de
------  I  love  the  taste  of  Cryptanalysis  in  the  morning!  ------




  reply	other threads:[~2011-03-29 12:23 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
2011-03-29 12:23                           ` stefan-lucks [this message]
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