00001 /*************************************************************************** 00002 * @file gnn_output.c 00003 * @brief Output Implementation. 00004 * 00005 * @date : 21-09-03 20:34 00006 * @author : Pedro Ortega C. <peortega@dcc.uchile.cl> 00007 * Copyright 2003 Pedro Ortega C. 00008 ****************************************************************************/ 00009 /* 00010 * This program is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. 00014 * 00015 * This program is distributed in the hope that it will be useful, 00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 * GNU Library General Public License for more details. 00019 * 00020 * You should have received a copy of the GNU General Public License 00021 * along with this program; if not, write to the Free Software 00022 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00023 */ 00024 00025 00026 00027 /** 00028 * @defgroup gnn_output_doc gnn_output : Writing sets of vectors. 00029 * @ingroup libgnn_dataset 00030 * 00031 * The \ref gnn_output type defines a common interface for vector sets 00032 * output handlers. 00033 * 00034 * \ref gnn_output datatypes are special objects that write vector samples 00035 * to output devices. The raw \ref gnn_output structure defines a common 00036 * interface that should be implemented by extensions of this type. Typical 00037 * implementations can write vectors to files, memory, ports, etc. 00038 * 00039 * There are two types of output devices: stream and random access. Stream 00040 * devices do print to an output stream without carying about the sample 00041 * and set size. In contrast, random access devices have a defined sample 00042 * and set size. 00043 */ 00044 00045 00046 00047 /******************************************/ 00048 /* Include Files */ 00049 /******************************************/ 00050 00051 #include "gnn_output.h" 00052 #include "gnn_utilities.h" 00053 00054 00055 /******************************************/ 00056 /* Static Declaration */ 00057 /******************************************/ 00058 00059 static int 00060 gnn_output_default_reset (gnn_output *set); 00061 00062 void 00063 gnn_output_default_destroy (gnn_output *set); 00064 00065 00066 00067 /******************************************/ 00068 /* Static Implementation */ 00069 /******************************************/ 00070 00071 /** 00072 * @brief Default "reset" function for a ouput writer. 00073 * @ingroup gnn_output_doc 00074 * 00075 * This function is the default "reset" function for an output set. 00076 * It does nothing. 00077 * 00078 * @param set A pointer to a \ref gnn_output. 00079 * @return 0 if succeeded. 00080 */ 00081 static int 00082 gnn_output_default_reset (gnn_output *set) 00083 { 00084 assert (set != NULL); 00085 00086 return 0; 00087 } 00088 00089 00090 00091 /** 00092 * @brief Default "destroy" function for an output. 00093 * @ingroup gnn_output_doc 00094 * 00095 * This function is the default "destroy" function for an output. It assumes 00096 * that there isn't any additional data for the specific dataset type, so 00097 * it actually just returns. 00098 * 00099 * @param set A pointer to a \ref gnn_output. 00100 */ 00101 void 00102 gnn_output_default_destroy (gnn_output *set) 00103 { 00104 return; 00105 } 00106 00107 00108 00109 /******************************************/ 00110 /* Public Interface */ 00111 /******************************************/ 00112 00113 /** 00114 * @brief Initializes a \ref gnn_output. 00115 * @ingroup gnn_output_doc 00116 * 00117 * This function initializes a given vector writer, setting its properties 00118 * and installing its functions. 00119 * 00120 * If the "reset", or "destroy" functions aren't provided, then the default 00121 * functions \ref gnn_output_default_reset and \ref gnn_output_default_destroy 00122 * are installed respectively. The "put" function is mandatory, and should 00123 * always be provided. 00124 * 00125 * As an example, suppose that you have made your own extension to the 00126 * \ref gnn_output datatype, one that writes to a random access file, which you 00127 * called "my_writer". Also, suppose you have already coded the 00128 * appropiate "reset", "put" and "destroy" functions for your special 00129 * writer. Then, 00130 * \code 00131 my_writer *myoutput; // a pointer to the output writer to be created 00132 gnn_output *set; // a pointer to the same structure, but viewed as a 00133 // gnn_output 00134 00135 // allocate memory for the output writer 00136 myoutput = (my_writer *) malloc (sizeof (my_writer)); 00137 00138 // view as a gnn_output 00139 set = (gnn_output *) myoutput; 00140 00141 // initialize the output (you know there are only 100 samples) 00142 gnn_output_init (set, gnnOutputRA, 100, 5, serial_writer_reset, 00143 serial_writer_get, 00144 serial_writer_destroy); 00145 * \endcode 00146 * would initialize your output writer for 100 patterns, whose sample vectors 00147 * are of size 5. 00148 * 00149 * Please refer also to the alternative functions \ref gnn_output_stream_init 00150 * and \ref gnn_output_random_access_init. 00151 * 00152 * @param set A pointer to a \ref gnn_output. 00153 * @param mode Can be gnnOutputStream or gnnOutputRA. 00154 * @param size The number of samples that it contains. 00155 * @param m The size of the output samples. 00156 * @param reset A pointer to the output's "reset" function. 00157 * @param put A pointer to the output's "put" function. 00158 * @param destroy A pointer to the output's "destroy" function. 00159 * @return 0 if succeeded. 00160 */ 00161 int 00162 gnn_output_init (gnn_output *set, 00163 gnn_output_type mode, 00164 size_t size, 00165 size_t m, 00166 gnn_output_reset_type reset, 00167 gnn_output_put_type put, 00168 gnn_output_destroy_type destroy) 00169 { 00170 assert (set != NULL); 00171 00172 /* check sizes */ 00173 if (mode == gnnOutputRA && size < 1) 00174 { 00175 GSL_ERROR ("random access output sets should handle a stricly " 00176 "positive number of samples", GSL_EINVAL); 00177 } 00178 if (mode == gnnOutputRA && m < 1) 00179 { 00180 GSL_ERROR ("random access output sets should have samples of stricly " 00181 "positive number of vector elements", GSL_EINVAL); 00182 } 00183 00184 /* check get function */ 00185 if (put == NULL) 00186 { 00187 GSL_ERROR ("the \"put\" function is mandatory and should be provided", 00188 GSL_EINVAL); 00189 } 00190 00191 /* set fields */ 00192 set->mode = mode; 00193 set->size = size; 00194 set->m = m; 00195 set->reset = gnn_output_default_reset; 00196 set->put = put; 00197 set->destroy = gnn_output_default_destroy; 00198 00199 /* install functions */ 00200 if (reset != NULL) 00201 set->reset = reset; 00202 if (destroy != NULL) 00203 set->destroy = destroy; 00204 00205 return 0; 00206 } 00207 00208 /** 00209 * @brief Initializes a \ref gnn_output stream device. 00210 * @ingroup gnn_output_doc 00211 * 00212 * This function initializes a given stream vector writer, setting its 00213 * properties and installing its functions. Please refer to 00214 * \ref gnn_output_init for detailed documentation. 00215 * 00216 * @param set A pointer to a \ref gnn_output. 00217 * @param reset A pointer to the output's "reset" function. 00218 * @param put A pointer to the output's "put" function. 00219 * @param destroy A pointer to the output's "destroy" function. 00220 * @return 0 if succeeded. 00221 */ 00222 int 00223 gnn_output_stream_init (gnn_output *set, 00224 gnn_output_reset_type reset, 00225 gnn_output_put_type put, 00226 gnn_output_destroy_type destroy) 00227 { 00228 return gnn_output_init (set, gnnOutputStream, 0, 0, reset, put, destroy); 00229 } 00230 00231 /** 00232 * @brief Initializes a \ref gnn_output random access device. 00233 * @ingroup gnn_output_doc 00234 * 00235 * This function initializes a given random access vector writer, setting its 00236 * properties and installing its functions. Please refer to 00237 * \ref gnn_output_init for detailed documentation. 00238 * 00239 * @param set A pointer to a \ref gnn_output. 00240 * @param size The number of samples that it can contain. 00241 * @param m The size of the output samples. 00242 * @param reset A pointer to the output's "reset" function. 00243 * @param put A pointer to the output's "put" function. 00244 * @param destroy A pointer to the output's "destroy" function. 00245 * @return 0 if succeeded. 00246 */ 00247 int 00248 gnn_output_random_access_init (gnn_output *set, 00249 size_t size, 00250 size_t m, 00251 gnn_output_reset_type reset, 00252 gnn_output_put_type put, 00253 gnn_output_destroy_type destroy) 00254 { 00255 return gnn_output_init (set, gnnOutputRA, size, m, reset, put, destroy); 00256 } 00257 00258 /** 00259 * @brief Destroy a \ref gnn_output. 00260 * @ingroup gnn_output_doc 00261 * 00262 * This function destroys the output reader. 00263 * 00264 * @param set A pointer to a \ref gnn_output. 00265 */ 00266 void 00267 gnn_output_destroy (gnn_output *set) 00268 { 00269 assert (set != NULL); 00270 00271 set->destroy (set); 00272 free (set); 00273 } 00274 00275 /** 00276 * @brief Reset a output set. 00277 * @ingroup gnn_output_doc 00278 * 00279 * This function resets the output handler. 00280 * 00281 * @param set A pointer to a \ref gnn_output. 00282 * @return 0 if succeeded. 00283 */ 00284 int 00285 gnn_output_reset (gnn_output *set) 00286 { 00287 assert (set != NULL); 00288 00289 set->reset (set); 00290 } 00291 00292 /** 00293 * @brief Puts the k-th sample. 00294 * @ingroup gnn_output_doc 00295 * 00296 * This function writes the given vector at the k-th position of the output 00297 * device. 00298 * 00299 * @param set A pointer to a \ref gnn_input. 00300 * @param k The index of the sample to be retrieved. 00301 * @param v A pointer to the vector that should be outputted. 00302 * @return Returns 0 if succeeded. 00303 */ 00304 int 00305 gnn_output_put (gnn_output *set, size_t k, const gsl_vector *v) 00306 { 00307 assert (set != NULL); 00308 00309 return set->put (set, k, v); 00310 } 00311 00312 /** 00313 * @brief Gets the number of samples in the output. 00314 * @ingroup gnn_output_doc 00315 * 00316 * This function returns the output set's size. This number normally identifies 00317 * the number of samples that the output device can handle simultaneously. 00318 * It corresponds also to the maximum index that can be given in a call 00319 * at \ref gnn_output_put. 00320 * 00321 * @param set A pointer to a \ref gnn_output. 00322 * @return Returns the size. 00323 */ 00324 size_t 00325 gnn_output_get_size (gnn_output *set) 00326 { 00327 assert (set != NULL); 00328 00329 return set->size; 00330 } 00331 00332 /** 00333 * @brief Gets the output sample size. 00334 * @ingroup gnn_output_doc 00335 * 00336 * This function returns the size of the output vectors that are written to 00337 * this output device. 00338 * 00339 * @param set A pointer to a \ref gnn_output. 00340 * @return Returns the size. 00341 */ 00342 size_t 00343 gnn_output_sample_get_size (gnn_output *set) 00344 { 00345 assert (set != NULL); 00346 00347 return set->m; 00348 } 00349 00350 /** 00351 * @brief Check if stream device. 00352 * @ingroup gnn_output_doc 00353 * 00354 * This function returns 1 if the device is a stream device. 00355 * 00356 * @param set A pointer to a \ref gnn_output. 00357 * @return Returns 1 if the output is a stream device or 0 if not. 00358 */ 00359 int 00360 gnn_output_is_stream (gnn_output *set) 00361 { 00362 assert (set != NULL); 00363 00364 if (set->mode == gnnOutputStream) 00365 return 1; 00366 else 00367 return 0; 00368 } 00369 00370 /** 00371 * @brief Check if random access device. 00372 * @ingroup gnn_output_doc 00373 * 00374 * This function returns 1 if the device is a random access output device. 00375 * 00376 * @param set A pointer to a \ref gnn_output. 00377 * @return Returns 1 if the output is a RA device or 0 if not. 00378 */ 00379 int 00380 gnn_output_is_random_access (gnn_output *set) 00381 { 00382 assert (set != NULL); 00383 00384 if (set->mode == gnnOutputRA) 00385 return 1; 00386 else 00387 return 0; 00388 } 00389 00390 00391
1.2.18