/******************************************************************************************* Copyright (C) 2004,2005,2006,2007,2008 (Nuno A. Fonseca) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. Last rev: $Id: range_list.h,v 1.1 2008-03-26 23:05:22 nunofonseca Exp $ **************************************************************************/ /* Leaf Each leaf uses 16 bits ( each bit represents one number ) */ #define NUM unsigned long /* Node Each node (non leaf) uses 8 bits. - 8 bits are used to represent the state of the 4 subtrees ( subranges ). - 2 bits are used to represent the state for each subtreee States of a subtree: 00 (0) - range not in interval 11 (3)- all range in interval 10 (2)- range parcially in interval An extra byte is used to keep the number of nodes in the subtrees. */ struct s_node { //short quadrant; unsigned short int quadrant_1: 2; // unsigned short int quadrant_2: 2; unsigned short int quadrant_3: 2; unsigned short int quadrant_4: 2; unsigned short int num_subnodes: 8; }; typedef enum { R_TOTALLY_IN_INTERVAL=3, R_PARCIALLY_IN_INTERVAL=2, R_NOT_IN_INTERVAL=0, R_IGNORE=1} QUADRANT_STATUS; #define BRANCH_FACTOR 4 /* factor of division of the range */ #define LEAF_SIZE 16 /* how many numbers are represented by a leaf */ #define NODE_SIZE sizeof(RL_Node) #define NODE(tree,idx) (RL_Node*)&tree->root[idx] #define ROOT(tree) 0 #define IS_ROOT(tree,interval) (tree->range_max<=interval) #define ROOT_INTERVAL(tree) (tree->root_i*BRANCH_FACTOR) #define MIN(a,b) ((aLEAF_SIZE)?1:0) #define REALLOC_MEM(tree) (tree->mem_alloc < (tree->size+1)*NODE_SIZE) #define MEM_SIZE(tree) (tree->size+2)*NODE_SIZE #define TREE_SIZE(tree) tree->mem_alloc+sizeof(RL_Tree) typedef union { struct s_node i_node; unsigned short int leaf; } RL_Node; /* A node is a internal node (inode) or a leaf depending on their depth in the tree */ /* Range_List Contains the root node, max range size, */ struct rl_struct { RL_Node* root; NUM size; // number of nodes NUM mem_alloc; // memory allocated for *root NUM range_max; // maximum value of the interval NUM root_i; // root interval }; typedef struct rl_struct RL_Tree; /* Buffer */ struct s_buffer { RL_Node* root_node; unsigned long size; // memory (in bytes) allocated for root_node }; typedef struct s_buffer RL_Buffer; //---------------------------------------------------------------- // Bits operations #define BITMAP_empty(b) ((b) == 0) #define BITMAP_member(b,n) (((b) & (1<<(n))) != 0) #define BITMAP_alone(b,n) ((b) == (1<<(n))) #define BITMAP_subset(b1,b2) (((b1) & (b2)) == b2) #define BITMAP_same(b1,b2) ((b1) == (b2)) #define BITMAP_on_all(b) ((b) = 255) #define BITMAP_clear(b) ((b) = 0) #define BITMAP_and(b1,b2) ((b1) &= (b2)) #define BITMAP_minus(b1,b2) ((b1) &= ~(b2)) #define BITMAP_insert(b,n) ((b) |= (1<<(n))) #define BITMAP_delete(b,n) ((b) &= (~(1<<(n)))) #define BITMAP_copy(b1,b2) ((b1) = (b2)) #define BITMAP_intersection(b1,b2,b3) ((b1) = ((b2) & (b3))) #define BITMAP_difference(b1,b2,b3) ((b1) = ((b2) & (~(b3)))) # //---------------------------------------------------------------- typedef enum { TRUE=1, FALSE=0} BOOLEAN; typedef enum { IN=1, OUT=0} STATUS; // #define BUFFER_SIZE 1000 /* ********************************************************************************** */ /* API */ RL_Tree* new_rl(NUM max_size); RL_Tree* copy_rl(RL_Tree *tree); void free_rl(RL_Tree* range); void rl_all(RL_Tree* tree,STATUS status); void display_tree(RL_Tree *tree); RL_Tree* set_in_rl(RL_Tree* tree,NUM number,STATUS status); BOOLEAN in_rl(RL_Tree* range,NUM number); BOOLEAN freeze_rl(RL_Tree* tree); /* write operations on the range are finishe */ RL_Tree* intersect_rl(RL_Tree* range1,RL_Tree* range2); NUM rl_next_in_bigger(RL_Tree *tree,NUM min); /* Returns next number in tree bigger than min */ #define IS_FREEZED(tree) (tree->mem_alloc!=0)