home *** CD-ROM | disk | FTP | other *** search
- -- $Source: /home/harp/1/proto/monoBANK/winnt/win32-search.adb,v $
- -- $Revision: 1.6 $ $Date: 95/02/06 12:57:17 $ $Author: mg $
- -------------------------------------------------------------------------------
- --
- -- THIS FILE AND ANY ASSOCIATED DOCUMENTATION IS FURNISHED "AS IS" WITHOUT
- -- WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
- -- TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
- -- PURPOSE. The user assumes the entire risk as to the accuracy and the
- -- use of this file.
- --
- -- Copyright (c) Intermetrics, Inc. 1995
- -- Royalty-free, unlimited, worldwide, non-exclusive use, modification,
- -- reproduction and further distribution of this file is permitted.
- --
- -------------------------------------------------------------------------------
-
-
- with Ada.Unchecked_Conversion;
- with Interfaces.C;
- with System;
-
- package body Win32.Search is
-
- type AC_Elem is access constant Elem;
-
- subtype Unsigned is Interfaces.C.Unsigned;
-
- Elem_Size: constant Unsigned := Unsigned(Elem'Size/System.Storage_Unit);
-
- function Compare (Left, Right : AC_Elem) return Integer;
- -- pragma Convention (Stdcall, Compare);
-
- function Compare (Left, Right : AC_Elem) return Integer is
- begin
- if Left.All = Right.all then
- return 0;
- elsif Left.all < Right.all then
- return -1;
- else
- return 1;
- end if;
- end Compare;
-
- type Compare_Func is access function (Left, Right : AC_Elem) return Integer;
-
- function c_bsearch(key : AC_Elem;
- base : AC_Elem;
- num : Unsigned;
- width : Unsigned;
- compare: Compare_Func)
- return AC_Elem;
- -- pragma Convention(Stdcall, Compare_Func);
-
- function c_lsearch(key : AC_Elem;
- base : access Elem;
- num : access Unsigned;
- width : Unsigned;
- compare: Compare_Func)
- return AC_Elem;
-
- function c_lfind(key : AC_Elem;
- base : AC_Elem;
- num : access Unsigned;
- width : Unsigned;
- compare: Compare_Func)
- return AC_Elem;
-
- procedure c_qsort(base : access Elem;
- num : Unsigned;
- width : Unsigned;
- compar: Compare_Func);
-
- pragma Import(C, c_bsearch, "bsearch");
- pragma Import(C, c_lsearch, "_lsearch"); -- Windows NT
- pragma Import(C, c_lfind, "_lfind"); -- Windows NT
- -- pragma Import(C, c_lsearch, "lsearch"); -- Sun
- -- pragma Import(C, c_lfind, "lfind"); -- Sun
- pragma Import(C, c_qsort, "qsort");
-
- Dummy: Elem;
-
- function "-" (Left, Right: AC_Elem) return Integer is
- -- Interfaces.C.Pointers not working right at the time of writing.
- function To_Int is new Ada.Unchecked_Conversion (AC_Elem, Integer);
- begin
- return (To_Int(Left) - To_Int(Right)) / Integer(Elem_Size);
- end "-";
-
- procedure Bsearch(The_Key : in Elem;
- The_Array : in Array_of_Elem;
- Result : out Index) is
- Local_Copy: aliased constant Elem := The_Key;
- Res : AC_Elem := c_bsearch(
- Local_Copy'unchecked_access,
- The_Array(The_Array'First)'unchecked_access,
- The_Array'Length,
- Elem_Size,
- Compare'access);
- begin
- if Res = null then -- assumes Ada and C null are the same
- Result := Not_Found;
- else
- Result := The_Array'First +
- Index(Res - The_Array(The_Array'First)'unchecked_access);
- end if;
- end Bsearch;
-
- procedure LSearch(The_Key : in Elem;
- The_Array : in out Array_of_Elem;
- Last_Valid_Elem : in Index;
- Result : out Index) is
- Local_Copy: aliased constant Elem := The_Key;
- Res : AC_Elem;
- Len : aliased Unsigned :=
- Unsigned(Last_Valid_Elem-The_Array'First+1);
- begin
- if Last_Valid_Elem < The_Array'Last then
- Res := C_Lsearch(Local_Copy'unchecked_access,
- The_Array(The_Array'First)'unchecked_access,
- Len'access,
- Elem_Size,
- Compare'access);
- Result := The_Array'First +
- Index(Res - The_Array(The_Array'First)'unchecked_access);
- else
- Lfind(The_Key, The_Array, Result);
- if Result = Not_Found then
- raise Constraint_Error;
- end if;
- end if;
- end Lsearch;
-
-
- procedure Lfind(The_Key : in Elem;
- The_Array : in Array_of_Elem;
- Result : out Index) is
-
- Local_Copy: aliased constant Elem := The_Key;
- Len : aliased Unsigned := The_Array'Length;
- Res : AC_Elem := c_lfind(
- Local_Copy'unchecked_access,
- The_Array(The_Array'First)'unchecked_access,
- Len'access,
- Elem_Size,
- Compare'access);
- begin
- if Res = null then -- assumes Ada and C null are the same
- Result := Not_Found;
- else
- Result := The_Array'First +
- Index(Res - The_Array(The_Array'First)'unchecked_access);
- end if;
- end Lfind;
-
- procedure Qsort(The_Array : in out Array_of_Elem) is
- begin
- C_Qsort(The_Array(The_Array'First)'access,
- The_Array'Length,
- Elem_Size,
- Compare'access);
- end Qsort;
-
- end Win32.Search;
-