Re: Copying string slices before calling subroutines?
- From: "Dmitry A. Kazakov" <mailbox@xxxxxxxxxxxxxxxxx>
- Date: Sat, 5 May 2007 09:41:57 +0200
On Fri, 04 May 2007 23:27:57 +0100, Simon Wright wrote:
My reworking begins
function Index
(Source : String;
Pattern : String;
Going : Ada.Strings.Direction := Ada.Strings.Forward;
Mapping : Ada.Strings.Maps.Character_Mapping
:= Ada.Strings.Maps.Identity) return Natural
is
Cur_Index : Natural;
Potential_Match : Boolean;
use Ada.Strings;
use Ada.Strings.Maps;
begin
if Pattern = "" then
raise Pattern_Error;
end if;
-- Forwards case
if Going = Forward then
for J in 1 .. Source'Length - Pattern'Length + 1 loop
Cur_Index := Source'First + J - 1;
Potential_Match := True;
for K in Pattern'Range loop
if Pattern (K) /=
Value (Mapping, Source (Cur_Index + K - 1)) then
Potential_Match := False;
exit;
end if;
end loop;
if Potential_Match then
return Cur_Index;
end if;
end loop;
which calls Ada.Strings.Maps.Value rather more often than I suppose it
could.
You can save remapping of Source, just there is no need to do it in advance
and allocate a full copy of Source. Instead of that, you make a ring buffer
of mod Pattern'Length where you store Value (Mapping, Source (i)). Once you
advance the main string index you "rotate" the buffer.
Then Pattern'Length = 1 should be a special case, as well as identity
mapping. Though, I don't know how efficient the latter could be.
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
.
- References:
- Re: Reading and writing a big file in Ada (GNAT) on Windows XP
- From: george
- Re: Reading and writing a big file in Ada (GNAT) on Windows XP
- From: Fionn Mac Cumhaill
- Re: Reading and writing a big file in Ada (GNAT) on Windows XP
- From: Simon Wright
- Alternative Index implementation? (Was: Reading and writing a big file in Ada (GNAT) on Windows XP)
- From: Jacob Sparre Andersen
- Re: Alternative Index implementation? (Was: Reading and writing a big file in Ada (GNAT) on Windows XP)
- From: Dmitry A. Kazakov
- Copying string slices before calling subroutines? (Was: Alternative Index implementation?)
- From: Jacob Sparre Andersen
- Re: Copying string slices before calling subroutines?
- From: Jacob Sparre Andersen
- Re: Copying string slices before calling subroutines?
- From: Dmitry A. Kazakov
- Re: Copying string slices before calling subroutines?
- From: Jeffrey Creem
- Re: Copying string slices before calling subroutines?
- From: Simon Wright
- Re: Reading and writing a big file in Ada (GNAT) on Windows XP
- Prev by Date: Re: Copying string slices before calling subroutines?
- Next by Date: Re: Copying string slices before calling subroutines?
- Previous by thread: Re: Copying string slices before calling subroutines?
- Next by thread: Re: Reading and writing a big file in Ada (GNAT) on Windows XP
- Index(es):
Relevant Pages
|