TSP: The Transport Sample Protocol



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

gdisp_pointArray.c

00001 /* GTK - The GIMP Toolkit
00002  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Library General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Library General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Library General Public
00015  * License along with this library; if not, write to the
00016  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017  * Boston, MA 02111-1307, USA.
00018  */
00019 
00020 #include <stdio.h>
00021 #include <glib.h>
00022 #include "gdisp_pointArray.h"
00023 
00024 /*
00025  * Sets the fields of the structure to default values,
00026  * And allocate a sample buffer .
00027  */
00028 DoublePointArray_T*
00029 dparray_newSampleArray (guint maxSamples)
00030 {
00031 
00032   DoublePointArray_T *pArray = (DoublePointArray_T*)NULL;
00033 
00034   pArray = (DoublePointArray_T*)g_malloc0(sizeof(DoublePointArray_T));
00035 
00036   pArray->nbSamples  = 0;
00037   pArray->current    = 0;
00038   pArray->first      = 0;
00039   pArray->marker     = 0;
00040   pArray->maxSamples = maxSamples;
00041   pArray->samples    = (DoublePoint_T*)
00042                        g_malloc0(maxSamples * sizeof(DoublePoint_T));
00043 
00044   return pArray;
00045 
00046 }
00047 
00048 
00049 /*
00050  * Free memory that has been allocated to this point array.
00051  */
00052 void
00053 dparray_freeSampleArray (DoublePointArray_T* pArray)
00054 {
00055 
00056   g_free (pArray->samples);
00057   g_free (pArray);
00058 
00059 }
00060 
00061 
00062 /*
00063  * Add an array sample element.
00064  */
00065 void
00066 dparray_addSample (DoublePointArray_T *pArray,
00067                    DoublePoint_T      *point)
00068 {
00069   pArray->samples[pArray->current] = *point;
00070   pArray->current = (pArray->current + 1 ) % pArray->maxSamples;
00071 
00072   /*
00073    * Check end of ring buffer.
00074    */
00075   if (pArray->nbSamples != pArray->maxSamples) {
00076 
00077     pArray->nbSamples++;      
00078     pArray->first = 0;
00079 
00080   }
00081   else {
00082 
00083     pArray->first = pArray->current;
00084 
00085   }
00086 
00087 }
00088 
00089 
00090 /* 
00091  * Accessors on data.
00092  */
00093 guint
00094 dparray_getFirstIndex (DoublePointArray_T *pArray)
00095 {
00096 
00097   return pArray->first;
00098 
00099 }
00100 
00101 guint
00102 dparray_getCurrentIndex (DoublePointArray_T *pArray)
00103 {
00104 
00105   return pArray->current;
00106 
00107 }
00108 
00109 guint
00110 dparray_getNbSamples (DoublePointArray_T *pArray)
00111 {
00112 
00113   return pArray->nbSamples;
00114 
00115 }
00116 
00117 
00118 /* 
00119  * Set/Get on marker, used by upper level to remember a specific position.
00120  */
00121 guint
00122 dparray_getMarkerIndex (DoublePointArray_T *pArray)
00123 {
00124 
00125   return pArray->marker;
00126 
00127 }
00128 
00129 void
00130 dparray_setMarkerIndex (DoublePointArray_T *pArray,
00131                         guint               index)
00132 {
00133 
00134   pArray->marker = index % pArray->maxSamples;
00135 
00136 }
00137 
00138 DoublePoint_T
00139 dparray_getSample (DoublePointArray_T *pArray,
00140                    int                 index)
00141 {
00142 
00143   if (index >= 0) {
00144 
00145     return pArray->samples[ index % pArray->nbSamples ]; 
00146 
00147   }
00148   else {
00149 
00150     return pArray->samples[0]; 
00151 
00152   }
00153 
00154 }
00155 
00156 DoublePoint_T*
00157 dparray_getSamplePtr (DoublePointArray_T *pArray,
00158                       int                 index)
00159 {
00160 
00161   if (index >= 0) {
00162 
00163     return &pArray->samples[ index % pArray->nbSamples ]; 
00164 
00165   }
00166   else {
00167 
00168     return &pArray->samples[0]; 
00169 
00170   }
00171 
00172 }
00173 
00174 
00175 /*
00176  * Get the samples available from a specific position
00177  * in this tricky circular array.
00178  */
00179 guint 
00180 dparray_getLeftSamplesFromPos (DoublePointArray_T *pArray,
00181                                guint               index)
00182 {
00183 
00184   if (index < pArray->current) {
00185 
00186     return pArray->current - index;
00187 
00188   }
00189   if (index > pArray->current) {
00190 
00191     return pArray->current + pArray->maxSamples - index;
00192 
00193   }
00194 
00195   return pArray->nbSamples;
00196 
00197 }
00198 
00199 
00200 /*
00201  * Debug : Dump the values of the pointer.
00202  */
00203 void
00204 dparray_printFields (DoublePointArray_T *pArray)
00205 {
00206 
00207   printf ("Structure pArray : 0x%X \n", (guint)pArray         );
00208   printf ("\t ->samples     : 0x%X \n", (guint)pArray->samples);
00209   printf ("\t ->nbSamples   : %d   \n", pArray->nbSamples     );
00210   printf ("\t ->maxSamples  : %d   \n", pArray->maxSamples    );
00211   printf ("\t ->current     : %d   \n", pArray->current       );
00212   printf ("\t ->first       : %d   \n", pArray->first         );
00213   printf ("\t ->marker      : %d   \n", pArray->marker        );
00214 
00215 }
Framework Home Page.

Beware !! TSP wave is coming...