home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 25 / nopv25.iso / 040A / PBL30SRC.ZIP / COMDEF.H < prev    next >
Encoding:
C/C++ Source or Header  |  1997-04-13  |  2.8 KB  |  86 lines

  1. /*
  2.  * This file is part of PB-Lib v3.0 C++ Programming Library
  3.  *
  4.  * Copyright (c) 1995, 1997 by Branislav L. Slantchev
  5.  * A fine product of Silicon Creations, Inc. (gargoyle)
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the License which accompanies this
  9.  * software. This library is distributed in the hope that it will
  10.  * be useful, but without any warranty; without even the implied
  11.  * warranty of merchantability or fitness for a particular purpose.
  12.  *
  13.  * You should have received a copy of the License along with this
  14.  * library, in the file LICENSE.DOC; if not, write to the address
  15.  * below to receive a copy via electronic mail.
  16.  *
  17.  * You can reach Branislav L. Slantchev (Silicon Creations, Inc.)
  18.  * at bslantch@cs.angelo.edu. The file SUPPORT.DOC has the current
  19.  * telephone numbers and the postal address for contacts.
  20. */
  21.  
  22. #ifndef INCLUDED_COMDEF_H
  23. #define INCLUDED_COMDEF_H
  24.  
  25. #ifndef __MINMAX_DEFINED
  26. #define __MINMAX_DEFINED
  27.  
  28. #ifdef MINMAX_TEMPLATE
  29.  
  30. template <class ClassType>
  31. inline const ClassType&
  32. min(const ClassType &a, const ClassType &b)
  33. {
  34.     return (a < b) ? a : b;
  35. }
  36.  
  37. template <class ClassType>
  38. inline const ClassType&
  39. max(const ClassType &a, const ClassType &b)
  40. {
  41.     return (a > b) ? a : b;
  42. }
  43.  
  44. #else
  45.  
  46. #define min(a,b) ((a) <= (b) ? (a) : (b))
  47. #define max(a,b) ((a) >= (b) ? (a) : (b))
  48.  
  49. #endif /* templatized versions */
  50.  
  51. #endif /* __MINMAX_DEFINED */
  52.  
  53. template <class ClassType>
  54. inline void
  55. swap(ClassType &a, ClassType &b)
  56. {
  57.     ClassType temp = a;
  58.     a = b;
  59.     b = temp;
  60. }
  61.  
  62. /*
  63.  * b i t   m a n i p u l a t i o n   f o r   i n t e g r a l   t y p e s
  64.  * ──────────────────────────────────────────────────────────────────────────
  65.  * these bit manipulation macros work with integers, chars and longs. note
  66.  * that bits are conventionally numbered from right to left, 0 to 7, 15 or 31
  67. */
  68. #define iTestBit(value, bitno)   Boolean((value) & (1L << (bitno)))
  69. #define iSetBit(value, bitno)    ( (value) |=  (1L << (bitno)) )
  70. #define iClearBit(value, bitno)  ( (value) &= ~(1L << (bitno)) )
  71. #define iToggleBit(value, bitno) ( (value) ^=  (1L << (bitno)) )
  72.  
  73. /*
  74.  * b i t   m a n i p u l a t i o n   f o r   a r r a y s
  75.  * ──────────────────────────────────────────────────────────────────────────
  76.  * these macros manipulate bits in arrays of 8-bit chars. note that the bits
  77.  * are numbered consecutively, starting from 0, up to the number of chars in
  78.  * the array * 8 - 1. (an array of 10 chars will hold 80 bits, from 0 to 79)
  79. */
  80. #define TestBit(a, bitno)       Boolean(a[(bitno)>>3] & (1<<((bitno) & 7)))
  81. #define SetBit(a, bitno)        ( a[(bitno) >> 3] |=  (1 << ((bitno) & 7)) )
  82. #define ClearBit(a, bitno)      ( a[(bitno) >> 3] &= ~(1 << ((bitno) & 7)) )
  83. #define ToggleBit(a, bitno)     ( a[(bitno) >> 3] ^=  (1 << ((bitno) & 7)) )
  84.  
  85. #endif /* INCLUDED_COMDEF_H */
  86.