File : a-coprnu.adb


   1 ------------------------------------------------------------------------------
   2 --                                                                          --
   3 --                         GNAT LIBRARY COMPONENTS                          --
   4 --                                                                          --
   5 --         A D A . C O N T A I N E R S . P R I M E _ N U M B E R S          --
   6 --                                                                          --
   7 --                                 B o d y                                  --
   8 --                                                                          --
   9 --          Copyright (C) 2004-2009, Free Software Foundation, Inc.         --
  10 --                                                                          --
  11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
  12 -- terms of the  GNU General Public License as published  by the Free Soft- --
  13 -- ware  Foundation;  either version 3,  or (at your option) any later ver- --
  14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
  15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
  16 -- or FITNESS FOR A PARTICULAR PURPOSE.                                     --
  17 --                                                                          --
  18 --                                                                          --
  19 --                                                                          --
  20 --                                                                          --
  21 --                                                                          --
  22 -- You should have received a copy of the GNU General Public License and    --
  23 -- a copy of the GCC Runtime Library Exception along with this program;     --
  24 -- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
  25 -- <http://www.gnu.org/licenses/>.                                          --
  26 --                                                                          --
  27 -- This unit was originally developed by Matthew J Heaney.                  --
  28 ------------------------------------------------------------------------------
  29 
  30 package body Ada.Containers.Prime_Numbers is
  31 
  32    --------------
  33    -- To_Prime --
  34    --------------
  35 
  36    function To_Prime (Length : Count_Type) return Hash_Type is
  37       I, J, K : Integer'Base;
  38       Index   : Integer'Base;
  39 
  40    begin
  41       I := Primes'Last - Primes'First;
  42       Index := Primes'First;
  43       while I > 0 loop
  44          J := I / 2;
  45          K := Index + J;
  46 
  47          if Primes (K) < Hash_Type (Length) then
  48             Index := K + 1;
  49             I := I - J - 1;
  50          else
  51             I := J;
  52          end if;
  53       end loop;
  54 
  55       return Primes (Index);
  56    end To_Prime;
  57 
  58 end Ada.Containers.Prime_Numbers;