Bitmap implementation
This commit was SVN r864.
Этот коммит содержится в:
родитель
3992c9582a
Коммит
a786afb192
@ -10,6 +10,7 @@ noinst_LTLIBRARIES = liblfc.la
|
||||
# Source code files
|
||||
|
||||
headers = \
|
||||
lam_bitmap.h \
|
||||
lam_hash_table.h \
|
||||
lam_list.h \
|
||||
lam_object.h \
|
||||
@ -22,6 +23,7 @@ liblfc_la_SOURCES = \
|
||||
lam_list.c \
|
||||
lam_object.c \
|
||||
lam_pointer_array.c \
|
||||
lam_bitmap.c \
|
||||
lam_value_array.c
|
||||
|
||||
# Conditionally install the header files
|
||||
|
169
src/lam/lfc/lam_bitmap.c
Обычный файл
169
src/lam/lfc/lam_bitmap.c
Обычный файл
@ -0,0 +1,169 @@
|
||||
/*
|
||||
* $HEADER$
|
||||
*/
|
||||
|
||||
#include "lam/lfc/lam_bitmap.h"
|
||||
|
||||
#define SIZE_OF_CHAR (sizeof(char) * 8)
|
||||
#define LAM_INVALID_BIT -1
|
||||
|
||||
static void lam_bitmap_construct(lam_bitmap_t *bm);
|
||||
static void lam_bitmap_destruct(lam_bitmap_t *bm);
|
||||
|
||||
lam_class_t lam_bitmap_t_class = {
|
||||
"lam_bitmap_t",
|
||||
OBJ_CLASS(lam_object_t),
|
||||
(lam_construct_t)lam_bitmap_construct,
|
||||
(lam_construct_t)lam_bitmap_destruct
|
||||
};
|
||||
|
||||
|
||||
static void
|
||||
lam_bitmap_construct(lam_bitmap_t *bm)
|
||||
{
|
||||
bm->size = 0;
|
||||
bm->array_size = 0;
|
||||
bm->bitmap = NULL;
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
lam_bitmap_destruct(lam_bitmap_t *bm)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
lam_bitmap_init(lam_bitmap_t *bm, size_t size)
|
||||
{
|
||||
size_t actual_size = size / SIZE_OF_CHAR;
|
||||
|
||||
actual_size += (size % SIZE_OF_CHAR == 0) ? 0 : 1;
|
||||
bm->size = actual_size * SIZE_OF_CHAR;
|
||||
bm->bitmap = (char *) realloc(bm->bitmap, actual_size);
|
||||
if (NULL == bm->bitmap)
|
||||
return LAM_ERR_SYSRESOURCE;
|
||||
|
||||
bm->array_size = actual_size;
|
||||
lam_bitmap_clear_all_bits(bm);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
lam_bitmap_set_bit(lam_bitmap_t *bm, int bit)
|
||||
{
|
||||
size_t index, offset, new_size;
|
||||
int i;
|
||||
|
||||
index = bit / SIZE_OF_CHAR - 1;
|
||||
offset = bit % SIZE_OF_CHAR -1;
|
||||
|
||||
if (index >= bm->array_size) {
|
||||
/* We need to allocate more space for the bitmap, since we are
|
||||
out of range. We dont throw any error here, because this is
|
||||
valid and we simply expand the bitmap */
|
||||
|
||||
new_size = index / bm->array_size * bm->array_size;
|
||||
/* New size is just a multiple of the original size to fit in
|
||||
the index */
|
||||
|
||||
bm->bitmap = (char *) realloc(bm->bitmap, new_size);
|
||||
if (NULL == bm->bitmap) {
|
||||
return LAM_ERR_SYSRESOURCE;
|
||||
}
|
||||
|
||||
/* zero out the new elements */
|
||||
for (i = bm->array_size; i < new_size; ++i) {
|
||||
bm->bitmap[i] = 0;
|
||||
}
|
||||
}
|
||||
bm->bitmap[index] |= (1 << offset);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
lam_bitmap_clear_bit(lam_bitmap_t *bm, int bit)
|
||||
{
|
||||
size_t index, offset;
|
||||
|
||||
index = bit / SIZE_OF_CHAR - 1;
|
||||
offset = bit % SIZE_OF_CHAR;
|
||||
|
||||
if (index >= bm->array_size) {
|
||||
return LAM_INVALID_BIT;
|
||||
}
|
||||
|
||||
bm->bitmap[index] &= ~(1 << offset);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
lam_bitmap_is_set_bit(lam_bitmap_t *bm, int bit)
|
||||
{
|
||||
size_t index, offset;
|
||||
|
||||
index = bit / SIZE_OF_CHAR - 1;
|
||||
offset = bit % SIZE_OF_CHAR -1;
|
||||
|
||||
if (index >= bm->array_size) {
|
||||
return LAM_INVALID_BIT;
|
||||
}
|
||||
|
||||
if ((0 != bm->bitmap[index]) & (1 << offset))
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
lam_bitmap_clear_all_bits(lam_bitmap_t *bm)
|
||||
{
|
||||
memset(bm->bitmap, 0, bm->array_size);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
lam_bitmap_set_all_bits(lam_bitmap_t *bm)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < bm->array_size; ++i) {
|
||||
bm->bitmap[i] = ~((char) 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
lam_bitmap_find_and_set_first_unset_bit(lam_bitmap_t *bm)
|
||||
{
|
||||
size_t i;
|
||||
int position = 0;
|
||||
char temp;
|
||||
for (i = 0; i < bm->array_size; ++i) {
|
||||
|
||||
/* Neglect all which dont have an unset bit */
|
||||
while (bm->bitmap[i] == (char)~0)
|
||||
continue;
|
||||
|
||||
/* This one has an unset bit, find its bit number */
|
||||
temp = bm->bitmap[i];
|
||||
while (temp & 0x1) {
|
||||
++position;
|
||||
temp >>= 1;
|
||||
}
|
||||
|
||||
/* Now set the bit number */
|
||||
bm->bitmap[i] |= (bm->bitmap[i] + 1);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
size_t
|
||||
lam_bitmap_size(lam_bitmap_t *bm)
|
||||
{
|
||||
return bm->size;
|
||||
}
|
129
src/lam/lfc/lam_bitmap.h
Обычный файл
129
src/lam/lfc/lam_bitmap.h
Обычный файл
@ -0,0 +1,129 @@
|
||||
/*
|
||||
* $HEADER$
|
||||
*
|
||||
*/
|
||||
|
||||
/** @file
|
||||
*
|
||||
* A bitmap implementation. The bits start off with 0, so this
|
||||
* bitmap has bits numbered as bit 0, bit 1, bit 2 and so on
|
||||
*/
|
||||
|
||||
#ifndef LAM_BITMAP_H
|
||||
#define LAM_BITMAP_H
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "lam_config.h"
|
||||
#include "lam/stdint.h"
|
||||
#include "lam/types.h"
|
||||
#include "lam/lfc/lam_object.h"
|
||||
|
||||
/* VPS: Just to compile right now, has to move later on */
|
||||
#define LAM_ERR_SYSRESOURCE -1
|
||||
|
||||
extern lam_class_t lam_bitmap_t_class;
|
||||
|
||||
struct lam_bitmap_t {
|
||||
lam_object_t super; /**< Subclass of lam_object_t */
|
||||
char *bitmap; /**< The actual bitmap array of characters */
|
||||
size_t size; /**< The number of bits in the bitmap */
|
||||
size_t array_size; /**< The actual array size that maintains the bitmap */
|
||||
};
|
||||
|
||||
typedef struct lam_bitmap_t lam_bitmap_t;
|
||||
|
||||
#if defined(c_plusplus) || defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
/**
|
||||
* Initializes the bitmap and sets its size. This must be called
|
||||
* before the bitmap can be actually used
|
||||
*
|
||||
* @param bitmap The input bitmap (IN)
|
||||
* @param size The initial size of the bitmap in terms of bits (IN)
|
||||
* @return LAM error code or success
|
||||
*
|
||||
*/
|
||||
int lam_bitmap_init (lam_bitmap_t *bm, size_t size);
|
||||
|
||||
|
||||
/**
|
||||
* Set a bit of the bitmap. If the bit asked for is beyond the current
|
||||
* size of the bitmap, then the bitmap is extended to accomodate the
|
||||
* bit
|
||||
*
|
||||
* @param bitmap The input bitmap (IN)
|
||||
* @param bit The bit which is to be set (IN)
|
||||
* @return LAM error code or success
|
||||
*
|
||||
*/
|
||||
int lam_bitmap_set_bit(lam_bitmap_t *bm, int bit);
|
||||
|
||||
|
||||
/**
|
||||
* Clear/unset a bit of the bitmap. If the bit is beyond the current
|
||||
* size of the bitmap, an error is returned
|
||||
*
|
||||
* @param bitmap The input bitmap (IN)
|
||||
* @param bit The bit which is to be cleared (IN)
|
||||
* @return LAM error code if the bit is out of range, else success
|
||||
*
|
||||
*/
|
||||
int lam_bitmap_clear_bit(lam_bitmap_t *bm, int bit);
|
||||
|
||||
|
||||
/**
|
||||
* Find out if a bit is set in the bitmap
|
||||
*
|
||||
* @param bitmap The input bitmap (IN)
|
||||
* @param bit The bit which is to be checked (IN)
|
||||
* @return LAM error code if the bit is out of range
|
||||
* 1 if the bit is set
|
||||
* 0 if the bit is not set
|
||||
*
|
||||
*/
|
||||
int lam_bitmap_is_set_bit(lam_bitmap_t *bm, int bit);
|
||||
|
||||
|
||||
/**
|
||||
* Find the first clear bit in the bitmap and set it
|
||||
*
|
||||
* @param bitmap The input bitmap (IN)
|
||||
* @return bit number The bit number of the first unset bit
|
||||
*/
|
||||
int lam_bitmap_find_and_set_first_unset_bit(lam_bitmap_t *bm);
|
||||
|
||||
|
||||
/**
|
||||
* Clear all bits in the bitmap
|
||||
*
|
||||
* @param bitmap The input bitmap (IN)
|
||||
*
|
||||
*/
|
||||
void lam_bitmap_clear_all_bits(lam_bitmap_t *bm);
|
||||
|
||||
|
||||
/**
|
||||
* Set all bits in the bitmap
|
||||
* @param bitmap The input bitmap (IN)
|
||||
*
|
||||
*/
|
||||
void lam_bitmap_set_all_bits(lam_bitmap_t *bm);
|
||||
|
||||
|
||||
/**
|
||||
* Gives the current size (number of bits) in the bitmap
|
||||
*
|
||||
* @param bitmap The input bitmap (IN)
|
||||
*
|
||||
*/
|
||||
size_t lam_bitmap_size (lam_bitmap_t *bm);
|
||||
|
||||
#if defined(c_plusplus) || defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
Загрузка…
Ссылка в новой задаче
Block a user