📄 treeassociation.h
字号:
/* $Id: TreeAssociation.h,v 1.2 1997/02/02 01:31:05 matt Exp $ Association as a binary tree. ** This class is obsolete since the binary tree class began using (key, value) pairs. (c) May 95 Matt Phillips. */#ifndef _BTASSOC_H#define _BTASSOC_H#include "Association.h"#include "AssociationItem.h"#include "OrderedBinaryTree.h"// NOTE: this class uses technically illegal upcasting from class AK// to A class in order to generate temporary "A" objects that do not// destruct as they go out of scope. this technique is used by the// add, get and remove members where only the key will be used, since// the data member will be unitialised.template <class K, class T, class A, class AK>class TreeAssociationImp : public Association<K, T>{ // K = key type, T = type to be associated with key // A = assoc record type, AK = key subclass of Apublic: typedef TreeAssociationImpIter<K, T, A, AK> Iterator; int nItems () const {return tree.nItems ();} int isFull () const {return tree.isFull ();} int isEmpty () const {return tree.isEmpty ();} void clear () {tree.clear ();} virtual T &add (K &key, T &i) { A &a = tree.add ((A &)AK (key)); // naughty upcast! a.set (i); return a.ref (); } virtual int remove (const K &key) {return tree.remove ((A &)AK (key));} // naughty upcast! virtual T *get (const K &key) const { A *a = tree.get ((A &)AK (key)); // naughty upcast! return a ? &(a->ref ()) : 0; } Iterator *makeIter () const {return new Iterator (*this);}protected: OrderedBinaryTreeImp<A, DBinaryTreeItem<A> > tree; friend Iterator;};template <class K, class T, class A, class AK>class TreeAssociationImpIter : public AssociationIter<K, T>{public: TreeAssociationImpIter (const TreeAssociationImp<K, T, A, AK> &a) : iter (a.tree) {} virtual void reset () {iter.reset ();} virtual operator int () const {return int (iter);} virtual void operator ++ (int) {iter++;} virtual T value () const {return iter.value ().ref ();} // this was iter.value ().ref (), and caused an incorrect // destructor call in the hash table implementation of associations. virtual T &ref () const {return iter.ref ().ref ();} virtual const K &getKey () const {return iter.ref ().refKey ();}protected: OrderedBinaryTreeImpIter<A, DBinaryTreeItem<A> > iter;};#define TypeDTreeAssociation(K, T) \TreeAssociationImp<K, T, DAssocItem<K, T>, DAssocKey<K> >#define TypeITreeAssociation(K, T, ownsK, ownsT) \TreeAssociationImp<K, T, IAssocItem<K, T, ownsK, ownsT>, IAssocKey<K> >#define TypeIKTreeAssociation(K, T, ownsK) \TreeAssociationImp<K, T, IKAssocItem<K, T, ownsK>, IAssocKey<K> >#define TypeITTreeAssociation(K, T, ownsT) \TreeAssociationImp<K, T, ITAssocItem<K, T, ownsT>, DAssocKey<K> >#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -