[Date Prev][Date Next][Thread Prev][Thread Next][Author Index][Date Index][Thread Index]

Stepper protocol

I did a similar iterator thingie for HyperChem, although since it was
in C it used macros and a couple of functions with gross switch
statements. A typical loop would be:

	LOOP atoms; // structure containing the state of the loop
	ID id; // iteration variable
	Atoms(&atoms, molecule); // set up the loop
	while( id = Next(&atoms) ) {
		... do something ...

Note that having Next() return NULL at the end of the loop makes your
loops look quite neat. You could start at a particular value by
calling SetNext(LOOP *, ID). (The default behaviour was to step
through with Next() until it was about to return the right value.)
For loops that iterated through tuples of atoms (e.g. bonds, angles,
torsions) you could call a function like GetAngle(LOOP *, ID *, ID *,
ID *) to get all the information. The return value from Next() would
be the most useful one of the several atoms in the tuple. (There was
some *very* hairy code to step through torsions and angles properly in
a "stateless" way, without returning the same torsion twice!) This
wouldn't really apply to C++, as we could just return the entire
tuple. Applying this to steppers would get you something like:

	Stepper * atoms = atomsOf(molecule)->setNext(idBegin);
	while( ID * id = atoms->next() ) {
		.. do something ..

I must say it looks better in C++...