comp.lang.ada
 help / color / mirror / Atom feed
From: "David C. Hoos" <david.c.hoos.sr@ada95.com>
Subject: Re: Parity using 'Write, was Re: Calculate and set Parity
Date: Wed, 18 Sep 2002 13:47:52 -0500
Date: 2002-09-18T13:47:52-05:00	[thread overview]
Message-ID: <mailman.1032374882.10581.comp.lang.ada@ada.eu.org> (raw)
In-Reply-To: 0C3i9.459302$me6.55005@sccrnsc01

I think it would be better to return a type Parity_Kind is (Even, Odd),
rather than a Boolean, or else name the function Odd_Parity (or
Even_Parity), so that True and False have obvious meanings.

----- Original Message -----
From: <tmoran@acm.org>
Newsgroups: comp.lang.ada
To: <comp.lang.ada@ada.eu.org>
Sent: Wednesday, September 18, 2002 1:29 PM
Subject: Parity using 'Write, was Re: Calculate and set Parity


> Assuming a reasonable compiler implementation of streams, where extra
> bits from base type expansion are zeros, here's a way to calculate
> the parity (or byte checksum) of an arbitrary record using 'write.
> It accumulates a checksum in a stream object, and uses functions to
> fetch a byte or a parity boolean from that accumulated checksum element.
>   S : aliased Xors.Stream_Type;
>   type something is record ...
>   ...
>   S.Check_Element := 0;
>   Some_Record_Type'Write(S'access, R);
>   Parity := Xors.Parity(S);
>   Checksum_Byte := Xors.Check_Byte(S);
>   ...
>
> with Ada.Streams;
> package Xors is
>
>   type Stream_Type is new Ada.Streams.Root_Stream_Type with record
>     Check_Element: Ada.Streams.Stream_Element := 0;
>   end record;
>
>   procedure Read(Stream : in out Stream_Type; -- nop
>                  Item   :    out Ada.Streams.Stream_Element_Array;
>                  Last   :    out Ada.Streams.Stream_Element_Offset);
>
>   procedure Write(Stream : in out Stream_Type;
>                   Item   : in     Ada.Streams.Stream_Element_Array);
>
>   function Check_Byte(Stream : Stream_Type) return
Ada.Streams.Stream_Element;
>
>   function Parity(Stream : Stream_Type) return Boolean;
>
> end Xors;
>
> package body Xors is
>   use Ada.Streams;
>
>   procedure Read(Stream : in out Stream_Type; -- nop
>                  Item   :    out Stream_Element_Array;
>                  Last   :    out Stream_Element_Offset) is
>   begin
>     Last := Item'First - 1;
>   end Read;
>
>   procedure Write(Stream : in out Stream_Type;
>                   Item   : in     Stream_Element_Array) is
>   begin
>     for I in Item'range loop
>       Stream.Check_Element := Stream.Check_Element xor Item(I);
>     end loop;
>   end Write;
>
>   function Check_Byte(Stream : Stream_Type) return Stream_Element is
>     Source  : Stream_Element := Stream.Check_Element;
>     Target  : Stream_Element := 0;
>   begin
>     while Source /= 0 loop
>       Target := Target xor (Source mod 255);
>       -- assume integer'size >= stream_element'size
>       Source := Stream_Element(Integer(Source) / 256);
>     end loop;
>     return Target;
>   end Check_Byte;
>
>   function Parity(Stream : Stream_Type) return Boolean is
>     Source  : Stream_Element := Check_Byte(Stream);
>     Target  : Boolean := False;
>   begin
>     while Source /= 0 loop
>       Target := Target xor ((Source mod 2) = 1);
>       Source := Source / 2;
>     end loop;
>     return Target;
>   end Parity;
>
> end Xors;
> _______________________________________________
> comp.lang.ada mailing list
> comp.lang.ada@ada.eu.org
> http://ada.eu.org/mailman/listinfo/comp.lang.ada
>




  reply	other threads:[~2002-09-18 18:47 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-09-16 16:42 Calculate and set Parity Sebastian
2002-09-16 18:14 ` Stephen Leake
2002-09-16 18:58 ` Jeffrey Carter
2002-09-16 19:51   ` tmoran
2002-09-17  1:36   ` tmoran
2002-09-17 22:05 ` Nick Roberts
2002-09-17 23:23   ` tmoran
2002-09-18  0:17   ` tmoran
2002-09-18 20:25     ` Nick Roberts
2002-09-18  2:52 ` SteveD
2002-09-18 18:29 ` Parity using 'Write, was " tmoran
2002-09-18 18:47   ` David C. Hoos [this message]
2002-09-18 22:17     ` tmoran
replies disabled

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