Main Page | Alphabetical List | Compound List | File List | Compound Members | File Members

tkarray.c File Reference

#include <assert.h>
#include <stddef.h>
#include <string.h>
#include "TKArray.h"
#include "ErrorLog.h"
#include "ram.h"

Go to the source code of this file.

Compounds

struct  geTKArray
struct  geTKArray_BinaryFileHeader

Defines

#define TK_MAX_ARRAY_LENGTH   (0x7FFFFFFF)
#define TK_ARRAYSIZE   (offsetof(geTKArray, Elements))
#define TK_ASSERT_VALID(A)   ((void)0)

Typedefs

typedef geTKArray geTKArray

Functions

geTKArray *GENESISCC geTKArray_Create (int ElementSize)
geTKArray *GENESISCC geTKArray_CreateEmpty (int ElementSize, int ElementCount)
geTKArray *GENESISCC geTKArray_CreateFromBinaryFile (geVFile *pFile)
geBoolean GENESISCC geTKArray_SamplesAreTimeLinear (const geTKArray *Array, geFloat Tolerance)
geBoolean GENESISCC geTKArray_WriteToBinaryFile (const geTKArray *Array, geVFile *pFile)
void GENESISCC geTKArray_Destroy (geTKArray **PA)
int GENESISCC geTKArray_BSearch (const geTKArray *A, geTKArray_TimeType Key)
geBoolean GENESISCC geTKArray_Insert (geTKArray **PtrA, geTKArray_TimeType Key, int *Index)
geBoolean GENESISCC geTKArray_DeleteElement (geTKArray **PtrA, int N)
void *GENESISCC geTKArray_Element (const geTKArray *A, int N)
geTKArray_TimeType GENESISCC geTKArray_ElementTime (const geTKArray *A, int N)
int GENESISCC geTKArray_NumElements (const geTKArray *A)
int GENESISCC geTKArray_ElementSize (const geTKArray *A)


Define Documentation

#define TK_ARRAYSIZE   (offsetof(geTKArray, Elements))
 

Definition at line 60 of file tkarray.c.

Referenced by geTKArray_Create(), geTKArray_CreateEmpty(), geTKArray_CreateFromBinaryFile(), geTKArray_DeleteElement(), geTKArray_Insert(), and geTKArray_WriteToBinaryFile().

#define TK_ASSERT_VALID A   )     ((void)0)
 

Definition at line 81 of file tkarray.c.

Referenced by geTKArray_BSearch(), geTKArray_Create(), geTKArray_CreateEmpty(), geTKArray_DeleteElement(), geTKArray_Destroy(), geTKArray_Element(), geTKArray_ElementSize(), geTKArray_ElementTime(), geTKArray_Insert(), and geTKArray_NumElements().

#define TK_MAX_ARRAY_LENGTH   (0x7FFFFFFF)
 

Definition at line 57 of file tkarray.c.

Referenced by geTKArray_Insert().


Typedef Documentation

typedef struct geTKArray geTKArray
 


Function Documentation

int GENESISCC geTKArray_BSearch const geTKArray A,
geTKArray_TimeType  Key
 

Definition at line 224 of file tkarray.c.

References A, GENESISCC, geTKArray_TimeType, and TK_ASSERT_VALID.

Referenced by gePath_GetKeyframeIndex(), gePath_SampleChannel(), geTKArray_Insert(), geTKEvents_Delete(), and geTKEvents_SetupIterator().

00231 {
00232         int low,hi,mid;
00233         int ElementSize;
00234         const char *Array;
00235         geTKArray_TimeType test;
00236 
00237         TK_ASSERT_VALID(A);
00238         
00239         low = 0;
00240         hi = A->NumElements - 1;
00241         Array = A->Elements;
00242         ElementSize = A->ElementSize;
00243         
00244         while ( low<=hi )
00245                 {
00246                         mid = (low+hi)/2;
00247                         test = *(geTKArray_TimeType *)(Array + mid*ElementSize);
00248                         if ( Key > test )
00249                                 {
00250                                         low = mid+1;
00251                                 }
00252                         else
00253                                 {
00254                                         if ( Key < test )
00255                                                 {
00256                                                         hi = mid-1;
00257                                                 }
00258                                         else
00259                                                 {
00260                                                         return mid;
00261                                                 }
00262                                 }
00263                 }
00264         return hi;
00265 }

geTKArray* GENESISCC geTKArray_Create int  ElementSize  ) 
 

Definition at line 86 of file tkarray.c.

References A, ERR_TKARRAY_CREATE, geErrorLog_Add, GENESISCC, geRam_Allocate, geTKArray_TimeType, NULL, TK_ARRAYSIZE, and TK_ASSERT_VALID.

Referenced by geQKFrame_LinearCreate(), geQKFrame_SlerpCreate(), geQKFrame_SquadCreate(), geTKEvents_Create(), geTKEvents_CreateFromFile(), geVKFrame_HermiteCreate(), and geVKFrame_LinearCreate().

00090 {
00091         geTKArray *A;
00092 
00093         // first item in each element must be the time key
00094         assert( ElementSize >= sizeof(geTKArray_TimeType) );
00095 
00096         A = geRam_Allocate(TK_ARRAYSIZE);
00097         if ( A == NULL)
00098         {
00099                 geErrorLog_Add(ERR_TKARRAY_CREATE, NULL);
00100                 return NULL;
00101         }
00102 
00103         A->ElementSize = ElementSize;
00104         A->NumElements = 0;
00105 
00106         TK_ASSERT_VALID(A);
00107 
00108         return A;       
00109 }

geTKArray* GENESISCC geTKArray_CreateEmpty int  ElementSize,
int  ElementCount
 

Definition at line 111 of file tkarray.c.

References A, geErrorLog_AddString, GENESISCC, geRam_Allocate, int32, NULL, TK_ARRAYSIZE, and TK_ASSERT_VALID.

Referenced by geQKFrame_CreateFromBinaryFile(), geQKFrame_CreateFromFile(), geVKFrame_CreateFromBinaryFile(), and geVKFrame_CreateFromFile().

00115 {
00116         geTKArray *A;
00117         int32 size = TK_ARRAYSIZE + ElementCount * ElementSize;
00118         A = (geTKArray*)geRam_Allocate(size);
00119         if( A == NULL )
00120         {
00121                 geErrorLog_AddString(-1,"Failure to allocate empty TKArray", NULL);
00122                 return NULL;
00123         }
00124         A->ElementSize = ElementSize;
00125         A->NumElements = ElementCount;
00126 
00127         TK_ASSERT_VALID(A);
00128 
00129         return A;       
00130 }

geTKArray* GENESISCC geTKArray_CreateFromBinaryFile geVFile pFile  ) 
 

Definition at line 132 of file tkarray.c.

References A, geTKArray_BinaryFileHeader::ElementSize, GE_FALSE, geErrorLog_AddString, GENESISCC, geRam_Allocate, geRam_Free, geVFile_Read(), int32, NULL, geTKArray_BinaryFileHeader::NumElements, and TK_ARRAYSIZE.

Referenced by geTKEvents_CreateFromFile().

00135 {
00136         int32 size;
00137         geTKArray* A;
00138         geTKArray_BinaryFileHeader Header;
00139 
00140         if (geVFile_Read(pFile, &Header, sizeof(geTKArray_BinaryFileHeader)) == GE_FALSE)
00141         {
00142                 geErrorLog_AddString(-1,"Failure to binary read TKArray header", NULL);
00143                 return NULL;
00144         }
00145 
00146         size = TK_ARRAYSIZE + Header.NumElements * Header.ElementSize;
00147         A = (geTKArray*)geRam_Allocate(size);
00148         if( A == NULL )
00149         {
00150                 geErrorLog_AddString(-1,"Failure to allocate TKArray during binary read", NULL);
00151                 return NULL;
00152         }
00153 
00154 
00155         if(geVFile_Read(pFile, A->Elements, size - sizeof(geTKArray_BinaryFileHeader)) == GE_FALSE)
00156                 {
00157                         geRam_Free(A);
00158                         geErrorLog_AddString(-1,"Failure to binary read TKArray body", NULL);
00159                         return NULL;
00160                 }
00161 
00162         A->NumElements = Header.NumElements;
00163         A->ElementSize = Header.ElementSize;
00164 
00165         return A;
00166 }

geBoolean GENESISCC geTKArray_DeleteElement geTKArray **  PtrA,
int  N
 

Definition at line 341 of file tkarray.c.

References A, GE_TRUE, geBoolean, GENESISCC, geRam_Realloc, NULL, TK_ARRAYSIZE, and TK_ASSERT_VALID.

Referenced by gePath_DeleteKeyframe(), gePath_InsertKeyframe(), geTKEvents_Delete(), and geTKEvents_Insert().

00347 {
00348         geTKArray *A;
00349         geTKArray *ChangedA;
00350         
00351         assert( PtrA != NULL);
00352         A = *PtrA;
00353         TK_ASSERT_VALID(A);
00354         assert(N >= 0);
00355         assert(N < A->NumElements);
00356         
00357         memmove( (A->Elements) + (N) * (A->ElementSize),  //dest
00358                          (A->Elements) + (N+1) * (A->ElementSize),  //src
00359                          ((A->NumElements) - (N+1))* (A->ElementSize) );
00360 
00361         A->NumElements--;
00362         ChangedA = (geTKArray *)geRam_Realloc(A, 
00363                                 TK_ARRAYSIZE + A->NumElements * A->ElementSize);
00364         if ( ChangedA != NULL ) 
00365         {       
00366                 // if realloc fails to shrink block. no real error.
00367                 A = ChangedA;
00368         }
00369 
00370         *PtrA = A;
00371 
00372         return GE_TRUE;
00373 }

void GENESISCC geTKArray_Destroy geTKArray **  PA  ) 
 

Definition at line 213 of file tkarray.c.

References GENESISCC, geRam_Free, NULL, and TK_ASSERT_VALID.

Referenced by gePath_CreateFromBinaryFile(), gePath_Destroy(), and geTKEvents_Destroy().

00215 {
00216         assert( PA  != NULL );
00217         TK_ASSERT_VALID(*PA);
00218 
00219         geRam_Free(*PA);
00220         *PA = NULL;
00221 }

void* GENESISCC geTKArray_Element const geTKArray A,
int  N
 

Definition at line 376 of file tkarray.c.

References A, GENESISCC, and TK_ASSERT_VALID.

Referenced by gePath_ReadChannel_F0_(), gePath_SampleChannel(), geQKFrame_CreateFromBinaryFile(), geQKFrame_CreateFromFile(), geQKFrame_Insert(), geQKFrame_Modify(), geQKFrame_PathIsHinged(), geQKFrame_Query(), geQKFrame_SlerpRecompute(), geQKFrame_SquadRecompute(), geQKFrame_WriteToBinaryFile(), geQKFrame_WriteToFile(), geTKEvents_CreateFromFile(), geTKEvents_Delete(), geTKEvents_GetNextEvent(), geTKEvents_Insert(), geTKEvents_WriteToFile(), geVKFrame_CreateFromBinaryFile(), geVKFrame_CreateFromFile(), geVKFrame_HermiteRecompute(), geVKFrame_Insert(), geVKFrame_Modify(), geVKFrame_Query(), geVKFrame_WriteToBinaryFile(), and geVKFrame_WriteToFile().

00378 {
00379         TK_ASSERT_VALID(A);
00380         assert(N >= 0);
00381         assert(N < A->NumElements);
00382 
00383         return (void *)( (A->Elements) + (N * (A->ElementSize)) );
00384 }

int GENESISCC geTKArray_ElementSize const geTKArray A  ) 
 

Definition at line 406 of file tkarray.c.

References A, GENESISCC, and TK_ASSERT_VALID.

Referenced by geQKFrame_Insert(), geQKFrame_Modify(), geQKFrame_Query(), geVKFrame_HermiteRecompute(), geVKFrame_Insert(), geVKFrame_Modify(), and geVKFrame_Query().

00408 {
00409         TK_ASSERT_VALID(A);
00410         return A->ElementSize;
00411 }

geTKArray_TimeType GENESISCC geTKArray_ElementTime const geTKArray A,
int  N
 

Definition at line 387 of file tkarray.c.

References A, GENESISCC, geTKArray_TimeType, and TK_ASSERT_VALID.

Referenced by gePath_GetKeyframeIndex(), gePath_GetTimeExtents(), gePath_Recompute(), gePath_SampleChannel(), geQKFrame_WriteToBinaryFile(), geQKFrame_WriteToFile(), geTKArray_SamplesAreTimeLinear(), geTKEvents_Delete(), geTKEvents_GetExtents(), geTKEvents_GetNextEvent(), geTKEvents_SetupIterator(), geVKFrame_WriteToBinaryFile(), and geVKFrame_WriteToFile().

00389 {
00390         TK_ASSERT_VALID(A);
00391         assert(N >= 0);
00392         assert(N < A->NumElements);
00393         
00394         return *(geTKArray_TimeType *)((A->Elements) + (N * (A->ElementSize)) );
00395 }

geBoolean GENESISCC geTKArray_Insert geTKArray **  PtrA,
geTKArray_TimeType  Key,
int *  Index
 

Definition at line 268 of file tkarray.c.

References A, ERR_TKARRAY_INSERT_ENOMEM, ERR_TKARRAY_INSERT_IDENTICAL, ERR_TKARRAY_TOO_BIG, GE_FALSE, GE_TKA_TIME_TOLERANCE, GE_TRUE, geBoolean, geErrorLog_Add, GENESISCC, geRam_Realloc, geTKArray_BSearch(), geTKArray_TimeType, NULL, TK_ARRAYSIZE, TK_ASSERT_VALID, and TK_MAX_ARRAY_LENGTH.

Referenced by gePath_ReadChannel_F0_(), geQKFrame_Insert(), geTKEvents_CreateFromFile(), geTKEvents_Insert(), and geVKFrame_Insert().

00279 {
00280         int n;
00281         geTKArray *ChangedA;
00282         geTKArray *A;
00283         geTKArray_TimeType Found;
00284 
00285         assert( PtrA );
00286         A = *PtrA;
00287         TK_ASSERT_VALID(A);
00288 
00289         n = geTKArray_BSearch(A,Key);
00290         // n is the element just prior to the location of the new element
00291 
00292         if(Index)
00293                 *Index = n+1;
00294 
00295         if (n >= 0)
00296         {
00297                 Found =  *(geTKArray_TimeType *)(A->Elements + (n * (A->ElementSize)) );
00298                 // Found <= Key  (within +-GE_TKA_TIME_TOLERANCE)
00299                 if (Found > Key - GE_TKA_TIME_TOLERANCE)
00300                 {       // if Found==Key, bail.  Can't have two identical keys.
00301                         geErrorLog_Add(ERR_TKARRAY_INSERT_IDENTICAL, NULL);
00302                         return GE_FALSE;
00303                 }
00304         }
00305 
00306         if (A->NumElements >= TK_MAX_ARRAY_LENGTH)
00307         {
00308                 geErrorLog_Add(ERR_TKARRAY_TOO_BIG, NULL);
00309                 return GE_FALSE;
00310         }
00311 
00312         ChangedA = (geTKArray *)geRam_Realloc(A, 
00313                                 TK_ARRAYSIZE + (A->NumElements + 1) * A->ElementSize);
00314 
00315         if ( ChangedA == NULL )
00316         {       
00317                 geErrorLog_Add(ERR_TKARRAY_INSERT_ENOMEM, NULL);
00318                 return GE_FALSE;
00319         }
00320         A = ChangedA;
00321 
00322         // advance n to new element's position
00323         n++;
00324         
00325         // move elements as necessary
00326         if(n < A->NumElements)
00327         {
00328                 memmove( A->Elements + (n + 1) * A->ElementSize,        // dest
00329                                  A->Elements + n * A->ElementSize,                      // src
00330                                  (A->NumElements - n) * A->ElementSize);        // count
00331         }
00332 
00333         *(geTKArray_TimeType *)((A->Elements) + ((n) * (A->ElementSize)) ) = Key;
00334         A->NumElements++;
00335         *PtrA = A;
00336 
00337         return GE_TRUE;
00338 }

int GENESISCC geTKArray_NumElements const geTKArray A  ) 
 

Definition at line 398 of file tkarray.c.

References A, GENESISCC, and TK_ASSERT_VALID.

Referenced by gePath_CreateCopy(), gePath_GetKeyframeCount(), gePath_GetTimeExtents(), gePath_Recompute(), gePath_SampleChannel(), gePath_WriteToBinaryFile(), gePath_WriteToFile(), geQKFrame_ComputeBlockSize(), geQKFrame_DetermineCompressionType(), geQKFrame_Modify(), geQKFrame_PathIsHinged(), geQKFrame_Query(), geQKFrame_SlerpRecompute(), geQKFrame_SquadRecompute(), geQKFrame_WriteToBinaryFile(), geQKFrame_WriteToFile(), geTKEvents_Delete(), geTKEvents_GetExtents(), geTKEvents_GetNextEvent(), geTKEvents_Insert(), geTKEvents_WriteToFile(), geVKFrame_ComputeBlockSize(), geVKFrame_HermiteRecompute(), geVKFrame_Modify(), geVKFrame_Query(), geVKFrame_WriteToBinaryFile(), and geVKFrame_WriteToFile().

00400 {
00401         TK_ASSERT_VALID(A);
00402         return A->NumElements;
00403 }

geBoolean GENESISCC geTKArray_SamplesAreTimeLinear const geTKArray Array,
geFloat  Tolerance
 

Definition at line 169 of file tkarray.c.

References GE_FALSE, GE_TRUE, geBoolean, GENESISCC, geTKArray_ElementTime(), geTKArray_TimeType, and geTKArray::NumElements.

Referenced by geQKFrame_DetermineCompressionType(), geVKFrame_WriteToBinaryFile(), and geVKFrame_WriteToFile().

00170 {
00171         int i;
00172 
00173         geTKArray_TimeType Delta,Nth,LastNth,NthDelta;
00174                         
00175         if (Array->NumElements < 2)
00176                 return GE_TRUE;
00177 
00178         LastNth = geTKArray_ElementTime(Array, 0);
00179         Nth     = geTKArray_ElementTime(Array, 1);
00180         Delta   =  Nth - LastNth;
00181         LastNth = Nth;
00182         
00183         for (i=2; i< Array->NumElements; i++)
00184                 {
00185                         Nth = geTKArray_ElementTime(Array, i);
00186                         NthDelta = (Nth-LastNth)-Delta;
00187                         if (NthDelta<0.0f) NthDelta = -NthDelta;
00188                         if (NthDelta>Tolerance)
00189                                 {
00190                                         return GE_FALSE;
00191                                 }
00192                         LastNth = Nth;
00193                 }
00194         return GE_TRUE;
00195 }

geBoolean GENESISCC geTKArray_WriteToBinaryFile const geTKArray Array,
geVFile pFile
 

Definition at line 197 of file tkarray.c.

References geTKArray::ElementSize, GE_FALSE, GE_TRUE, geBoolean, geErrorLog_AddString, GENESISCC, geVFile_Write(), NULL, geTKArray::NumElements, and TK_ARRAYSIZE.

Referenced by geTKEvents_WriteToBinaryFile().

00201 {
00202         int size;
00203         
00204         size = TK_ARRAYSIZE + Array->NumElements * Array->ElementSize;
00205         if(geVFile_Write(pFile, Array, size) == GE_FALSE)
00206         {
00207                 geErrorLog_AddString(-1,"Failure to write binary TKArray data", NULL);
00208                 return GE_FALSE;
00209         }
00210         return GE_TRUE;
00211 }


Generated on Tue Sep 30 12:38:14 2003 for GTestAndEngine by doxygen 1.3.2