1. Inserting to a sequence

What is the fastest way of inserting an item into a sequence? I know ED.EX
uses the following two methods:

1) For inserting a char into a line:

   -- NOTE: this is paraphrased from insert_string()
   line = line[1..b_col-1] & char & line[b_col..length(line)]

2) For inserting a line into a file:

   -- NOTE: this is paraphrased from insert()
   buffer = append(buffer, 0)
   for i = length(buffer)-1 to b_line by -1 do
      buffer[i+1] = buffer[i]
   end for
   buffer[b_line] = line

Is it safe to assume, then, that method #1 is best for relatively short
sequences (such as program file lines), while method #2 is best for
potentially huge sequences (such as big text files)? Or are there other
sequence insertion methods which work better? Anyone done any tests on this?

The reason I ask is because I'm working on a program where I want to insert
data into a sorted sequence, and it would be very impractical for me to have
to sort the sequence every time another record is added.

Thanks in advance,
   Gabriel Boehme

new topic     » topic index » view message » categorize

2. Re: Inserting to a sequence

Gabriel Boehme writes:

> What is the fastest way of inserting an item into
> a sequence? I know ED.EX uses the following
> two methods:
>1) For inserting a char into a line:

>   line = line[1..b_col-1] & char & line[b_col..length(line)]

> 2) For inserting a line into a file:

>   buffer = append(buffer, 0)
>   for i = length(buffer)-1 to b_line by -1 do
>      buffer[i+1] = buffer[i]
>   end for
>   buffer[b_line] = line

Method 1 is the simplest, and it's what I originally
used for inserting a line. As I recall, I switched to
method 2 a few years ago, after running out of memory
at this exact point, while editing a huge file.
Method 2 needs less memory, i.e. temp space,
so I thought it might help. In fact it doesn't really help
much but I left it that way.

Method 2 might be faster than method 1,
depending on the size of the sequence.
Provided the sequence is reasonably long,
you can make method 2 even faster if you replace
     for
         ...
     end for

with:

buffer[bline+1..length(buffer)] = buffer[bline..length(buffer)-1]

The performance of ed depends mainly on the
screen-writing operations, not the buffer manipulations.

Regards,
     Rob Craig
     Rapid Deployment Software
     http://members.aol.com/FilesEu/

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu