home *** CD-ROM | disk | FTP | other *** search
- --
- -- Define the class for both key and element type
- --
- <code DEFINE_COLLECTION_CLASS>
- <define _MEMBER_TYPE_>IString\</define>
-
- class $ELEMENT_CLASS$
- {
- //-----------------------------------------------------------------------------
- // Required members
- //-----------------------------------------------------------------------------
- public:
- <if ($DEFAULT_CONSTRUCTOR$)>
- //------------------------------------------------------------------------
- // Default constructor
- //------------------------------------------------------------------------
- $ELEMENT_CLASS$()
- <if ($KEY_PTR$)>
- :iSelf(this)
- </if>
- {
- }
- </if>
- <if ($COPY_CONSTRUCTOR$)>
- //------------------------------------------------------------------------
- // Copy constructor
- //------------------------------------------------------------------------
- $ELEMENT_CLASS$($ELEMENT_CLASS$ const& a$ELEMENT_CLASS$)
- : iName(a$ELEMENT_CLASS$.iName)
- <if ($KEY_PTR$)>
- , iSelf(this)
- </if>
- {
- }
- </if>
- <if ($ASSIGNMENT$)>
- //------------------------------------------------------------------------
- // Assignment operator
- //------------------------------------------------------------------------
- $ELEMENT_CLASS$& operator = ($ELEMENT_CLASS$ const& a$ELEMENT_CLASS$)
- {
- iName = a$ELEMENT_CLASS$.iName;
- <if ($KEY_PTR$)>
- iSelf = this;
- </if>
- return *this;
- }
- </if>
- <if (($K_EQUALITY$) | ($EQUALITY$)) >
- //------------------------------------------------------------------------
- // Equality test
- //------------------------------------------------------------------------
- IBoolean operator== ($ELEMENT_CLASS$ const& a$ELEMENT_CLASS$) const
- {
- return (iName == a$ELEMENT_CLASS$.iName);
- }
- </if>
- <if ( ($ORDERING$) | ($K_ORDERING$) )>
- //------------------------------------------------------------------------
- // Ordering relation between $ELEMENT_CLASS$ objects.
- //------------------------------------------------------------------------
- IBoolean operator< ($ELEMENT_CLASS$ const& a$ELEMENT_CLASS$) const
- {
- return (iName < a$ELEMENT_CLASS$.iName);
- }
- </if>
- //-----------------------------------------------------------------------------
- // Additional members (customization)
- //-----------------------------------------------------------------------------
- //------------------------------------------------------------------------
- // Constructor to initialize the private memebers
- //------------------------------------------------------------------------
- $ELEMENT_CLASS$($_MEMBER_TYPE_$ name) :
- iName(name)
- <if ($KEY_PTR$)>
- ,iSelf(this)
- </if>
- {
- }
-
- //------------------------------------------------------------------------
- // Overload the output operator to print out the contents of the
- // collection. The following is an example of printing out the
- // name member function of the $ELEMENT_CLASS$.
- //------------------------------------------------------------------------
- friend
- ostream& operator<< (ostream& os, $ELEMENT_CLASS$ const& a$ELEMENT_CLASS$)
- {
- return os << a$ELEMENT_CLASS$.iName;
- }
-
- //------------------------------------------------------------------------
- // Define access function to the private members of the class
- //------------------------------------------------------------------------
- $_MEMBER_TYPE_$ const& name() const
- {
- return iName;
- }
- <if ($KEY_ACCESS_COLLECTION$)>
-
- //------------------------------------------------------------------------
- // Obtain the key from a given element.
- //------------------------------------------------------------------------
- $KEY_TYPE$ const& getKey() const
- {
- \<if ($KEY_PTR$)>
- // We cannot return "this" because it'll break the c++ rules, so
- // iSelf is used instead.
- return iSelf;
- \<else>
- return *this;
- \</if>
- }
- </if>
-
- private:
- //------------------------------------------------------------------------
- // Private members of the element type.
- //------------------------------------------------------------------------
- $_MEMBER_TYPE_$ iName;
- <if ($KEY_PTR$)>
- $KEY_TYPE$ iSelf;
- </if>
-
- }; // End of class $ELEMENT_CLASS$
- <if ( (($HASH_FUNCTION$) & ($K_HASH_FUNCTION$)) & \
- ( (($KEY_PTR$) & (!$ELEMENT_PTR$)) | ((!$KEY_PTR$) & ($ELEMENT_PTR$)) ) )>
-
- //-----------------------------------------------------------------------------
- // Need a two hash functions
- //-----------------------------------------------------------------------------
- inline unsigned long hash($ELEMENT_CLASS$ const& a$ELEMENT_CLASS$, unsigned long hashInput)
- {
- return hash((const char*)a$ELEMENT_CLASS$.name(), hashInput);
- }
-
- inline unsigned long hash($ELEMENT_CLASS$* const& a$ELEMENT_CLASS$, unsigned long hashInput)
- {
- return hash((const char*)a$ELEMENT_CLASS$->name(), hashInput);
- }
- <else>\<if ($HASH_FUNCTION$)>
-
- //-----------------------------------------------------------------------------
- // Need a hash function
- //-----------------------------------------------------------------------------
- inline unsigned long hash($ELEMENT_TYPE$ const& a$ELEMENT_CLASS$, unsigned long hashInput)
- {
- <if ($ELEMENT_PTR$)>
- return hash((const char*)a$ELEMENT_CLASS$->name(), hashInput);
- <else>
- return hash((const char*)a$ELEMENT_CLASS$.name(), hashInput);
- </if>
- }
- <else>\<if ($K_HASH_FUNCTION$)>
-
- //-----------------------------------------------------------------------------
- // Need a hash function
- //-----------------------------------------------------------------------------
- inline unsigned long hash($KEY_TYPE$ const& a$KEY_CLASS$, unsigned long hashInput)
- {
- <if ($KEY_PTR$)>
- return hash((const char*)a$KEY_CLASS$->name(), hashInput);
- <else>
- return hash((const char*)a$KEY_CLASS$.name(), hashInput);
- </if>
- }
- </if>\</if>\</if>
-
-
- <if ($KEY_ACCESS_COLLECTION$)>
-
- //-----------------------------------------------------------------------------
- // Define the Key access function. This function must be defined outside
- // the element class. It has one argument, whose type is the element type.
- // The return value of this function must be a const reference.
- //-----------------------------------------------------------------------------
- inline $KEY_TYPE$ const& key($ELEMENT_TYPE$ const& t)
- {
- // This function must call a member function that returns the key. In
- // this case the function is named get_key().
- // You can change always change it to call a different function.
- <if ($ELEMENT_PTR$)>
- return t->getKey();
- <else>
- return t.getKey();
- </if>
- }
- </if>
- <if (($ELEMENT_PTR$) & ($ORDERING$)) >
-
- //-----------------------------------------------------------------------------
- // Define a comparison function
- //-----------------------------------------------------------------------------
- inline long compare($ELEMENT_TYPE$ const& e1, $ELEMENT_TYPE$ const& e2)
- {
- if (*e1 < *e2) return -1;
- else if (*e2 < *e1) return 1;
- else return 0;
- }
- </if>
- <if (($ELEMENT_PTR$) & ($EQUALITY$)) >
-
- //-----------------------------------------------------------------------------
- // Need an equality function
- //-----------------------------------------------------------------------------
- inline IBoolean equal($ELEMENT_TYPE$ const& e1, $ELEMENT_TYPE$ const& e2)
- {
- return (*e1 == *e2);
- }
- </if>
-
- //-----------------------------------------------------------------------------
- // Collection Class declaration
- //-----------------------------------------------------------------------------
- typedef $CLTN_TYPE$<$ELEMENT_TYPE$, $KEY_TYPE$> $CLTN_NAME$;
- </code DEFINE_COLLECTION_CLASS>