Topic: templates for containers, enhancement
Author: holen@netcom.com (Victor A. Holen )
Date: Thu, 14 Apr 1994 06:52:50 GMT Raw View
Motivation:
Time and again project that requires efficient containers, either hand
codes each case or writes a code generating pre-processor.
All C++ books I could find, use only very simplified container models
and/or sacrifice efficiency by generating non-intrusive containers.
While there are many programs where non-intrusive containers are
fine, or even advantageous, there are many application where one can not
afford the extra space nor time they take.
I see no _elegant_ way to use current C++ mechanisms to build, for
example, link lists where parent can own more than one list of
children, and a child can be in different parent lists.
Proposal:
Enhance templates to allow identifiers as parameters. At least some
identifiers as in the example below.
// slist.c example of use
class Pin; class Comp; class Net;
template<class Parent, class Child, Child* first, Child* next > class Slist {
public:
static inline void add(Parent* par, Child* chi) {chi->next= par->first;
par->first= chi; }
//del(), iterate(), whatever()...procedures for parent/child slists
};
class Comp {
Pin* iFirst; // for input pin list
Pin* oFirst; // for output pin list
public:
friend class Slist<Comp, Pin, iFirst, nextByComp>;
friend class Slist<Comp, Pin, oFirst, nextByComp>;
};
class Pin {
Pin* nextByComp;
Pin* nextByNet;
public:
friend class Slist<Comp, Pin, iFirst, nextByComp>;
friend class Slist<Comp, Pin, oFirst, nextByComp>;
friend class Slist<Net, Pin, first, nextByNet>;
};
class Net {
Pin* first; // for list of pins in this net
public:
friend class Slist<Net, Pin, first, nextByNet>;
};
main() {
Slist<Comp, Pin, iFirst, nextByComp> compIPin;
Slist<Comp, Pin, oFirst, nextByComp> compOPin;
Slist<Net, Pin, first, nextByNet > netPin;
Comp* comp;
Net* net;
Pin* pin;
//...
compIPin.add(comp,pin);
netPin.add(net,pin);
}
Notes:
I assume a good compiler can optimize call to netPin.add(net,pin) as
efficient as pin.addNet(net) or addNetPin(net,pin)
This approach requires user to type in a bit more details than a
good code generating pre-processor does, but the obvious advantages
are much greater.
--
victor holen@netcom.com