#ifndef __RWBTREE_H__
#define __RWBTREE_H__

/*
 * RWBTree -- in memory B-Tree.
 *
 * $Id: btree.h,v 7.9 1996/06/12 18:03:48 tuck Exp $
 *
 * (c) Copyright 1989 - 1996 Rogue Wave Software, Inc.
 * ALL RIGHTS RESERVED
 *
 * The software and information contained herein are proprietary to, and
 * comprise valuable trade secrets of, Rogue Wave Software, Inc., which
 * intends to preserve as trade secrets such software and information.
 * This software is furnished pursuant to a written license agreement and
 * may be used, copied, transmitted, and stored only in accordance with
 * the terms of such license and with the inclusion of the above copyright
 * notice.  This software and information or any other copies thereof may
 * not be provided or otherwise made available to any other person.
 *
 * Notwithstanding any other lease or license that may pertain to, or
 * accompany the delivery of, this computer software and information, the
 * rights of the Government regarding its use, reproduction and disclosure
 * are as set forth in Section 52.227-19 of the FARS Computer
 * Software-Restricted Rights clause.
 * 
 * Use, duplication, or disclosure by the Government is subject to
 * restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
 * Technical Data and Computer Software clause at DFARS 252.227-7013.
 * Contractor/Manufacturer is Rogue Wave Software, Inc.,
 * P.O. Box 2328, Corvallis, Oregon 97339.
 *
 * This computer software and information is distributed with "restricted
 * rights."  Use, duplication or disclosure is subject to restrictions as
 * set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
 * Computer Software-Restricted Rights (April 1985)."  If the Clause at
 * 18-52.227-74 "Rights in Data General" is specified in the contract,
 * then the "Alternate III" clause applies.
 *
 ***************************************************************************
 *
 * $Log: btree.h,v $
 * Revision 7.9  1996/06/12 18:03:48  tuck
 * Fixed problem where there was a missing "defined" after #
 *
 * Revision 7.8  1996/05/08 18:35:54  kyle
 * Added export pragmas for Mac code fragments
 * Added operator<= that will not be overridden by ::operator<=()
 * and guarded it as RW_GLOBAL_SCP_BUG2
 * Added details to global scope issue and combined it with comment
 * about Borland
 *
 * Revision 7.7  1996/03/15 17:35:57  kyle
 * Added comment concering RW_GLOBAL_BEFORE_CLASS_SCOPE which affects
 * Mac Symantec too.
 *
 * Revision 7.6  1996/03/07 19:54:15  kevinj
 * Bug #2684: Added RW_GLOBAL_BEFORE_CLASS_SCOPE to make inline
 * specialized template operator<= for times when <utility>'s
 * templatized operator<= is used instead of
 * the class member operator<=.
 *
 * Revision 7.5  1996/02/18 01:35:05  griswolf
 * Replace tabs with spaces, per Rogue Wave standard.
 *
 * Revision 7.4  1996/01/29 20:28:12  pearson
 * Specialize global template function operator<= for Borland 5.0
 *
 * Revision 7.3  1995/09/05 18:09:39  jims
 * Use new copyright macro
 *
 * Revision 7.2  1995/09/01  00:34:34  griswolf
 * Scopus #1057: Make RWBTree re-entrant. Also, cache entries().
 *
 * Revision 7.1  1994/10/16  00:36:07  josh
 * Merged 6.1 and 7.0 development trees
 *
 * Revision 6.2  1994/07/12  18:45:56  vriezen
 * Update Copyright notice
 *
 * Revision 6.1  1994/04/15  19:09:30  vriezen
 * Move all files to 6.1
 *
 * Revision 2.6  1993/09/10  02:56:53  keffer
 * Switched RCS idents to avoid spurious diffs
 *
 * Revision 2.5  1993/04/09  19:35:45  keffer
 * Indexing is now done using size_t
 *
 * Revision 2.4  1993/03/20  22:02:16  keffer
 * Eliminated int to unsigned type conversions.
 *
 * Revision 2.3  1993/03/13  02:14:22  keffer
 * keyP removed in favor of RWCollectableP
 *
 * Revision 2.2  1993/01/28  01:56:00  myersn
 * derive from RWMemoryPool via the macro RWMemoryPool_OPTION for MT-safety
 *
 * Revision 2.0  1992/10/23  03:27:32  keffer
 * RCS Baseline version
 *
 * 
 *    Rev 1.5   04 Aug 1992 18:55:48   KEFFER
 * Added "virtual" keyword to destructor for documentation purposes.
 * 
 *    Rev 1.4   22 May 1992 17:04:08   KEFFER
 * Now uses RWDECLARE_COLLECTABLE() macro
 * 
 *    Rev 1.3   04 Mar 1992 09:07:24   KEFFER
 * Changed nil to rwnil
 * 
 *    Rev 1.2   18 Feb 1992 09:54:10   KEFFER
 * 
 *    Rev 1.1   28 Oct 1991 09:08:08   keffer
 * Changed inclusions to <rw/xxx.h>
 * 
 *    Rev 1.0   28 Jul 1991 08:12:46   keffer
 * Tools.h++ V4.0.5 PVCS baseline version
 *
 */

#include "rw/colclass.h"
#include "rw/mempool.h"

// Set the (half)order of the B-Tree:
#ifdef RDEBUG
  const unsigned rworder  = 2; /* heavy excercise if RDEBUG! */
#else
  const unsigned rworder  = 50; /* realistic "production" value */
#endif
  const unsigned rworder2 = 2*rworder;

/****************************************************************
 *                                                              *
 *                      RWBTreeNode                             *
 *                                                              *
 ****************************************************************/

#ifdef RW_PRAGMA_EXPORT
#pragma export on
#endif

class RWExport RWBTreeNode   RWMemoryPool_OPTION {
friend class RWExport RWBTree;

  RWBTreeNode();                        // Private constructors.
  RWBTreeNode(RWCollectable*);
  unsigned      counter;                // count of [rworder2] fields in use.
  RWCollectableP key[rworder2];         // Array of pointers keys.
  RWBTreeNode*  next[rworder2+1];       // Array of pointers to child nodes.
  unsigned      binarySearch(const RWCollectable*) const;
  void          initialize();
  RWBoolean     subSetOf(const RWBTree& bt) const;
};

/****************************************************************
 *                                                              *
 *                      RWBTree                                 *
 *                                                              *
 ****************************************************************/

class RWExport RWBTree : public RWCollection {

  RWDECLARE_COLLECTABLE(RWBTree)

public:

  RWBTree();
  virtual ~RWBTree();
  RWBTree(const RWBTree&);

  void                          operator=(const RWBTree&);
  // operator <= means "subsetOf()"
#ifndef RW_GLBL_SCP_BUG2
  RWBoolean         operator<=(const RWBTree& bt) const;
#else
// We must define an operator<=() that won't be overriden by ::operator<=()
  RWBoolean         rwBTree_operator_le(const RWBTree& bt) const;
#endif
  RWBoolean                     operator==(const RWBTree& bt) const;

// Special member function to return the height of the B-tree.
  unsigned                      height() const;

/************ Standard Collection classes functions **************/
  virtual void                  apply(RWapplyCollectable, void*);
  virtual void                  clear();
//virtual void                  clearAndDestroy();
//virtual RWBoolean             contains(const RWCollectable*) const;
  virtual size_t                entries() const;
  virtual RWCollectable*        find(const RWCollectable*) const;
  virtual RWCollectable*        insert(RWCollectable*);
  virtual RWBoolean             isEqual(const RWCollectable*) const;
  virtual RWBoolean             isEmpty() const {return root==rwnil;}
  virtual size_t                occurrencesOf(const RWCollectable*) const;
  virtual RWCollectable*        remove(const RWCollectable*);
//virtual void                  removeAndDestroy(const RWCollectable*);

private:

  RWBTreeNode*          root;           // root = first node in tree.
  // the following two pointers were used in a way that broke re-
  // entrancy. We have retained "dummyPtr" so that the binary image 
  // will be the same size as before. We have retained "sizeHack" for
  // the same reason, and are also using it to store the current number
  // of items stored, so we can avoid walking the whole tree to compute
  // entries();
  RWCollectable*        dummyPtr;   // was RWCollectable* tempKey;
  char*                 sizeHack;   // was RWBTreeNode*   tempNode;

protected:

  // Apply to all children
  void                  apl(RWBTreeNode*, RWapplyCollectable, void*);
  // Delete all children.
  void                  del(RWBTreeNode*);
  // insert recursively
  int                   ins(RWCollectable* a, RWBTreeNode*,
                            RWCollectable*&, RWBTreeNode*&);
  // remove all children of this node
  int                   rem(const RWCollectable* a, RWBTreeNode*, RWCollectable*&);
};    


/*  
 *  Special fix for Borland 5.0, this is related to bug #2437.  Borland 5.0
 *  will not choose class scope over global scope ever, but will recognize
 *  this as a function template specialization.
 *
 *  Symantec for Power Macintosh v8.0.4d11 also chooses global
 *  scope.  In this particualr case,it can't be forced to use class scope,
 *  ie. ClassName::classFn()  and classInstance.classFn()
 *  don't overide ::classFn().
 */
 
#ifndef RW_NO_STL
# if defined(RW_GLOBAL_BEFORE_CLASS_SCOPE) || defined( RW_GLBL_SCP_BUG )
inline bool operator<=(const RWBTree& x, const RWBTree& y)
{
#  ifndef RW_GLBL_SCP_BUG2
    return x.operator<=(y);
#  else
    return x.rwBTree_operator_le(y);
#  endif  /* RW_GLBL_SCP_BUG2 */
}
# endif
#endif  /* RW_NO_STL */

#ifdef RW_PRAGMA_EXPORT
#pragma export off
#endif

#endif /* __RWBTREE_H__ */

