Main Page | Modules | Data Structures | File List | Data Fields | Globals | Related Pages

queue.h

Go to the documentation of this file.
00001 #ifndef DEF_QUEUE 00002 #define DEF_QUEUE 00003 00004 /* ---------------------------------------------------------------------------- 00005 @COPYRIGHT : 00006 Copyright 1993,1994,1995 David MacDonald, 00007 McConnell Brain Imaging Centre, 00008 Montreal Neurological Institute, McGill University. 00009 Permission to use, copy, modify, and distribute this 00010 software and its documentation for any purpose and without 00011 fee is hereby granted, provided that the above copyright 00012 notice appear in all copies. The author and McGill University 00013 make no representations about the suitability of this 00014 software for any purpose. It is provided "as is" without 00015 express or implied warranty. 00016 ---------------------------------------------------------------------------- */ 00017 00018 /* ----------------------------- MNI Header ----------------------------------- 00019 @NAME : queue.h 00020 @INPUT : 00021 @OUTPUT : 00022 @RETURNS : 00023 @DESCRIPTION: Macros for maintaining queues of any element type. 00024 @METHOD : 00025 @GLOBALS : 00026 @CALLS : 00027 @CREATED : David MacDonald 00028 @MODIFIED : 00029 ---------------------------------------------------------------------------- */ 00030 00031 00032 #include <volume_io/arrays.h> 00033 00034 #define SHIFT_QUEUE_AT 100 00035 #define SHIFT_QUEUE_RATIO 1 00036 00037 /* ----------------------------- MNI Header ----------------------------------- 00038 @NAME : QUEUE_STRUCT 00039 @INPUT : type 00040 @OUTPUT : 00041 @RETURNS : 00042 @DESCRIPTION: Macro to define a queue of elements of type 'type'. 00043 @METHOD : 00044 @GLOBALS : 00045 @CALLS : 00046 @CREATED : David MacDonald 00047 @MODIFIED : 00048 ---------------------------------------------------------------------------- */ 00049 00050 #define QUEUE_STRUCT( type ) \ 00051 struct \ 00052 { \ 00053 int head, tail; \ 00054 int n_queue_alloced; \ 00055 type *queue; \ 00056 } 00057 00058 /* ----------------------------- MNI Header ----------------------------------- 00059 @NAME : INITIALIZE_QUEUE 00060 @INPUT : q 00061 @OUTPUT : 00062 @RETURNS : 00063 @DESCRIPTION: Macro to initializes queue to empty. 00064 @METHOD : 00065 @GLOBALS : 00066 @CALLS : 00067 @CREATED : David MacDonald 00068 @MODIFIED : 00069 ---------------------------------------------------------------------------- */ 00070 00071 #define INITIALIZE_QUEUE( q ) \ 00072 { \ 00073 (q).head = 0; \ 00074 (q).tail = 0; \ 00075 (q).n_queue_alloced = 0; \ 00076 (q).queue = 0; \ 00077 } 00078 00079 /* ----------------------------- MNI Header ----------------------------------- 00080 @NAME : INSERT_IN_QUEUE 00081 @INPUT : q 00082 : entry - element to insert 00083 @OUTPUT : 00084 @RETURNS : 00085 @DESCRIPTION: Macro to insert an entry in the queue. 00086 @METHOD : 00087 @GLOBALS : 00088 @CALLS : 00089 @CREATED : David MacDonald 00090 @MODIFIED : 00091 ---------------------------------------------------------------------------- */ 00092 00093 #define INSERT_IN_QUEUE( q, entry ) \ 00094 { \ 00095 ADD_ELEMENT_TO_ARRAY_WITH_SIZE( (q).queue, (q).n_queue_alloced, \ 00096 (q).head, entry, DEFAULT_CHUNK_SIZE ) \ 00097 } 00098 00099 /* ----------------------------- MNI Header ----------------------------------- 00100 @NAME : NUMBER_IN_QUEUE 00101 @INPUT : q 00102 @OUTPUT : 00103 @RETURNS : 00104 @DESCRIPTION: Returns the number of entries in the queue. 00105 @METHOD : 00106 @GLOBALS : 00107 @CALLS : 00108 @CREATED : David MacDonald 00109 @MODIFIED : 00110 ---------------------------------------------------------------------------- */ 00111 00112 #define NUMBER_IN_QUEUE( q ) ((q).head - (q).tail) 00113 00114 /* ----------------------------- MNI Header ----------------------------------- 00115 @NAME : IS_QUEUE_EMPTY 00116 @INPUT : 00117 @OUTPUT : 00118 @RETURNS : TRUE or FALSE 00119 @DESCRIPTION: Returns TRUE if the queue is empty 00120 @METHOD : 00121 @GLOBALS : 00122 @CALLS : 00123 @CREATED : David MacDonald 00124 @MODIFIED : 00125 ---------------------------------------------------------------------------- */ 00126 00127 #define IS_QUEUE_EMPTY( q ) ((q).tail >= (q).head) 00128 00129 /* ----------------------------- MNI Header ----------------------------------- 00130 @NAME : REMOVE_FROM_QUEUE 00131 @INPUT : q 00132 @OUTPUT : entry 00133 @RETURNS : 00134 @DESCRIPTION: Removes the first entry from the queue, and stores it in 'entry'. 00135 : Rather than shift the queue down every time, it only shifts 00136 : every once in a while, causing a little wasted space at the 00137 : beginning of the array. 00138 @METHOD : 00139 @GLOBALS : 00140 @CALLS : 00141 @CREATED : David MacDonald 00142 @MODIFIED : 00143 ---------------------------------------------------------------------------- */ 00144 00145 #define REMOVE_FROM_QUEUE( q, entry ) \ 00146 { \ 00147 if( !IS_QUEUE_EMPTY(q) ) \ 00148 { \ 00149 entry = (q).queue[(q).tail]; \ 00150 ++(q).tail; \ 00151 \ 00152 if( (q).tail == (q).head ) \ 00153 { \ 00154 (q).head = 0; \ 00155 (q).tail = 0; \ 00156 } \ 00157 else if( (q).tail > SHIFT_QUEUE_AT && \ 00158 (q).tail/((q).head - (q).tail) > SHIFT_QUEUE_RATIO )\ 00159 { \ 00160 int _i_; \ 00161 for_less( _i_, (q).tail, (q).head ) \ 00162 { \ 00163 (q).queue[_i_-(q).tail] = (q).queue[_i_]; \ 00164 } \ 00165 (q).head -= (q).tail; \ 00166 (q).tail = 0; \ 00167 } \ 00168 } \ 00169 } 00170 00171 /* ----------------------------- MNI Header ----------------------------------- 00172 @NAME : DELETE_QUEUE 00173 @INPUT : q 00174 @OUTPUT : 00175 @RETURNS : 00176 @DESCRIPTION: Deletes the queue. 00177 @METHOD : 00178 @GLOBALS : 00179 @CALLS : 00180 @CREATED : David MacDonald 00181 @MODIFIED : 00182 ---------------------------------------------------------------------------- */ 00183 00184 #define DELETE_QUEUE( q ) \ 00185 { \ 00186 if( (q).n_queue_alloced > 0 ) \ 00187 { \ 00188 FREE( (q).queue ); \ 00189 } \ 00190 } 00191 00192 00193 #endif

Generated on Wed Jul 28 09:10:57 2004 for BICPL by doxygen 1.3.7