[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: cursor/stream/c++ iterator class?

Eric Mahurin

4/28/2005 5:44:00 PM


--- Simon Strandgaard <neoneye@gmail.com> wrote:
> On 4/28/05, Eric Mahurin <eric_mahurin@yahoo.com> wrote:
> > --- Simon Strandgaard <neoneye@gmail.com> wrote:
> > > I have made a lib containing some external iterators,
> see:
> > > http://aeditor.rubyforge.org...
> > >
> > > download it here:
> > >
> http://rubyforge.org/frs/download.php/704/iterat...
> >
> > Thanks Simon. I just looked through what you have. You
> gave
> > me a few ideas. But, here are a few things I don't like:
> >
> > * for efficiency I would like the ability to read/write
> > multiple elements at a time. I want the API to have a
> superset
> > of the features in IO if possible.
>
> True, I didn't made mine for speed.
>
>
> > * instead of calling has_next? I'm just having next return
> nil
> > or an empty string/array (for getting N elements). If the
> > collection can have nil elements, you'll have to ask for N
> > elements (could be 1) so that you get an empty array at the
> > end.
>
> What if you are iterating through an array that
> coincidentially
> contains a nil.. like this [42, nil, 42] ?

For collections that can contain nil, you'll have to pass the
optional length argument (=1 if you want 1 element). So in the
above, consecutive calls to get(1) will return [42], [nil],
[42], and [] (or nil - haven't decided). Calls to get() will
return 42, nil (you'll think your're at the end), 42, nil. So
get(length) is like read(length) (returns a String) and get()
is like getc() (returns a character/Fixnum).

> > * no standard insert/delete operations. I don't need them
> now,
> > but they might be useful to others.
>
> Yeah, I don't have neither insert nor delete.
> I didn't needed it when I made the lib. I though about it,
> but couldn't decide how to approach this problem.

So that the number of methods doesn't blow up, I decided to
have my "get" and "put" methods take a flags argument to
provide many functions:

forward/reverse
move/hold
read(return original value)/ignore(return length)
scan or replace vs. delete or insert

Last I counted, I have 12 get functions and 10 put functions
(all of which can operator on individual elements or a
string/array of them). The base class will base all of these
on 4 primitives (getdelete before/after and insert
before/after), but most classes will want to override most of
it.

> > * like the C++ iterator, yours standardizes on
> > allowing/creating/comparing multiple iterators on the same
> data
> > structure. I don't want to do this because many sequential
> > data structures can't handle this. The primary example
> would
> > be an IO (especially a non-file) which only allows you to
> > read/write at one location at a time. Another would be a
> text
> > editor buffer where you can only insert/delete at the
> cursor
> > position. Instead of allowing multiple iterators for a
> data
> > structure, you'll be able to get and set the position where
> > "position" is an object that you might be able to compare
> > and/or do arithmetic on (depends on the data structure).
>
> This sounds interesting. I would be interesting at looking
> at how you would approach this.

The best analogy to this is simply the IO tell/pos and
seek/pos= methods. For this type of position (an integer),
you can compare and add/subtract, but other position objects
may not be so flexible. I don't think I'll specify what these
position objects look like. Some classes where the underlying
data-structure is unidirectional (i.e. IO pipes) will use this
to signal when to start/stop buffering so that you can go back
to the original position.

> > I think I'll call the class I'm doing "Cursor" because it
> most
> > resembles what you can do at the cursor in a text editor
> and
> > I've also seen the term "cursor" used for external
> iterator.
> >
> > I might take some of the ideas in your Iterator classes
> also
> > (some from C++ STL iterator it looks like), if you don't
> mind.
>
> Feel free to use all of it as you like.
> Is your project on rubyforge ?

not yet.



__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail...


1 Answer

Simon Strandgaard

4/28/2005 6:07:00 PM

0

On 4/28/05, Eric Mahurin <eric_mahurin@yahoo.com> wrote:
> --- Simon Strandgaard <neoneye@gmail.com> wrote:
[snip]
> > Yeah, I don't have neither insert nor delete.
> > I didn't needed it when I made the lib. I though about it,
> > but couldn't decide how to approach this problem.
>
> So that the number of methods doesn't blow up, I decided to
> have my "get" and "put" methods take a flags argument to
> provide many functions:
>
> forward/reverse
> move/hold
> read(return original value)/ignore(return length)
> scan or replace vs. delete or insert
>
> Last I counted, I have 12 get functions and 10 put functions
> (all of which can operator on individual elements or a
> string/array of them). The base class will base all of these
> on 4 primitives (getdelete before/after and insert
> before/after), but most classes will want to override most of
> it.

Watch out not putting too much behavior in a single function :-)

Can you paste some of your code? I'd like to see it.


--
Simon Strandgaard