// -*- C++ -*-
/***************************************************************************
 *
 * collate - Declarations for the Standard Library character collation facet
 *
 ***************************************************************************
 *    
 *  Copyright 2000 Compaq Computer Corporation
 *
 *  COMPAQ Registered in U.S. Patent and Trademark Office.
 *
 *  Confidential computer software. Valid license from Compaq required for
 *  possession, use or copying. Consistent with FAR 12.211 and 12.212,
 *  Commercial Computer Software, Computer Software Documentation, and
 *  Technical Data for Commercial Items are licensed to the U.S. Government
 *  under vendor's standard commercial license.
 *
 ****************************************************************************
 *
 * (c) Copyright 1994, 1998 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.
 *
 **************************************************************************/

#ifndef __STD_COLLATE__
#define __STD_COLLATE__

#ifndef __STD_RWLOCALE__
#include <rwlocale>
#endif

#if defined(__DECCXX)
#   ifdef __PRAGMA_ENVIRONMENT
#      pragma __environment __save
#      pragma __environment __cxx_header_defaults
#   endif
#endif

#if defined(__VMS) && defined(__DECCXX) && !defined(__DECFIXCXXL1158)
#pragma __extern_prefix __save
#pragma __extern_prefix "CXXL$" 
#endif

#ifndef _RWSTD_NO_NAMESPACE
namespace _RWrwstd {
#endif

// ------------------------------------------------------
// Implementation class template -- collate_table<charT>.
// ------------------------------------------------------

// Data structure that drives the collate<charT> process.  It contains a
// binary tree of nodes, each of which defines the collation treatment to be
// applied to a range of characters.

template <class charT>
class _RWSTDExportTemplate collate_table {
 public:
  struct node {
    node *left;             // Node for charT values that are too small (resp.
    node *right;            //  too large) for this node
    const charT *table;     // Translate table or NULL
    charT minC,maxC;        // Range of charT-s covered by this node
    charT offset;           // Amount to add to charT if table is NULL
  };

  long length;              // Length of memory block containing all the nodes
  node root;                // Root node of table
};

// -----------------------------------------------------
// Implementation class template -- collate_data<charT>.
// -----------------------------------------------------

// collate<charT> derives from this (via rwstd::collate_impl) to get its
// private data members.

template <class charT>
class _RWSTDExportTemplate collate_data
{
 public:
  typedef collate_table<charT> table;
  typedef _TYPENAME table::node node;

  const table *_RWtable;

  collate_data (const table &t): _RWtable(&t) { }
  charT _RWcoll_order (charT) const;
};

template <class charT>
inline charT collate_data<charT>::_RWcoll_order (charT c) const
{
  const node *t=&_RWtable->root;
  while (t)
    if (c < t->minC)
      t=t->left;
    else if (c > t->maxC)
      t=t->right;
    else {
      if (t->table)
        c=t->table[size_t(c-t->minC)];
      else
        c+=t->offset;
      break;
    }

  return c;
}

// -----------------------------------------------------
// Implementation class template -- collate_impl<charT>.
// -----------------------------------------------------

// Facet collate<charT> derives from this to get the part of its behavior that
// is specialized for char and wchar_t.

template <class charT>
class _RWSTDExportTemplate collate_impl :
    public collate_data<charT>
{
  static collate_table<charT> _RWdefault_table;
 public:
  collate_impl (void):
      collate_data<charT>(_RWdefault_table) { }
};

// Specialization for char.
_RWSTD_TEMPLATE
class _RWSTDExport collate_impl<char>:
    public collate_data<char>
{
  static collate_table<char> _RWSTDExportStatic _RWdefault_table;
 public:
  collate_impl (void):
      collate_data<char>(_RWdefault_table) { }
};

// Specialization for wchar_t.
#ifndef _RWSTD_NO_WIDE_CHAR
_RWSTD_TEMPLATE
class _RWSTDExport collate_impl<wchar_t>:
    public collate_data<wchar_t>
{
  static collate_table<wchar_t> _RWSTDExportStatic _RWdefault_table;

 public:
  collate_impl (void):
      collate_data<wchar_t>(_RWdefault_table) { }
};
#endif

#ifndef _RWSTD_NO_NAMESPACE
} namespace std {
#endif

// -----------------------------------------------------
// Standard character collation facet -- collate<charT>.
// -----------------------------------------------------

template <class charT>
class collate: public locale::facet,
    public __RWSTD::collate_impl<charT>
{
 public:
  typedef charT char_type;
  typedef basic_string<charT,char_traits<charT>,allocator<charT> > string_type;

  _EXPLICIT collate (size_t refs=0):
      locale::facet(refs,locale::_RWrw_collate_category)
    { }

  int compare (const charT* low1, const charT* high1,
               const charT* low2, const charT* high2) const
    { return do_compare(low1, high1, low2, high2); }

  string_type transform (const charT* low, const charT* high) const
    { return do_transform(low, high); }

  long hash (const charT* low, const charT* high) const
    { return do_hash(low, high); }

  static locale::id id;

  // Implementation:
  enum { _RWfacet_cat = locale::_RWrw_collate_category, _RWok_implicit = 1 };

 protected:
  virtual ~collate();

  virtual int do_compare (const charT* low1, const charT* high1,
                          const charT* low2, const charT* high2) const;
  virtual string_type do_transform (const charT* low, const charT* high) const;
  virtual long do_hash (const charT* low, const charT* high) const;

 private:
  #ifdef _RWSTD_NO_MEMBER_TEMPLATES
  locale::id &_RWget_id (void) const { return id; }
  #endif
};

// ------------------------------------------------------
// Standard facet specialization -- collate_byname<char>.
// ------------------------------------------------------

template <class charT>
class collate_byname;

#if defined(__DECCXX) && !defined(__DECFIXCXXL646)
#else
_RWSTD_TEMPLATE
class _RWSTDExport collate_byname<char>: public collate<char> {
 public:
  _EXPLICIT collate_byname (const char*, size_t refs=0);
 protected:
  virtual ~collate_byname ();

  // Virtual member functions, override functions in collate<char>.
  virtual int do_compare (const char* low1, const char* high1,
                          const char* low2, const char* high2) const;
  virtual string do_transform (const char* low, const char* high) const;

  // Virtual member functions inherited from collate<char>:
  // virtual long do_hash (const char* low, const char* high) const;

 private:
  // Implementation.
  string _RWcollate_name;
};
#endif

// ------------------------------------------------
// Standard derived facet -- collate_byname<charT>.
// ------------------------------------------------

template <class charT>
class  collate_byname: public collate<charT> {
 public:
  typedef basic_string<charT,char_traits<charT>,
                                allocator<charT> > string_type;

  _EXPLICIT collate_byname (const char*, size_t refs=0);

 protected:
  virtual ~collate_byname();

  // Virtual member functions overridden from collate<charT>.
  virtual int do_compare (const charT* low1, const charT* high1,
                          const charT* low2, const charT* high2) const;
  virtual string_type do_transform (const charT* low, const charT* high) const;

  // Virtual member functions inherited from collate<charT>:
#if defined(__DECCXX) && !defined(__DECFIXCXXL646)
  virtual long do_hash (const charT* low, const charT* high) const;
#else
  // virtual long do_hash (const charT* low, const charT* high) const;
#endif

 private:
  // Implementation.
  string_type _RWcollate_name;
};


#if defined(__DECCXX) && !defined(__DECFIXCXXL646)

template<>
class _RWSTDExport collate_byname<char>: public collate<char> {

 private:
  _LocaleManager* _LocaleManagerPtr;

 public:
  _EXPLICIT collate_byname (const char*, size_t refs=0);

 protected:
  virtual ~collate_byname ();

  // Virtual member functions, override functions in collate<char>.
  virtual int do_compare (const char* low1, const char* high1,
                          const char* low2, const char* high2) const;
  virtual string_type do_transform (const char* low, const char* high) const;

#if defined(__DECCXX) && !defined(__DECFIXCXXL663)
  virtual long do_hash(const char* low, const char* high) const;
#else
  long do_hash(const char* low, const char* high) const;
#endif

 private:
  // Implementation.
  string _RWcollate_name_;
};

// ---------------------------------------------------------
// Standard facet specialization -- collate_byname<wchar_t>.
// ---------------------------------------------------------

#ifndef _RWSTD_NO_WIDE_CHAR

template<>
class _RWSTDExport collate_byname<wchar_t>: public collate<wchar_t> {

 private:
  _LocaleManager* _LocaleManagerPtr;

 public:
  _EXPLICIT collate_byname (const char*, size_t refs=0);

 protected:
  virtual ~collate_byname ();

  // Virtual member functions, override functions in collate<char>.
  virtual int do_compare (const wchar_t* low1, const wchar_t* high1,
                          const wchar_t* low2, const wchar_t* high2) const;
  virtual string_type do_transform (const wchar_t* low, const wchar_t* high) const;

#if defined(__DECCXX) && !defined(__DECFIXCXXL663)
  virtual long do_hash(const wchar_t* low, const wchar_t* high) const;
#else
  long do_hash(const wchar_t* low, const wchar_t* high) const;
#endif

 private:
  // Implementation.
  string _RWcollate_name_;
};

#endif // __RWSTD_NO_WIDE_CHAR

#endif // 646

#ifndef _RWSTD_NO_NAMESPACE
} namespace _RWrwstd {
#endif

#ifndef _RWSTD_NO_FUNC_PARTIAL_SPEC
template <class charT>
inline _RW_STD::collate<charT>* create_named_facet
    (_RW_STD::collate<charT>*,const char *name,size_t refs)
{ return new _RW_STD::collate_byname<charT>(name,refs); }
#else

inline _RW_STD::collate<char>* create_named_facet
    (_RW_STD::collate<char>*,const char *name,size_t refs)
{ return new _RW_STD::collate_byname<char>(name,refs); }
#ifndef _RWSTD_NO_WIDE_CHAR

inline _RW_STD::collate<wchar_t>* create_named_facet
    (_RW_STD::collate<wchar_t>*,const char *name,size_t refs)
{ return new _RW_STD::collate_byname<wchar_t>(name,refs); }
#endif
#endif

#ifndef _RWSTD_NO_NAMESPACE
} 
#endif

#if defined(__VMS) && defined(__DECCXX) && !defined(__DECFIXCXXL1158)
#   pragma __extern_prefix __restore
#endif

#ifdef _RWSTD_COMPILE_INSTANTIATE
#include <collate.cc>
#endif


#if defined(__DECCXX)
#   ifdef __PRAGMA_ENVIRONMENT
#      pragma __environment __restore
#   endif
#endif

#endif // __STD_COLLATE__
