home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
cset21v1.zip
/
IBMCPP
/
IBMCLASS
/
IBSTKSS.H
< prev
next >
Wrap
Text File
|
1993-09-22
|
15KB
|
410 lines
/*******************************************************************************
* *
* COPYRIGHT: *
* IBM C/C++ Tools Version 2.01 - Collection Class Library *
* Licensed Materials - Property of IBM *
* (C) Copyright IBM Corporation 1992, 1993 *
* All Rights Reserved *
* US Government Users Restricted Rights - Use, duplication, or disclosure *
* restricted by GSA ADP Schedule Contract with IBM Corp. *
* *
*******************************************************************************/
#ifndef _IBSTKSS_H
#define _IBSTKSS_H
#include <iglobals.h>
#include <icursor.h>
#include <iibstkss.h>
#include <stdlib.h>
template < class Element, class Key, class ElementOps, size_t nodeSize >
class IWBSTKeySortedSet
{
#define IBSTMAX(a,b) ((a) < (b) ? (b) : (a))
#define IBSTORDER \
IBSTMAX (2, (nodeSize \
- (sizeof(IBSTKeySortedSetImpl::Inode) \
+ sizeof(IBSTKeySortedSetImpl::Node*)) \
) / (sizeof(Key) + sizeof(IBSTKeySortedSetImpl::Node*)) + 1)
static ElementOps elementOps ()
{ ElementOps ops;
return ops;
}
class Lnode;
class Inode;
class KeyWrapper;
class Operations;
class Cursor;
friend class Lnode;
friend class Inode;
friend class KeyWrapper;
friend class Operations;
friend class Cursor;
class Lnode : public IBSTKeySortedSetImpl::Lnode
{
friend class IWBSTKeySortedSet < Element, Key, ElementOps, nodeSize >;
friend class Operations;
private :
Element ivItem;
public:
Lnode (Element const& item) : ivItem (item)
{}
Lnode (Lnode const& lnode) : ivItem (lnode.ivItem)
{}
#if defined (__IBMCPP__) && defined (__DEBUG_ALLOC__)
void* operator new (size_t s, char const* fn, size_t ln)
{ return elementOps().allocate (s, fn, ln); }
void operator delete (void* p, char const* fn, size_t ln)
{ elementOps().deallocate (p, fn, ln); }
#else
void* operator new (size_t s)
{ return elementOps().allocate (s); }
void operator delete (void* p)
{ elementOps().deallocate (p); }
#endif // __IBMCPP__ && __DEBUG_ALLOC__
virtual void const*
getElement () const;
virtual void* getElement ();
virtual void replaceElement (void const* element);
virtual IBoolean isKeyEqualTo (void const* lnode) const;
virtual IBoolean isKeyEqualToKeyOfElement (void const* element) const;
virtual IBoolean isKeyEqualToKey (void const* key) const;
};
class Inode : public IBSTKeySortedSetImpl::Inode
{
friend class Operations;
IBSTKeySortedSetImpl::Node* ivNodeptrs [IBSTORDER];
Key ivKeys [IBSTORDER - 1];
public:
Inode () : IBSTKeySortedSetImpl::Inode (ivNodeptrs, ivKeys)
{}
Inode (Inode const& inode)
: IBSTKeySortedSetImpl::Inode (inode, ivNodeptrs, ivKeys)
{}
#if defined (__IBMCPP__) && defined (__DEBUG_ALLOC__)
void* operator new (size_t s, char const* fn, size_t ln)
{ return elementOps().allocate (s, fn, ln); }
void operator delete (void* p, char const* fn, size_t ln)
{ elementOps().deallocate (p, fn, ln); }
#else
void* operator new (size_t s)
{ return elementOps().allocate (s); }
void operator delete (void* p)
{ elementOps().deallocate (p); }
#endif // __IBMCPP__ && __DEBUG_ALLOC__
};
class KeyWrapper
{ Key key;
public:
#if defined (__IBMCPP__) && defined (__DEBUG_ALLOC__)
void* operator new (size_t s, char const* fn, size_t ln)
{ return elementOps().allocate (s, fn, ln); }
void operator delete (void* p, char const* fn, size_t ln)
{ elementOps().deallocate (p, fn, ln); }
#else
void* operator new (size_t s)
{ return elementOps().allocate (s); }
void operator delete (void* p)
{ elementOps().deallocate (p); }
#endif // __IBMCPP__ && __DEBUG_ALLOC__
};
class Operations : public IBSTKeySortedSetImpl::Operations
{
public:
virtual IBSTKeySortedSetImpl::Lnode*
newLnode (void const* element) const;
virtual IBSTKeySortedSetImpl::Lnode*
newLnodeFromLnode (void const* lnode) const;
virtual IBSTKeySortedSetImpl::Inode*
newInode () const;
virtual IBSTKeySortedSetImpl::Inode*
newInode (void const* inode) const;
virtual void* newKey () const;
virtual void deleteLnode (void* lnode) const;
virtual void deleteInode (void* inode) const;
virtual void deleteKey (void *key) const;
virtual IBoolean isKeyLessThan (void const* key1,
void const* key2) const;
virtual void copyKey (void* toKey,
unsigned int toIndex,
void const* fromKey,
unsigned int fromIndex) const;
virtual IBoolean isKeyLessThanLnode (void const* key,
void const* lnode) const;
virtual void copyKeyFromLnode (void* key,
void const* lnode);
virtual IBoolean isKeyGreaterThanLnode(void const* key,
unsigned int index,
void const* lnode) const;
virtual IBoolean isKeyGreaterThanKey (void const* key1,
unsigned int index,
void const* key2) const;
virtual IBoolean isKeyGreaterThanElement
(void const* key,
unsigned int index,
void const* element) const;
virtual IBoolean constantFunctionIteration
(void *function,
void* env,
void const* lnode) const;
virtual IBoolean functionIteration (void *function,
void* env,
void* lnode) const;
virtual IBoolean constantIteratorIteration
(void* iterator,
void const* lnode) const;
virtual IBoolean iteratorIteration (void* iterator,
void* lnode) const;
virtual long functionComparison (void *compareFunction,
void const* lnode1,
void const* lnode2) const;
};
private:
Operations ivOps;
IBSTKeySortedSetImpl ivImpl;
void init ();
void checkNotEmpty () const;
void checkCursorIsForThis (ICursor const&) const;
void checkCursor (ICursor const&) const;
void checkPositionExists (IPosition) const;
public:
class Cursor : public ICursor {
protected:
IWBSTKeySortedSet < Element, Key, ElementOps, nodeSize > const* ivCollection;
IBSTKeySortedSetImpl::Lnode *ivNode;
friend class IWBSTKeySortedSet < Element, Key, ElementOps, nodeSize >;
IBoolean isFor (IWBSTKeySortedSet
< Element, Key, ElementOps, nodeSize > const& c) const
{ return ivCollection == &c; }
public:
Cursor (IWBSTKeySortedSet < Element, Key, ElementOps, nodeSize > const& c)
: ivCollection (&c), ivNode (0) {};
IBoolean setToFirst ();
IBoolean setToNext ();
IBoolean isValid () const;
void invalidate ();
Element const& element () const
{ return ivCollection->elementAt (*this); }
IBoolean setToLast ()
{ return ivCollection->setToLast (*this); }
IBoolean setToPrevious ()
{ return ivCollection->setToPrevious (*this); }
IBoolean operator== (Cursor const& cursor) const
{ return ivCollection == cursor.ivCollection &&
ivNode == cursor.ivNode; }
IBoolean operator!= (Cursor const& cursor) const
{ return ! operator== (cursor); }
};
IWBSTKeySortedSet (INumber
numberOfElements = 100);
IWBSTKeySortedSet (IWBSTKeySortedSet < Element, Key, ElementOps, nodeSize > const&);
IWBSTKeySortedSet < Element, Key, ElementOps, nodeSize >&
operator = (IWBSTKeySortedSet < Element, Key, ElementOps, nodeSize > const&);
~IWBSTKeySortedSet ();
IBoolean add (Element const&);
IBoolean add (Element const&,
ICursor&);
void addAllFrom (IWBSTKeySortedSet < Element, Key, ElementOps, nodeSize > const&);
Element const& elementAt (ICursor const&) const;
Element& elementAt (ICursor const&);
Element const& anyElement () const;
void removeAt (ICursor const&);
INumber removeAll (IBoolean (*property)
(Element const&, void*),
void* additionalArgument = 0);
void replaceAt (ICursor const&,
Element const&);
void removeAll ();
IBoolean isBounded () const;
INumber maxNumberOfElements () const;
INumber numberOfElements () const;
IBoolean isEmpty () const;
IBoolean isFull () const;
ICursor* newCursor () const;
IBoolean setToFirst (ICursor&) const;
IBoolean setToNext (ICursor&) const;
IBoolean allElementsDo (IBoolean (*function)
(Element&, void*),
void* additionalArgument = 0);
IBoolean allElementsDo (IIterator <Element>&);
IBoolean allElementsDo (IBoolean (*function)
(Element const&, void*),
void* additionalArgument = 0)
const;
IBoolean allElementsDo (IConstantIterator
<Element>&) const;
IBoolean isConsistent () const;
Key const& key (Element const&) const;
IBoolean containsElementWithKey (Key const&) const;
IBoolean containsAllKeysFrom (IWBSTKeySortedSet < Element, Key, ElementOps, nodeSize > const&) const;
IBoolean locateElementWithKey (Key const&, ICursor&)
const;
IBoolean replaceElementWithKey (Element const&);
IBoolean replaceElementWithKey (Element const&,
ICursor&);
IBoolean locateOrAddElementWithKey (Element const&);
IBoolean locateOrAddElementWithKey (Element const&,
ICursor&);
IBoolean addOrReplaceElementWithKey (Element const&);
IBoolean addOrReplaceElementWithKey (Element const&,
ICursor&);
IBoolean removeElementWithKey (Key const&);
Element const& elementWithKey (Key const&) const;
Element& elementWithKey (Key const&);
void removeFirst ();
void removeLast ();
void removeAtPosition (IPosition);
Element const& firstElement () const;
Element const& lastElement () const;
Element const& elementAtPosition (IPosition) const;
IBoolean setToLast (ICursor&) const;
IBoolean setToPrevious (ICursor&) const;
void setToPosition (IPosition,
ICursor&) const;
IBoolean isFirst (ICursor const&) const;
IBoolean isLast (ICursor const&) const;
long compare (IWBSTKeySortedSet < Element, Key, ElementOps, nodeSize > const&,
long (*comparisonFunction)
(Element const&,
Element const&)) const;
};
#include <ibstkss.if>
#ifndef IBSTPAGESIZE
#define IBSTPAGESIZE 4096
#endif
template < class Element, class Key, class ElementOps >
class IGBSTKeySortedSet :
public IWBSTKeySortedSet < Element, Key, ElementOps, IBSTPAGESIZE > {
public:
IGBSTKeySortedSet (INumber n = 100) :
IWBSTKeySortedSet < Element, Key, ElementOps, IBSTPAGESIZE > (n) {}
};
template < class Element, class Key >
class IBSTKeySortedSet :
public IGBSTKeySortedSet < Element, Key, IKCOps < Element, Key > > {
public:
IBSTKeySortedSet (INumber n = 100) :
IGBSTKeySortedSet < Element, Key, IKCOps < Element, Key > > (n) {}
};
#ifdef __IBMCPP__
#ifndef __TEMPINC__
#include <ibstkss.c>
#endif
#endif
#endif