home *** CD-ROM | disk | FTP | other *** search
/ PC Format (South-Africa) 2001 May / PCFMay2001.iso / Xenon / C++ / FreeCommandLineTools.exe / Include / Rw / collate.cc < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-31  |  4.5 KB  |  143 lines

  1. #ifndef __COLLATE_CC
  2. #define __COLLATE_CC
  3. #pragma option push -b -a8 -pc -Vx- -Ve- -w-inl -w-aus -w-sig
  4. /***************************************************************************
  5.  *
  6.  * collate.cc - Definitions for the Standard Library character collation facet
  7.  *
  8.  ***************************************************************************
  9.  *
  10.  * Copyright (c) 1994-1999 Rogue Wave Software, Inc.  All Rights Reserved.
  11.  *
  12.  * This computer software is owned by Rogue Wave Software, Inc. and is
  13.  * protected by U.S. copyright laws and other laws and by international
  14.  * treaties.  This computer software is furnished by Rogue Wave Software,
  15.  * Inc. pursuant to a written license agreement and may be used, copied,
  16.  * transmitted, and stored only in accordance with the terms of such
  17.  * license and with the inclusion of the above copyright notice.  This
  18.  * computer software or any other copies thereof may not be provided or
  19.  * otherwise made available to any other person.
  20.  *
  21.  * U.S. Government Restricted Rights.  This computer software is provided
  22.  * with Restricted Rights.  Use, duplication, or disclosure by the
  23.  * Government is subject to restrictions as set forth in subparagraph (c)
  24.  * (1) (ii) of The Rights in Technical Data and Computer Software clause
  25.  * at DFARS 252.227-7013 or subparagraphs (c) (1) and (2) of the
  26.  * Commercial Computer Software รป Restricted Rights at 48 CFR 52.227-19,
  27.  * as applicable.  Manufacturer is Rogue Wave Software, Inc., 5500
  28.  * Flatiron Parkway, Boulder, Colorado 80301 USA.
  29.  *
  30.  **************************************************************************/
  31.  
  32. #ifndef _RWSTD_NO_NAMESPACE
  33. namespace std {
  34. #endif
  35.  
  36. // --------------------------------------
  37. // Facet collate<charT> member templates.
  38. // --------------------------------------
  39.  
  40. template <class charT>
  41. locale::id collate<charT>::id;
  42.  
  43. template <class charT>
  44. collate<charT>::~collate() { }
  45.  
  46. template <class charT>
  47. int collate<charT>::do_compare
  48.     (const charT* low1, const charT* high1,
  49.      const charT* low2, const charT* high2) const
  50. {
  51.   size_t len1=high1-low1;
  52.   size_t len2=high2-low2;
  53.   size_t len = len1 < len2 ? len1 : len2;
  54.   int comp;
  55.  
  56.   for (const charT *p=low1,*q=low2; len--; p++,q++)
  57.     if ((comp=__coll_order(*p)-__coll_order(*q))!=0)
  58.       return comp<0? -1 : 1;
  59.  
  60.   if (len1<len2)
  61.     return -1;
  62.   if (len2<len1)
  63.     return 1;
  64.   return 0;
  65. }
  66.  
  67. template <class charT>
  68. _TYPENAME collate<charT>::string_type
  69. collate<charT>::do_transform
  70.     (const charT* low, const charT* high) const
  71. {
  72.   string_type result(high-low,'\0');
  73.   _TYPENAME string_type::iterator out=result.begin();
  74.   for (const charT *p=low; p!=high; p++)
  75.     *out++=__coll_order(*p);
  76.   return result;
  77. }
  78.  
  79. template <class charT>
  80. long collate<charT>::do_hash (const charT *start, const charT *end) const
  81. {
  82.   // We hash the result of do_transform, so that keys that transform equally
  83.   // will hash equally, per the draft.
  84.  
  85.   string_type s=do_transform(start,end);
  86.   start=s.begin();
  87.   end=s.end();
  88.  
  89.   // Peter Weinberger's generic hashing algorithm, adapted by Andrew Binstock
  90.   // from a version by Allen Holub (see Andrew Binstock, "Hashing Revisited",
  91.   // Dr. Dobb's Journal, April 1996) and templatized by Rogue Wave.
  92.  
  93.   const int long_bits=CHAR_BIT*sizeof(long);
  94.   const int one_eighth=long_bits/8;
  95.   const int three_fourths=long_bits*3/4;
  96.   const int high_bits=(int)((~0L) << (long_bits-one_eighth));
  97.  
  98.   long result=0;
  99.   for (const charT *p=start; start<end; start++) {
  100.     result=(result << one_eighth) + *p;
  101.     long temp=result & high_bits;
  102.     if (temp)
  103.       result=(result^(temp>>three_fourths)) &~ high_bits;
  104.   }
  105.  
  106.   return result;
  107. }
  108.  
  109. // --------------------------------------------------------------------
  110. // Character collation by-name member templates: collate_byname<charT>
  111. // --------------------------------------------------------------------
  112.  
  113. template <class charT>
  114. collate_byname<charT>::collate_byname (const char* /*name*/, size_t refs):
  115.     collate<charT>(refs)
  116. { }
  117.  
  118. template <class charT>
  119. collate_byname<charT>::~collate_byname()
  120. { }
  121.  
  122. template <class charT>
  123. int collate_byname<charT>::do_compare
  124.     (const charT* /*low1*/, const charT* /*high1*/,
  125.      const charT* /*low2*/, const charT* /*high2*/) const
  126. {
  127.   return 0;
  128. }
  129.  
  130. template <class charT>
  131. _TYPENAME collate_byname<charT>::string_type
  132. collate_byname<charT>::do_transform
  133.     (const charT* /*low*/, const charT* /*high*/) const
  134. {
  135.   return string_type();
  136. }
  137.  
  138. #ifndef _RWSTD_NO_NAMESPACE
  139. } // namespace std
  140. #endif
  141. #pragma option pop
  142. #endif /* __COLLATE_CC */
  143.