/*=============================================================================*\ * File: gfsmBitVector.def * Author: Bryan Jurish * Description: finite state machine library: bit vectors: inline definitions * * Copyright (c) 2004-2008 Bryan Jurish. * * For information on usage and redistribution, and for a DISCLAIMER OF ALL * WARRANTIES, see the file, "LICENSE.txt," in this distribution. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *=============================================================================*/ #include #include /*====================================================================== * Utilities */ //-------------------------------------------------------------- // bits2bytes() GFSM_INLINE guint gfsm_bitvector_bits2bytes_(guint nbits) { return nbits/8; } //-------------------------------------------------------------- // bytes2bits() GFSM_INLINE guint gfsm_bitvector_bytes2bits_(guint nbytes) { return nbytes*8; } /*-------------------------------------------------------------- * size() */ GFSM_INLINE guint gfsm_bitvector_size(gfsmBitVector *bv) { return gfsm_bitvector_bytes2bits_(bv->len); } /*-------------------------------------------------------------- * get() */ GFSM_INLINE gboolean gfsm_bitvector_get(gfsmBitVector *bv, guint i) { if (i < gfsm_bitvector_size(bv)) { return g_array_index(bv, guint8, gfsm_bitvector_bits2bytes_(i)) & (1<<(i%8)) ? TRUE : FALSE; } return FALSE; } /*-------------------------------------------------------------- * resize() */ GFSM_INLINE void gfsm_bitvector_resize(gfsmBitVector *bv, guint nbits) { g_array_set_size(bv, gfsm_bitvector_bits2bytes_(nbits)+1); } /*-------------------------------------------------------------- * set() */ GFSM_INLINE void gfsm_bitvector_set(gfsmBitVector *bv, guint i, gboolean v) { if (i >= gfsm_bitvector_size(bv)) { gfsm_bitvector_resize(bv,i); } if (v) { g_array_index(bv, guint8, gfsm_bitvector_bits2bytes_(i)) |= (1<<(i%8)); } else { g_array_index(bv, guint8, gfsm_bitvector_bits2bytes_(i)) &= ~(1<<(i%8)); } } /*-------------------------------------------------------------- * new() */ GFSM_INLINE gfsmBitVector *gfsm_bitvector_new(void) { return g_array_new(FALSE,TRUE,1); } /*-------------------------------------------------------------- * sized_new() */ GFSM_INLINE gfsmBitVector *gfsm_bitvector_sized_new(guint nbits) { gfsmBitVector *bv = g_array_sized_new(FALSE,TRUE,1, gfsm_bitvector_bits2bytes_(nbits)+1); if (nbits) gfsm_bitvector_set(bv, nbits-1, 0); return bv; } /*-------------------------------------------------------------- * free() */ GFSM_INLINE void gfsm_bitvector_free(gfsmBitVector *bv) { g_array_free(bv,TRUE); } /*-------------------------------------------------------------- * clear() */ GFSM_INLINE void gfsm_bitvector_clear(gfsmBitVector *bv) { g_array_set_size(bv,0); } /*-------------------------------------------------------------- * zero() */ GFSM_INLINE gfsmBitVector *gfsm_bitvector_zero(gfsmBitVector *bv) { memset(bv->data, 0, bv->len); return bv; } /*-------------------------------------------------------------- * one() */ GFSM_INLINE gfsmBitVector *gfsm_bitvector_one(gfsmBitVector *bv) { memset(bv->data, 255, bv->len); return bv; }