Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.4 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1990 - 1999
  6. //
  7. // File: bitset.cxx
  8. //
  9. //--------------------------------------------------------------------------
  10. /* --------------------------------------------------------------------
  11. Microsoft OS/2 LAN Manager
  12. Copyright(c) Microsoft Corp., 1990
  13. -------------------------------------------------------------------- */
  14. /* --------------------------------------------------------------------
  15. File : bitset.cxx
  16. Title : Bit vector implementation of a set.
  17. History :
  18. mikemon ??-??-?? Beginning of recorded history.
  19. mikemon 11-13-90 Commented the source.
  20. -------------------------------------------------------------------- */
  21. #include <precomp.hxx>
  22. #include <bitset.hxx>
  23. int
  24. BITSET::Insert (
  25. IN int Key
  26. )
  27. {
  28. unsigned int * pNewBits;
  29. int cCount;
  30. //
  31. // Check if the Key will fit into the current bit vector.
  32. //
  33. if ((int) ((Key/(sizeof(int)*8))+1) > cBits)
  34. {
  35. //
  36. // We need more space in the bit vector, so allocate enough space
  37. // to hold the bit for the specified key, copy the old bit vector into the
  38. // new bit vector, and then free the old one.
  39. //
  40. pNewBits = new unsigned int [Key/(sizeof(int)*8)+1];
  41. if (pNewBits == (unsigned int *) 0)
  42. return(1);
  43. for (cCount = 0; cCount < cBits; cCount++)
  44. pNewBits[cCount] = pBits[cCount];
  45. cBits = Key/(sizeof(int)*8) + 1;
  46. for ( ; cCount < cBits; cCount++)
  47. pNewBits[cCount] = 0;
  48. if (pBits != &InitialStorage)
  49. delete pBits;
  50. pBits = pNewBits;
  51. }
  52. //
  53. // Turn on the appropriate bit, by ORing it in.
  54. //
  55. pBits[Key/(sizeof(int)*8)] |= (1 << (Key % (sizeof(int)*8)));
  56. return(0);
  57. }
  58. void
  59. BITSET::Delete (
  60. IN int Key
  61. )
  62. {
  63. if ((int) (Key/(sizeof(int)*8)) > cBits-1)
  64. return;
  65. //
  66. // Turn off the appropriate bit, by ANDing in the complement.
  67. //
  68. pBits[Key/(sizeof(int)*8)] &= ~(1 << (Key % (sizeof(int)*8)));
  69. }
  70. int
  71. BITSET::MemberP (
  72. IN int Key
  73. )
  74. {
  75. if ((int) (Key/(sizeof(int)*8)) > cBits-1)
  76. return(0);
  77. //
  78. // Test for the appropriate bit
  79. //
  80. return(((pBits[Key/(sizeof(int)*8)]
  81. & (1 << (Key % (sizeof(int)*8)))) ? 1 : 0));
  82. }