PageRenderTime 224ms CodeModel.GetById 120ms app.highlight 9ms RepoModel.GetById 92ms app.codeStats 0ms

/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
 30
 31#include "bitset.h"
 32
 33
 34cg_bitset_t * cg_bitset_create(cg_heap_t * heap, size_t elements)
 35{
 36	size_t words = (elements + CG_BITSET_BITS_PER_WORD - 1) / CG_BITSET_BITS_PER_WORD;
 37	
 38	cg_bitset_t * result = (cg_bitset_t *)
 39		cg_heap_allocate(heap, sizeof(cg_bitset_t) + words * sizeof(U32));
 40	
 41	result->elements = elements;
 42	return result;
 43}
 44
 45
 46void cg_bitset_assign(cg_bitset_t * target, cg_bitset_t * source) 
 47{
 48	size_t index;
 49	
 50	assert(target);
 51	assert(source);
 52	assert(target->elements == source->elements);
 53	
 54	for (index = 0; index < target->elements / CG_BITSET_BITS_PER_WORD; ++index)
 55	{
 56		target->bits[index] = source->bits[index];
 57	}
 58}
 59
 60
 61/* target = target U (source \ minus) */
 62int cg_bitset_union_minus(cg_bitset_t * target, cg_bitset_t * source,
 63						   cg_bitset_t * minus)
 64{
 65	size_t index;
 66	int result = 0;
 67	
 68	assert(target);
 69	assert(source);
 70	assert(minus);
 71	assert(target->elements == source->elements);
 72	assert(target->elements == minus->elements);
 73	
 74	for (index = 0; index < target->elements / CG_BITSET_BITS_PER_WORD; ++index)
 75	{
 76		U32 old = target->bits[index];
 77		target->bits[index] |= source->bits[index] & ~minus->bits[index];
 78
 79		result |= (target->bits[index] != old);
 80	}
 81
 82	return result;
 83}
 84
 85
 86int cg_bitset_union(cg_bitset_t * target, cg_bitset_t * source)
 87{
 88	size_t index;
 89	int result = 0;
 90	
 91	assert(target);
 92	assert(source);
 93	assert(target->elements == source->elements);
 94	
 95	for (index = 0; index < target->elements / CG_BITSET_BITS_PER_WORD; ++index)
 96	{
 97		U32 old = target->bits[index];
 98		target->bits[index] |= source->bits[index];
 99
100		result |= (target->bits[index] != old);
101	}
102
103	return result;
104}
105
106
107int cg_bitset_intersects(const cg_bitset_t * first, const cg_bitset_t * second) 
108{
109	size_t index;
110
111	assert(first);
112	assert(second);
113	assert(first->elements == second->elements);
114	
115	for (index = 0; index < first->elements / CG_BITSET_BITS_PER_WORD; ++index)
116	{
117		if (first->bits[index] & second->bits[index])
118			return 1;
119	}
120
121	return 0;
122}