?? mp_book.c
字號:
/*..........................................................................*//* *//* L a s t W a v e P a c k a g e 'mp' 2.1 *//* *//* Copyright (C) 2000 Remi Gribonval, Emmanuel Bacry and Javier Abadia.*//* email : remi.gribonval@inria.fr *//* email : lastwave@cmap.polytechnique.fr *//* *//*..........................................................................*//* *//* This program is a free software, you can redistribute it and/or *//* modify it under the terms of the GNU General Public License as *//* published by the Free Software Foundation; either version 2 of the *//* License, or (at your option) any later version *//* *//* This program is distributed in the hope that it will be useful, *//* but WITHOUT ANY WARRANTY; without even the implied warranty of *//* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *//* GNU General Public License for more details. *//* *//* You should have received a copy of the GNU General Public License *//* along with this program (in a file named COPYRIGHT); *//* if not, write to the Free Software Foundation, Inc., *//* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *//* *//*..........................................................................*/#include "lastwave.h"#include "mp_book.h"#include "int_fsilist.h"/**********************************//* * BOOK VARIABLES *//**********************************/char *bookType = "&book";static char *defaultName="";/* * Answers to the different print messages */void ShortPrintBook(BOOK book){ Printf("<&book[%d];%p>\n",book->size,book);}char *ToStrBook(BOOK book, char flagShort){ static char str[30]; if (book->name == defaultName || book->name == NULL) sprintf(str,"<&book;%p>",book); else sprintf(str,"<&book;%s>",book->name); return(str);}void PrintInfoBook(BOOK book){ // TODO : finish that Printf("Book '%s'\n",book->name); Printf(" size %d (sizeAlloc %d)\n",book->size,book->sizeAlloc); Printf(" sampling rate: %.2f Hertz\n",1/book->dx); Printf(" signal size : %d\n",book->signalSize);}/* * NumExtraction * * Signals (0m, 1m, ,...) */static char *numdoc = "A book contains some signals that can be adressed using the syntax '<i>'. Among them, signal '0' is used by default for Matching Pursuit analysis.";static void *NumExtractBook(BOOK book,void **arg){ long n; char flagDot; /* doc */ if (book == NULL) return(numdoc); n = ARG_NE_GetN(arg); flagDot = ARG_NE_GetFlagDot(arg); // Case of the extraction of a signal if(flagDot==NO) { if (n < 0 || n >= NBookSignals) { SetErrorf("'%d' is not a valid signal name in a book",n); return(NULL); } ARG_NE_SetResValue(arg,book->theSignals[n]); return(signalType); } else { SetErrorf("No signal '.%d' in a book",n); return(NULL); }}static void *GetOptionsBookV(BOOK book, void **arg){ static char *opt[] = {NULL}; return(opt);}static void *GetExtractInfoBookV(BOOK book, void **arg){ char *field = ARG_EI_GetField(arg); unsigned long *options = ARG_EI_GetPOptions(arg); static ExtractInfo extractInfo; static char flagInit = YES; /* Init of the extraction info */ if (flagInit) { extractInfo.nSignals = 1; extractInfo.dx = 1; extractInfo.xmin = 0; extractInfo.flags = EIIntIndex | EIErrorBound; flagInit = NO; } if(field==NULL || !strcmp(field,"")) { if (book->size == 0) { SetErrorf("No extraction of molecules in an empty book"); return(NULL); } extractInfo.xmax = book->size-1; return(&extractInfo); } if(!strcmp(field,"sig")) { extractInfo.xmax = NBookSignals-1; return(&extractInfo); }}static char *sigdoc = "{[<n>]} {Gets the signal <n> of a book}";static char *moldoc = "{[<n>]} {Gets the molecule <n> of a book}"; static void *GetBookV(BOOK book, void **arg){ char *field = ARG_G_GetField(arg); FSIList *fsiList; SIGNAL sig; /* doc */ if (book == NULL) { if (field==NULL || !strcmp(field,"")) return(moldoc); if (!strcmp(field,"sig")) return(sigdoc); } fsiList = ARG_G_GetFsiList(arg); if(fsiList==NULL) { if (field==NULL||!strcmp(field,"")) SetErrorf("The syntax is <book>[<n>]"); if (!strcmp(field,"sig")) SetErrorf("The syntax is <book>.sig[<n>]"); return(NULL); } if (fsiList->nx != 1) { SetErrorf("Only a single index can be used"); return(NULL); } if(field==NULL || !strcmp(field,"")) { ARG_G_SetResValue(arg,GetBookMolecule(book,(int) FSI_FIRST(fsiList))); return(moleculeType); } if (!strcmp(field,"sig")) { ARG_G_SetResValue(arg,book->theSignals[(int) FSI_FIRST(fsiList)]); return(signalType); } }static void *SetBookV(BOOK book, void **arg){ char *field = ARG_S_GetField(arg); FSIList *fsiList; VALUE value = NULL; char *equal = NULL; MOLECULE molecule; VALUE *pValueRes = NULL; /* doc */ if (book == NULL) { if (field==NULL || !strcmp(field,"")) return(moldoc); if (!strcmp(field,"sig")) return(sigdoc); } fsiList = ARG_S_GetFsiList(arg); if (field!=NULL && !strcmp(field,"sig")) { ARG_S_SetFsiList(arg,NULL); return(SetSignalField(book->theSignals[(int) FSI_FIRST(fsiList)],arg)); } // No extraction is treated if(fsiList!=NULL) { SetErrorf("You must specify a field for the molecule"); return(NULL); } equal = ARG_S_GetEqual(arg); value = ARG_S_GetRightValue(arg); // Case book += molecule if(*equal=='+' && value!=NULL && GetTypeValue(value)==moleculeType) { molecule = (MOLECULE) value; // Case of an empty book : we have to set the TFContent first if(book->size==0) CopyFieldsTFContent(GetMoleculeAtom(molecule,0,0),book); AddMolecule2Book(book,CopyMolecule(molecule,NULL)); pValueRes = ARG_S_GetResPValue(arg); *pValueRes = (VALUE)book; return(bookType); } return(NULL);}BOOK TNewBook(void){ BOOK book = NewBook(); TempValue(book); return(book);}/* * Get the current book * (generate an error if there is none) */BOOK GetBookCur(void){ BOOK book; if(!ParseTypedValLevel_(levelCur,"objCur",NULL,(VALUE *)&book,bookType)) Errorf1(""); AddRefValue(book); TempValue(book); return(book);}/*******************************************//* * Basic data management for BOOK */ /*******************************************/static void InitBook(BOOK book){ InitTFContent(book); book->size = 0; book->sizeAlloc = 0; book->molecules = NULL;}BOOK NewBook(){ BOOK book; unsigned short i; #ifdef DEBUGALLOC DebugType = "Book";#endif book = (BOOK) Malloc(sizeof(Book)); InitValue(book,&tsBook); InitBook(book); book->name = defaultName; for (i=0;i<NBookSignals;i++) book->theSignals[i] = NewSignal(); return(book); }// Delete the arrays of molecules. The rest is kept.void DeleteBookMolecules(BOOK book){ unsigned long i; if(book == NULL) Errorf("DeleteBookMolecules : NULL book"); // Delete all molecules if(book->molecules) { for(i = 0; i < book->size; i++) book->molecules[i] = DeleteMolecule(book->molecules[i]); // Note that we do not delete the array } book->size = 0;}void ClearBook(BOOK book){ if (book == NULL) Errorf("ClearBook : NULL book"); // Deletes the content of the arrays of molecules (keeping the allocation of the array) DeleteBookMolecules(book); /* Inits the other fields */ InitTFContent(book);}BOOK DeleteBook(BOOK book){ unsigned short i; if (book == NULL) Errorf("DeleteBook : NULL book"); if (book->nRef==0) Errorf("*** Danger : trying to delete a temporary book %s\n",book->name); RemoveRefValue(book); if (book->nRef > 0) return(NULL); DeleteBookMolecules(book); if(book->molecules) { Free(book->molecules); book->molecules = NULL; } /* Deallocates the name */ if (book->name != NULL && book->name != defaultName) { Free(book->name); book->name = NULL; } book->name = defaultName; for (i=0;i<NBookSignals;i++) { DeleteSignal(book->theSignals[i]); book->theSignals[i] = NULL; } #ifdef DEBUGALLOC DebugType = "Book";#endif Free(book); return(NULL);}// Copy the arrays of molecules. BOOK CopyBook(const BOOK bookIn,BOOK bookOut){ MOLECULE moleculeOut=NULL; unsigned long n; /* Checking arguments */ if(bookIn == NULL) return(NULL); if(bookOut== NULL) bookOut = NewBook(); if(bookIn == bookOut) return(bookOut); CheckBook(bookIn); ClearBook(bookOut); /* Copying the 'tfContent' structure */ CopyFieldsTFContent(bookIn,bookOut); // Copy the molecules : Set the right size first to make it faster ... SizeBook(bookOut,bookIn->size); for(n = 0; n < bookIn->size; n++) { moleculeOut = CopyMolecule(GetBookMolecule(bookIn,n),NULL); AddMolecule2Book(bookOut,moleculeOut); } return(bookOut);}// If 'sizeAlloc' is smaller than book->size, an error is generated.// Else the allocation size of the array of molecules is adjusted// to 'sizeAlloc'.// -the newly allocated part of the array is initialized to NULL molecules;// -the previously allocated part is kept (book->size is not changed)void SizeBook(BOOK book,unsigned long sizeAlloc){ unsigned long i; if(sizeAlloc<book->size) Errorf("SizeBook : cannot (re)allocate less than the number of molecules"); if(sizeAlloc==book->size) return; // Case of an first allocation if(book->sizeAlloc == 0) { book->molecules = (MOLECULE*) Calloc(sizeAlloc,sizeof(MOLECULE)); book->sizeAlloc = sizeAlloc; } // Case of a resize else { book->molecules = (MOLECULE*) Realloc(book->molecules,sizeAlloc*sizeof(MOLECULE)); // Initialize the newly allocated data, if necessary for(i = book->sizeAlloc; i < sizeAlloc; i++) book->molecules[i] = NULL; book->sizeAlloc = sizeAlloc; }}/*--------------------------------------------------------------------------*//* * append a molecule into a book */
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -