Hey Helio.
Good discussion here. I like it that you've taken an
interest to this debate and you seem to be a pretty
good fellow C++'er.
> Hence my idea of providing factory functions for
> each
> tag, mapping the tag names to pointers for the
> factoy
> functions, and then retrieving the pointers from a
> map
> as the tags are read from the XML document. As I
> exposed, it is not as simple as reflection, but it
> can
> be done, and is more efficient than conditional
> chains.
Agreed when considering the overall worse case senario
using Big-O. O(log n) with a balanced tree (std::map)
versus the worst case run time of a conditional
statement O(n)
Now what about the best case senario? With a
std::map, you still get Omega(log n) but with a
conditional statement you end up with Omega(1).
Practically we do not always run into the worst case
senario. In my mock example:
> > Tag &tagClass createClass(String someClassName)
> > {
> > if(someClassName == "InnerTemplate")
> > return new InnerTemplate;
> > else if(someClassName == "xxx")
> > return new XXX
> > }
Let us consider the best case senario in which
InnerTemplate is given as "someClassName". In that
case, the conditional will be quicker than finding it
in a map since it is at the top of the conditional
statements.
Now let us consider if the very last conditional
statement is encountered. The map would have been
quicker.
Certain tags are going to occur higher in frequency on
average than others so I group those at the top. The
overall runtime is actually a bit quicker than if I
were to search a map since I know ahead of time which
tags occur at a higher frequency.
I know that these tags are going to occur at a higher
frequency for *every* set of AIML tags because these
are the required tags such as pattern and template and
so on...
Other tags such as <AIML> are going to be at the
bottom. I only create one AIML class mapped to the
AIML tag name since that tag only exists once for each
aiml file.
So, I might take a single hit on the AIML tag, costing
me O(n) compared to the O(log(n)) but I quickly make
up for it when I start encountering pattern and
template tags which are closer to the top.
> // Ugly hack to allow load-time execution of code.
> class CategoryRegister
> {
> public:
> CategoryRegister()
> {
> factories["Category"] = newCategory;
> }
> }
So you still have to write code to say, new Category
here? With each new tag you add you still have to
hand code the "new TagName"?
That and you even say Ugly hack in your comments.
That's my troubles. It's ugly if you go down that
route. Plus if I keep my tag memory allocation
grouped together in the simple factory method I can
always add new factory methods that perhaps do not use
conditional statements but other algorithms such as a
HashMap which would probably be quicker depending on
the type of AIML data set you are loading and its
size:
/**
* Determines which algorithm to use to load
* the AIML tags. By default it uses
* conditional statements but you can change
* it to use a Balanced tree with
* tagLoadAlgorithm(BalancedTree)
*
* or HashMap
* tagLoadAlgorithm(HashMap)
*
* or SkipList
*
* tagLoadAlgorithm(SkipList)
*
* or etc...
*/
void tagLoadAlgorithm(Enum algorithm);
--- Helio Perroni Filho <
xperroni@...> wrote:
>
> --- mehri <
foreverlinux@...> escreveu:
>
> > Problem is that from the xml I get the AIML tag
> name
> > as a String. From the string I have to
> instantiate
> > the class with the same name.
>
> Hence my idea of providing factory functions for
> each
> tag, mapping the tag names to pointers for the
> factoy
> functions, and then retrieving the pointers from a
> map
> as the tags are read from the XML document. As I
> exposed, it is not as simple as reflection, but it
> can
> be done, and is more efficient than conditional
> chains.
>
> > I know of a few ways but none are elegant and all
> > are more complex then simply registering the
> classes
> in
> > a factory method:
> >
> > Tag &tagClass createClass(String someClassName)
> > {
> > if(someClassName == "InnerTemplate")
> > return new InnerTemplate;
> > else if(someClassName == "xxx")
> > return new XXX
> > }
>
> The problem with this approach is that you have to
> modify the factory method each time you add a new
> tag.
> With a global factory map, however, you could demand
> each new tag to register itself at loading time:
>
> // aiml.h
>
> // Global field.
> map<String, FactoryFunction> factories;
>
> // ...
>
> // category.h
>
> #include <aiml>
>
> class Category
> {
> // ...
> }
>
> Tag& newCategory()
> {
> return new Category();
> }
>
> // Ugly hack to allow load-time execution of code.
> class CategoryRegister
> {
> public:
> CategoryRegister()
> {
> factories["Category"] = newCategory;
> }
> }
>
> // Makes the class constructor be called at loading
> time.
> CategoryRegister& categoryRegister;
>
> --
> Ja mata ne.
> Helio Perroni Filho
>
>
>
>
>
>
>
>
>
>
_______________________________________________________
>
> Yahoo! doce lar. Faça do Yahoo! sua homepage.
>
http://br.yahoo.com/homepageset.html
>
> _______________________________________________
> alicebot-developer mailing list
>
alicebot-developer@...
>
http://list.alicebot.org/mailman/listinfo/alicebot-developer>
.................................................o' \,=./ `o
Mehri (o o)
---=--=---=--=--=---=--=--=--=--=---=--=--=-----ooO--(_)--Ooo---
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
_______________________________________________
alicebot-developer mailing list
alicebot-developer@...
http://list.alicebot.org/mailman/listinfo/alicebot-developer