package body Wordfreq_Types is

   procedure Insert
     (Data_Vector : in out Data_Vectors.Vector;
      Word        : in     String) is

      I : Data_Vectors.Index_Subtype := Data_Vector.First_Index;
      J : Data_Vectors.Extended_Index := Data_Vector.Last_Index;
      K : Data_Vectors.Index_Subtype;

      Done : Boolean := False;

      procedure Process (Data : in out Data_Type) is
      begin
         if Data.Word < Word then
            I := K + 1;

         elsif Data.Word > Word then
            J := K - 1;

         else
            Data.Count := Data.Count + 1;
            Done := True;

         end if;
      end Process;

   begin -- Insert

      while I <= J loop

         K := I + (J - I) / 2;

         Data_Vector.Update_Element (K, Process'Access);

         if Done then
            return;
         end if;

      end loop;

      Data_Vector.Insert (I, Data_Type'(Word'Length, Word, 1));

   end Insert;


end Wordfreq_Types;
