-- This shows the use of GENERIC_SEARCH on simple arrays. with GENERIC_SEARCH ; with GENERIC_SORT ; with TEXT_IO ; use TEXT_IO ; procedure GENERIC_SEARCH_DEMO_1 is -- use each function and procedure -- on a simple array -- Objects being used for testing FIND and SEARCH FOUND : BOOLEAN ; POSITION : INTEGER ; -- stuff being sorted and searched subtype MY_STRING is STRING(1..3) ; ELEMENT : MY_STRING ; type STR_ARRAY is array( INTEGER range <> ) of MY_STRING ; STR_DATA : STR_ARRAY(1..4) := ( "CCC", "DDD", "AAA", "BBB") ; -- -- Note the same generic actual parameters are used for searching as -- were used for sorting -- package STR_SORT is new GENERIC_SORT (MY_STRING,INTEGER,STR_ARRAY) ; package STR_SEARCH is new GENERIC_SEARCH (MY_STRING,INTEGER,STR_ARRAY) ; begin -- Check that LINEAR_SEARCH and LINEAR_FIND can find each array element for I in STR_DATA'RANGE loop STR_SEARCH.LINEAR_SEARCH ( STR_DATA ( I ) , STR_DATA , POSITION , FOUND ) ; if not FOUND then PUT_LINE ( " ERROR ! "); end if ; PUT ( STR_DATA ( POSITION ) ) ; PUT ( " and " ) ; PUT ( STR_SEARCH.LINEAR_FIND ( STR_DATA ( I ) , STR_DATA ) ) ; PUT_LINE ( " found. "); end loop ; -- -- Sort array -- STR_SORT.SORT(STR_DATA) ; -- print results PUT_LINE(" SORTED DATA ") ; for I in STR_DATA'RANGE loop PUT_LINE(STR_DATA(I)) ; end loop ; -- Check that SEARCH and FIND can find each array element for I in STR_DATA'RANGE loop STR_SEARCH.SEARCH ( STR_DATA ( I ) , STR_DATA , POSITION , FOUND ) ; if not FOUND then PUT_LINE ( " ERROR ! "); end if ; PUT ( STR_DATA ( POSITION ) ) ; PUT ( " and " ) ; PUT ( STR_SEARCH.FIND ( STR_DATA ( I ) , STR_DATA ) ) ; PUT_LINE ( " found. "); end loop ; -- More examples can be seen by applying the FIND and SEARCH to the other -- four types in GENERIC_SORT_DEMO_1.ADA end GENERIC_SEARCH_DEMO_1 ;