00001 /*************************************************************************** 00002 * @file gnn_input.c 00003 * @brief Input Implementation. 00004 * 00005 * @date : 21-09-03 19:47 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_input_doc gnn_input : Reading and handling of sets of vectors. 00029 * @ingroup libgnn_dataset 00030 * 00031 * The \ref gnn_input type defines a common interface for vector sets 00032 * handlers. 00033 * 00034 * \ref gnn_input datatypes are special objects that read and provide access 00035 * to sets of vectors. The raw \ref gnn_input structure defines a common 00036 * interface that should be implemented by extensions of this type. Typical 00037 * implementations can read vectors from files, memory, ports, etc. 00038 */ 00039 00040 00041 00042 /******************************************/ 00043 /* Include Files */ 00044 /******************************************/ 00045 00046 #include "gnn_input.h" 00047 #include "gnn_utilities.h" 00048 00049 00050 /******************************************/ 00051 /* Static Declaration */ 00052 /******************************************/ 00053 00054 static int 00055 gnn_input_default_reset (gnn_input *set); 00056 00057 void 00058 gnn_input_default_destroy (gnn_input *set); 00059 00060 00061 00062 /******************************************/ 00063 /* Static Implementation */ 00064 /******************************************/ 00065 00066 /** 00067 * @brief Default "reset" function for a input set. 00068 * @ingroup gnn_input_doc 00069 * 00070 * This function is the default "reset" function for a input set. 00071 * Actuallly, it doesn't anything. 00072 * 00073 * @param set A pointer to a \ref gnn_input. 00074 * @return 0 if succeeded. 00075 */ 00076 static int 00077 gnn_input_default_reset (gnn_input *set) 00078 { 00079 assert (set != NULL); 00080 00081 return 0; 00082 } 00083 00084 00085 00086 /** 00087 * @brief Default "destroy" function for a dataset. 00088 * @ingroup gnn_input_doc 00089 * 00090 * This function is the default "destroy" function for an input set. It 00091 * assumes that there isn't any additional data for the specific dataset type, 00092 * so it actually just returns. 00093 * 00094 * @param set A pointer to a \ref gnn_input. 00095 */ 00096 void 00097 gnn_input_default_destroy (gnn_input *set) 00098 { 00099 return; 00100 } 00101 00102 00103 00104 /******************************************/ 00105 /* Public Interface */ 00106 /******************************************/ 00107 00108 /** 00109 * @brief Initializes a \ref gnn_input. 00110 * @ingroup gnn_input_doc 00111 * 00112 * This function initializes a given vector reader, setting its properties 00113 * and installing its functions. 00114 * 00115 * If the "reset", or "destroy" functions aren't provided, then the default 00116 * functions \ref gnn_input_default_reset and \ref gnn_input_default_destroy 00117 * are installed respectively. The "get" function is mandatory, and should 00118 * always be provided. 00119 * 00120 * As an example, suppose that you have made your own extension to the 00121 * \ref gnn_input datatype, one that reads from a serial port, which you 00122 * called "serial_reader". Also, suppose you have already coded the 00123 * appropiate "reset", "get" and "destroy" functions for your special 00124 * reader. Then, 00125 * \code 00126 serial_reader *myinput; // a pointer to the input reader to be created 00127 gnn_input *set; // a pointer to the same structure, but viewed as a 00128 // gnn_input 00129 00130 // allocate memory for the input reader 00131 myinput = (serial_reader *) malloc (sizeof (serial_reader)); 00132 00133 // view as a gnn_input 00134 set = (gnn_input *) myinput; 00135 00136 // initialize the input (you know there are only 100 samples) 00137 gnn_input_init (set, 100, 5, serial_reader_reset, 00138 serial_reader_get, 00139 serial_reader_destroy); 00140 * \endcode 00141 * would initialize your input reader for 100 patterns, whose sample vectors 00142 * are of size 5. 00143 * 00144 * @param set A pointer to a \ref gnn_input. 00145 * @param size The number of samples that it contains. 00146 * @param n The size of the samples. 00147 * @param reset A pointer to the input's "reset" function. 00148 * @param get A pointer to the input's "get" function. 00149 * @param destroy A pointer to the input's "destroy" function. 00150 * @return 0 if succeeded. 00151 */ 00152 int 00153 gnn_input_init (gnn_input *set, 00154 size_t size, 00155 size_t n, 00156 gnn_input_reset_type reset, 00157 gnn_input_get_type get, 00158 gnn_input_destroy_type destroy) 00159 { 00160 assert (set != NULL); 00161 00162 /* check sizes */ 00163 if (size < 1) 00164 { 00165 GSL_ERROR ("input sets should contain a stricly positive number of" 00166 "samples", GSL_EINVAL); 00167 } 00168 if (n < 1) 00169 { 00170 GSL_ERROR ("the samples should have a stricly positive number of" 00171 "components", GSL_EINVAL); 00172 } 00173 00174 /* check get function */ 00175 if (get == NULL) 00176 { 00177 GSL_ERROR ("the \"get\" function is mandatory and should be provided", 00178 GSL_EINVAL); 00179 } 00180 00181 /* set fields */ 00182 set->size = size; 00183 set->n = n; 00184 set->reset = gnn_input_default_reset; 00185 set->get = get; 00186 set->destroy = gnn_input_default_destroy; 00187 00188 /* install functions */ 00189 if (reset != NULL) 00190 set->reset = reset; 00191 if (destroy != NULL) 00192 set->destroy = destroy; 00193 00194 return 0; 00195 } 00196 00197 00198 /** 00199 * @brief Destroy a \ref gnn_input. 00200 * @ingroup gnn_input_doc 00201 * 00202 * This function destroys the input reader. 00203 * 00204 * @param set A pointer to a \ref gnn_input. 00205 */ 00206 void 00207 gnn_input_destroy (gnn_input *set) 00208 { 00209 assert (set != NULL); 00210 00211 set->destroy (set); 00212 free (set); 00213 } 00214 00215 /** 00216 * @brief Reset a input set. 00217 * @ingroup gnn_input_doc 00218 * 00219 * This function resets the input handler. 00220 * 00221 * @param set A pointer to a \ref gnn_input. 00222 * @return 0 if succeeded. 00223 */ 00224 int 00225 gnn_input_reset (gnn_input *set) 00226 { 00227 assert (set != NULL); 00228 00229 set->reset (set); 00230 } 00231 00232 /** 00233 * @brief Gets the k-th sample. 00234 * @ingroup gnn_input_doc 00235 * 00236 * This function returns a pointer to a buffer containing the k-th sample 00237 * vector. The content of the pointed vector will be valid until a next 00238 * function call on this input set. 00239 * 00240 * @param set A pointer to a \ref gnn_input. 00241 * @param k The index of the sample to be retrieved. 00242 * @return Returns a pointer to a buffer containing the sample vector, or 00243 * NULL else. 00244 */ 00245 const gsl_vector * 00246 gnn_input_get (gnn_input *set, 00247 size_t k) 00248 { 00249 assert (set != NULL); 00250 00251 return set->get (set, k); 00252 } 00253 00254 /** 00255 * @brief Gets the number of samples in the set. 00256 * @ingroup gnn_input_doc 00257 * 00258 * This function returns the set's size. 00259 * 00260 * @param set A pointer to a \ref gnn_input. 00261 * @return Returns the size. 00262 */ 00263 size_t 00264 gnn_input_get_size (gnn_input *set) 00265 { 00266 assert (set != NULL); 00267 00268 return set->size; 00269 } 00270 00271 /** 00272 * @brief Gets the sample's size. 00273 * @ingroup gnn_input_doc 00274 * 00275 * This function returns the size of the samples. 00276 * 00277 * @param set A pointer to a \ref gnn_input. 00278 * @return Returns the size. 00279 */ 00280 size_t 00281 gnn_input_sample_get_size (gnn_input *set) 00282 { 00283 assert (set != NULL); 00284 00285 return set->n; 00286 } 00287 00288 00289 00290
1.2.18