/opengles/src/codegen/bitset.c

http://ftk.googlecode.com/ · C · 122 lines · 66 code · 27 blank · 29 comment · 12 complexity · bc7ed64657ec57df61386a3ce85029d1 MD5 · raw file

  1. /****************************************************************************/
  2. /* */
  3. /* Copyright (c) 2004, Hans-Martin Will. All rights reserved. */
  4. /* */
  5. /* Redistribution and use in source and binary forms, with or without */
  6. /* modification, are permitted provided that the following conditions are */
  7. /* met: */
  8. /* */
  9. /* * Redistributions of source code must retain the above copyright */
  10. /* notice, this list of conditions and the following disclaimer. */
  11. /* */
  12. /* * Redistributions in binary form must reproduce the above copyright */
  13. /* notice, this list of conditions and the following disclaimer in the */
  14. /* documentation and/or other materials provided with the distribution. */
  15. /* */
  16. /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
  17. /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
  18. /* LIMITED TO, THEIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A */
  19. /* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER */
  20. /* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, */
  21. /* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, */
  22. /* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR */
  23. /* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF */
  24. /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
  25. /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
  26. /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
  27. /* */
  28. /****************************************************************************/
  29. #include "bitset.h"
  30. cg_bitset_t * cg_bitset_create(cg_heap_t * heap, size_t elements)
  31. {
  32. size_t words = (elements + CG_BITSET_BITS_PER_WORD - 1) / CG_BITSET_BITS_PER_WORD;
  33. cg_bitset_t * result = (cg_bitset_t *)
  34. cg_heap_allocate(heap, sizeof(cg_bitset_t) + words * sizeof(U32));
  35. result->elements = elements;
  36. return result;
  37. }
  38. void cg_bitset_assign(cg_bitset_t * target, cg_bitset_t * source)
  39. {
  40. size_t index;
  41. assert(target);
  42. assert(source);
  43. assert(target->elements == source->elements);
  44. for (index = 0; index < target->elements / CG_BITSET_BITS_PER_WORD; ++index)
  45. {
  46. target->bits[index] = source->bits[index];
  47. }
  48. }
  49. /* target = target U (source \ minus) */
  50. int cg_bitset_union_minus(cg_bitset_t * target, cg_bitset_t * source,
  51. cg_bitset_t * minus)
  52. {
  53. size_t index;
  54. int result = 0;
  55. assert(target);
  56. assert(source);
  57. assert(minus);
  58. assert(target->elements == source->elements);
  59. assert(target->elements == minus->elements);
  60. for (index = 0; index < target->elements / CG_BITSET_BITS_PER_WORD; ++index)
  61. {
  62. U32 old = target->bits[index];
  63. target->bits[index] |= source->bits[index] & ~minus->bits[index];
  64. result |= (target->bits[index] != old);
  65. }
  66. return result;
  67. }
  68. int cg_bitset_union(cg_bitset_t * target, cg_bitset_t * source)
  69. {
  70. size_t index;
  71. int result = 0;
  72. assert(target);
  73. assert(source);
  74. assert(target->elements == source->elements);
  75. for (index = 0; index < target->elements / CG_BITSET_BITS_PER_WORD; ++index)
  76. {
  77. U32 old = target->bits[index];
  78. target->bits[index] |= source->bits[index];
  79. result |= (target->bits[index] != old);
  80. }
  81. return result;
  82. }
  83. int cg_bitset_intersects(const cg_bitset_t * first, const cg_bitset_t * second)
  84. {
  85. size_t index;
  86. assert(first);
  87. assert(second);
  88. assert(first->elements == second->elements);
  89. for (index = 0; index < first->elements / CG_BITSET_BITS_PER_WORD; ++index)
  90. {
  91. if (first->bits[index] & second->bits[index])
  92. return 1;
  93. }
  94. return 0;
  95. }