• Advertisement
  • entries
    707
  • comments
    1173
  • views
    434667

New enumerator

Sign in to follow this  

50 views

I rewrote my enumerator system and dropped my stl wrappers. Instead, I went with derived classes. Obviously it adds the overhead of having creating an instance of the enumerator.

I was going to implement the operator++, but it wouldn't work right because either I'd have to go with returning a bool, which isn't how a prefix or postfix operator works, or I would have to implement some sort of comparison which would screw the whole thing up as well. So, I'm sticking with .MoveNext(), but I have overloaded the * and -> operators.

Obviously most people are still going to think it's a retarded/crappy idea, but I like it and it's not like they have to use it. Plus, it's totally compatible with std::vector and std::map, and looks just like std::vector and std::map, except they're in dfg namespace now.

MaulingMonkey said it was a more sane way of doing it, but I'm sure he still thinks that it's a dumb idea lol.

Anyway, here's new the new file if anybody is interested:

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Dragonfire Games Code Library
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#ifndef _DFGCL_STLENUMERATORS_H_
#define _DFGCL_STLENUMERATORS_H_

// EXAMPLES CAN BE FOUND AT THE BOTTOM OF THE FILE

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Header files
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#include
#include
#include
#include

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
namespace dfg
{
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
[DESCRIPTION]
[MEMBERS]
[REMARKS]
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

template
class Enumerator
{
typename CONTAINERTYPE::iterator Start, End, Current;
public:

/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
[DESCRIPTION]
Constructor for Enumerator.
[PARAMETERS]
Start - The iterator to start enumeration at.
End - The iterator to stop enumeration at.
[REMARKS]
The End iterator will not be returned during enumeration.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

Enumerator(typename CONTAINERTYPE::iterator Start, typename CONTAINERTYPE::iterator End)
{
this->Start = Start;
this->End = End;
Current = End;
}

/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
[DESCRIPTION]
Constructor for Enumerator.
[PARAMETERS]
Container - The container that will be enumerated.
[REMARKS]
Start and End are set to Container.begin() and Container.end().
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

Enumerator(CONTAINERTYPE& Container)
{
this->Start = Container.begin();
this->End = Container.end();
Current = End;
}

/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
[DESCRIPTION]
Accessor operator for Enumerator.
Returns the current iterator.
[PARAMETERS]
[REMARKS]
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

typename CONTAINERTYPE::iterator operator->()
{
return Current;
}

/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
[DESCRIPTION]
Dereferencing operator for Enumerator.
Returns the current node's reference.
[PARAMETERS]
[REMARKS]
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

typename CONTAINERTYPE::reference operator*()
{
return *Current;
}

/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
[DESCRIPTION]
Moves to the next node in the container. Once End is reached it returns false, otherwise
it returns true.
[PARAMETERS]
[REMARKS]
Will return to Start once it reaches End.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

bool MoveNext()
{
if(Current == End)
Current = Start;
else
++Current;

if(Current == End)
{
Current = End;
return false;
}
return true;
}
};

template
class ConstEnumerator
{
typename CONTAINERTYPE::const_iterator Start, End, Current;
public:
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
[DESCRIPTION]
Constructor for ConstEnumerator.
[PARAMETERS]
Start - The iterator to start enumeration at.
End - The iterator to stop enumeration at.
[REMARKS]
The End iterator will not be returned during enumeration.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

ConstEnumerator(typename CONTAINERTYPE::const_iterator Start, typename CONTAINERTYPE::const_iterator End)
{
this->Start = Start;
this->End = End;
Current = End;
}

/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
[DESCRIPTION]
Constructor for ConstEnumerator.
[PARAMETERS]
Container - The container that will be enumerated.
[REMARKS]
Start and End are set to Container.begin() and Container.end().
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

ConstEnumerator(const CONTAINERTYPE& Container)
{
this->Start = Container.begin();
this->End = Container.end();
Current = End;
}

/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
[DESCRIPTION]
Accessor operator for ConstEnumerator.
Returns the current iterator.
[PARAMETERS]
[REMARKS]
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

typename CONTAINERTYPE::const_iterator operator->()
{
return Current;
}

/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
[DESCRIPTION]
Dereferencing operator for ConstEnumerator.
Returns the current node's reference.
[PARAMETERS]
[REMARKS]
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

typename CONTAINERTYPE::const_reference operator*()
{
return *Current;
}

/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
[DESCRIPTION]
Moves to the next node in the container. Once End is reached it returns false, otherwise
it returns true.
[PARAMETERS]
[REMARKS]
Will return to Start once it reaches End.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

bool MoveNext()
{
if(Current == End)
Current = Start;
else
++Current;

if(Current == End)
{
Current = End;
return false;
}
return true;
}
};

template
class ReverseEnumerator
{
typename CONTAINERTYPE::reverse_iterator Start, End, Current;
public:
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
[DESCRIPTION]
Constructor for ReverseEnumerator.
[PARAMETERS]
Start - The iterator to start enumeration at.
End - The iterator to stop enumeration at.
[REMARKS]
The End iterator will not be returned during enumeration.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/


ReverseEnumerator(typename CONTAINERTYPE::reverse_iterator Start, typename CONTAINERTYPE::reverse_iterator End)
{
this->Start = Start;
this->End = End;
Current = End;
}

/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
[DESCRIPTION]
Constructor for ReverseEnumerator.
[PARAMETERS]
Container - The container that will be enumerated.
[REMARKS]
Start and End are set to Container.rbegin() and Container.rend().
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

ReverseEnumerator(const CONTAINERTYPE& Container)
{
this->Start = Container.begin();
this->End = Container.end();
Current = End;
}

/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
[DESCRIPTION]
Accessor operator for ReverseEnumerator.
Returns the current iterator.
[PARAMETERS]
[REMARKS]
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

typename CONTAINERTYPE::reverse_iterator operator->()
{
return Current;
}

/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
[DESCRIPTION]
Dereferencing operator for ReverseEnumerator.
Returns the current node's reference.
[PARAMETERS]
[REMARKS]
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

typename CONTAINERTYPE::reference operator*()
{
return *Current;
}

/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
[DESCRIPTION]
Moves to the next node in the container. Once End is reached it returns false, otherwise
it returns true.
[PARAMETERS]
[REMARKS]
Will return to Start once it reaches End.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

bool MoveNext()
{
if(Current == End)
Current = Start;
else
++Current;

if(Current == End)
{
Current = End;
return false;
}
return true;
}
};

template
class ConstReverseEnumerator
{
typename CONTAINERTYPE::const_reverse_iterator Start, End, Current;
public:
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
[DESCRIPTION]
Constructor for ConstReverseEnumerator.
[PARAMETERS]
Start - The iterator to start enumeration at.
End - The iterator to stop enumeration at.
[REMARKS]
The End iterator will not be returned during enumeration.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

ConstReverseEnumerator(typename CONTAINERTYPE::const_reverse_iterator Start, typename CONTAINERTYPE::const_reverse_iterator End)
{
this->Start = Start;
this->End = End;
Current = End;
}

/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
[DESCRIPTION]
Constructor for ConstReverseEnumerator.
[PARAMETERS]
Container - The container that will be enumerated.
[REMARKS]
Start and End are set to Container.rbegin() and Container.rend().
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

ConstReverseEnumerator(const CONTAINERTYPE& Container)
{
this->Start = Container.rbegin();
this->End = Container.rend();
Current = End;
}

/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
[DESCRIPTION]
Accessor operator for ConstReverseEnumerator.
Returns the current iterator.
[PARAMETERS]
[REMARKS]
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

typename CONTAINERTYPE::const_reverse_iterator operator->()
{
return Current;
}

/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
[DESCRIPTION]
Dereferencing operator for ConstReverseEnumerator.
Returns the current node's reference.
[PARAMETERS]
[REMARKS]
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

typename CONTAINERTYPE::const_reference operator*()
{
return *Current;
}

/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
[DESCRIPTION]
Moves to the next node in the container. Once End is reached it returns false, otherwise
it returns true.
[PARAMETERS]
[REMARKS]
Will return to Start once it reaches End.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

bool MoveNext()
{
if(Current == End)
Current = Start;
else
++Current;

if(Current == End)
{
Current = End;
return false;
}
return true;
}
};

template >
class vector : public std::vector
{
typedef std::vector CONTAINERTYPE;
public:
vector() : std::vector()
{
}

vector(const std::vector& Vector) : std::vector(Vector)
{
}

typedef Enumerator enumerator;
typedef ConstEnumerator const_enumerator;
typedef ReverseEnumerator reverse_enumerator;
typedef ConstReverseEnumerator const_reverse_enumerator;

enumerator get_enum() { return enumerator(*this); }
const_enumerator get_enum() const { return const_enumerator(*this); }
reverse_enumerator get_renum() { return reverse_enumerator(*this); }
const_reverse_enumerator get_renum() const { return const_reverse_enumerator(*this); }
};

template , typename ALLOCATOR = std::allocatorconst KEYTYPE, TYPE> > >
class map : public std::map
{
typedef std::map CONTAINERTYPE;
public:
map() : std::map()
{
}

map(const std::map& Map) : std::map(Map)
{
}

typedef Enumerator enumerator;
typedef ConstEnumerator const_enumerator;
typedef ReverseEnumerator reverse_enumerator;
typedef ConstReverseEnumerator const_reverse_enumerator;

enumerator get_enum(CONTAINERTYPE::iterator Start = this->begin(), CONTAINERTYPE::iterator End = this->end()) { return enumerator(Start, End); }
const_enumerator get_enum(CONTAINERTYPE::iterator Start = this->begin(), CONTAINERTYPE::iterator End = this->end()) const { return const_enumerator(*this); }
reverse_enumerator get_renum(CONTAINERTYPE::iterator Start = this->rbegin(), CONTAINERTYPE::iterator End = this->rend()) { return reverse_enumerator(*this); }
const_reverse_enumerator get_renum(CONTAINERTYPE::iterator Start = this->rbegin(), CONTAINERTYPE::iterator End = this->rend()) const { return const_reverse_enumerator(*this); }
};
}

/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Examples:
typedef dfg::vector StringVector;

StringVector MyVector;

// fill MyVector with strings here.

// StringVector::enumerator can be enumerator or const_enumerator.
StringVector::enumerator Enum = MyVector.get_enum();
while(Enum.MoveNext())
std::cout<c_str()<
// Reverse enumeration
typedef dfg::vector StringVector;

StringVector MyVector;

// fill MyVector with strings here.

// StringVector::enumerator can be reverse_enumerator or const_reverse_enumerator.
// Note that the reverse enumerator functions are prefixed with an r (get_renum.)
StringVector::reverse_enumerator Enum = MyVector.get_renum();
while(Enum.MoveNext())
std::cout<c_str()<
// Map, regular enumeration
typedef dfg::map Dictionary;

Dictionary MyDictionary;

// fill MyDictionary with stuff here.

// Dictionary::enumerator can be enumerator or const_enumerator.
Dictionary::enumerator Enum = MyDictionary.get_enum();
while(Enum.MoveNext())
std::cout<first.c_str()<<") "<second.c_str()<
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Example - Using Enumerator & Co.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
typedef std::vector StringVector;

StringVector MyVector;

// fill MyVector with strings here.

// dfg::Enumerator can be Enumerator, ConstEnumerator, ReverseEnumerator, or ConstReverseEnumerator.
dfg::Enumerator Enum(MyVector);
while(Enum.MoveNext())
std::cout<c_str()<
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
typedef std::map Dictionary;

Dictionary MyDictionary;

// fill in MyDictionary with stuff here.

// dfg::Enumerator can be Enumerator, ConstEnumerator, ReverseEnumerator, or ConstReverseEnumerator.
dfg::Enumerator Enum(MyDictionary);
while(Enum.MoveNext())
std::cout<first.c_str()<<") "<second.c_str()<
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/








Malathedra


Things are moving along somewhat. I'm working on prettying up some of the maps a little, making sure they have good designs and whatnot, while Raymond is finishing up MW's patch.

Edit: Yes, I realize that the enumerator is pretty pointless at this stage, but I'm just experimenting.
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement