home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / layout / bits.h next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  3.9 KB  |  189 lines

  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19.  
  20. // TABS 3
  21. //
  22. // Bits array implementation
  23. //
  24. // Lloyd W. Tabb
  25. //
  26. #ifndef _bits_h_
  27. #define _bits_h_
  28.  
  29. //
  30. //
  31.  
  32. #ifdef min
  33. #undef min
  34. #endif
  35. #define min(a, b) (((a) < (b)) ? (a) : (b))
  36. #ifdef max
  37. #undef max
  38. #endif
  39. #define max(a, b) (((a) > (b)) ? (a) : (b))
  40.  
  41. class BitReference {
  42. public:
  43.     uint8 *m_pBits;
  44.     uint8 m_mask;
  45.     BitReference(uint8* pBits, uint8 mask){
  46.         m_pBits = pBits;
  47.         m_mask = mask;
  48.     }
  49.  
  50.     int operator= (int i){
  51.         *m_pBits = (i ? *m_pBits | m_mask : *m_pBits & ~m_mask);
  52.         return !!i;
  53.     }
  54.  
  55.     operator int(void){
  56.         return !!(*m_pBits & m_mask);
  57.     }
  58. };
  59.  
  60. #if 00 /* some compiler don't seem to handle this type of template */
  61.  
  62.  
  63. template <int iSize>
  64. class TBitArray {
  65. private:
  66.     uint8 m_Bits[iSize/8+1];
  67. public:
  68.     BitArray(){
  69.         memset(&m_Bits,0, iSize/8+1);
  70.     }
  71.  
  72.     BitReference operator [] (int i){
  73.         return BitReference(&m_Bits[i >> 3], 1 << (i & 7));
  74.     }
  75.  
  76. };
  77.  
  78.  
  79.  
  80. #endif
  81.  
  82.  
  83. #define BIT_ARRAY_END   -1
  84.  
  85. class CBitArray {
  86. private:
  87.    uint8 *m_Bits;
  88.    long size;
  89. public:
  90.    //
  91.    //  This constructor is unused, and link clashes with 
  92.    //  CBitArray(long n, int iFirst, ...) for some Cfront based
  93.    //  compilers (HP, SGI 5.2, ...)...djw
  94.    //
  95. #if 0
  96.    CBitArray(long n=0) : m_Bits(0), size(0)  {
  97.       if( n ){
  98.          SetSize(n);
  99.       }
  100.    }
  101. #endif
  102.  
  103.    //
  104.    //  Call this constructor with a maximum number, a series of bits and 
  105.    //  BIT_ARRAY_END  for example: CBitArray a(100, 1 ,4 9 ,7, BIT_ARRAY_END);
  106.    //
  107.    //  CBitArray::CBitArray(long n, int iFirst, ...) is now edtutil.cpp.
  108.    //  Had to move it into a C++ file so that it could be non-inline.
  109.    //  varargs/inline is a non-portable combo...djw
  110.    // 
  111.    CBitArray(long n, int iFirst, ...);
  112.  
  113.    void SetSize(long n);
  114.  
  115.    long Size(){ return size; }
  116.  
  117.    BitReference operator [] (int i){
  118.       XP_ASSERT(i >= 0 && i < size );
  119.       return BitReference(&m_Bits[i >> 3], 1 << (i & 7));
  120.    }
  121.  
  122.    void ClearAll();
  123. };
  124.  
  125.  
  126.  
  127. #if 00
  128. //
  129. // This template will generate a Set that can contain all possible values.
  130. //  for an enum.  Sets are created empty.
  131. //
  132. //  ASSUMES:
  133. //    all enum elements are contiguous.
  134. //
  135. //  EXAMPLE:
  136. //
  137. //    enum CType {
  138. //       ctAlpha,
  139. //       ctVoul,
  140. //       ctDigit,
  141. //       ctPunctuation,
  142. //
  143. //       ctMax,
  144. //    };
  145. //
  146. //    typedef TSet<CType,ctMax> Set_CType;
  147. //
  148. //    Set_CType x;
  149. //    x.Add(ctAlpha);         // x has the attribute alpha
  150. //    x.Add(ctVoul);          // x has the attribute Voul
  151. //
  152. //
  153. template <class SetEnum, int setMax>
  154. class TSet {
  155.     char bits[setMax/8+1];
  156. public:
  157.    TSet(){
  158.       memset(bits,0,sizeof(bits));
  159.    }
  160.  
  161.    //
  162.    // check to see if enum value is in the set
  163.    //
  164.    int In( SetEnum v ){
  165.       return !!(bits[v>>3] & (1<<(v & 7)));
  166.    }
  167.  
  168.    //
  169.    // Remove Enum from the set
  170.    //
  171.    void Remove( SetEnum v ){
  172.      // bits[v/8] &= ~(1<<v%8);
  173.      bits[v>>3] &= ~(1<< (v & 7));
  174.    }
  175.  
  176.    //
  177.    // Add Enum to the set
  178.    //
  179.    void Add( SetEnum v ){
  180.      //bits[v/8] |= 1<<v%8;
  181.      bits[v>>3] |= 1<< (v & 7);
  182.    }
  183.  
  184. };
  185.  
  186. #endif
  187.  
  188. #endif
  189.