RefVector.h

Go to the documentation of this file.
00001 /*
00002         This file is part of ALIZE which is an open-source tool for 
00003         speaker recognition.
00004 
00005     ALIZE is free software: you can redistribute it and/or modify
00006     it under the terms of the GNU Lesser General Public License as 
00007     published by the Free Software Foundation, either version 3 of 
00008     the License, or any later version.
00009 
00010     ALIZE is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU Lesser General Public License for more details.
00014 
00015     You should have received a copy of the GNU Lesser General Public 
00016     License along with ALIZE.
00017     If not, see <http://www.gnu.org/licenses/>.
00018         
00019         ALIZE is a development project initiated by the ELISA consortium
00020         [alize.univ-avignon.fr/] and funded by the French Research 
00021         Ministry in the framework of the TECHNOLANGUE program 
00022         [www.technolangue.net]
00023 
00024         The ALIZE project team wants to highlight the limits of voice
00025         authentication in a forensic context.
00026         The "Person  Authentification by Voice: A Need of Caution" paper 
00027         proposes a good overview of this point (cf. "Person  
00028         Authentification by Voice: A Need of Caution", Bonastre J.F., 
00029         Bimbot F., Boe L.J., Campbell J.P., Douglas D.A., Magrin-
00030         chagnolleau I., Eurospeech 2003, Genova].
00031         The conclusion of the paper of the paper is proposed bellow:
00032         [Currently, it is not possible to completely determine whether the 
00033         similarity between two recordings is due to the speaker or to other 
00034         factors, especially when: (a) the speaker does not cooperate, (b) there 
00035         is no control over recording equipment, (c) recording conditions are not 
00036         known, (d) one does not know whether the voice was disguised and, to a 
00037         lesser extent, (e) the linguistic content of the message is not 
00038         controlled. Caution and judgment must be exercised when applying speaker 
00039         recognition techniques, whether human or automatic, to account for these 
00040         uncontrolled factors. Under more constrained or calibrated situations, 
00041         or as an aid for investigative purposes, judicious application of these 
00042         techniques may be suitable, provided they are not considered as infallible.
00043         At the present time, there is no scientific process that enables one to 
00044         uniquely characterize a person=92s voice or to identify with absolute 
00045         certainty an individual from his or her voice.]
00046         Contact Jean-Francois Bonastre for more information about the licence or
00047         the use of ALIZE
00048 
00049         Copyright (C) 2003-2010
00050         Laboratoire d'informatique d'Avignon [lia.univ-avignon.fr]
00051         ALIZE admin [alize@univ-avignon.fr]
00052         Jean-Francois Bonastre [jean-francois.bonastre@univ-avignon.fr]
00053 */
00054 
00055 #if !defined(ALIZE_RefVector_h)
00056 #define ALIZE_RefVector_h
00057 
00058 #if defined(_WIN32)
00059 #if defined(ALIZE_EXPORTS)
00060 #define ALIZE_API __declspec(dllexport)
00061 #else
00062 #define ALIZE_API __declspec(dllimport)
00063 #endif
00064 #else
00065 #define ALIZE_API
00066 #endif
00067 
00068 #if defined(_WIN32)
00069 #pragma warning( disable : 4150)                        // disable warning 4150 due to virtual SegCluster functions
00070 #endif
00071 
00072 
00073 #include "Object.h"
00074 #include <new>
00075 #include <memory.h>
00076 #include "DoubleSquareMatrix.h"
00077 #include "Exception.h"
00078 #include "Feature.h"
00079 
00080 namespace alize
00081 {
00090 
00091   template <class T> class ALIZE_API RefVector : public Object
00092   {
00093     friend class TestRefVector;
00094   
00095   public:
00096   
00104     explicit RefVector(unsigned long capacity = 1)
00105     :Object(), _size(0), _capacity(capacity!=0?capacity:1),
00106      _array(createArray()) {}
00107 
00108     RefVector(const RefVector<T>& v)
00109     :Object(), _size(v._size), _capacity(v._size!=0?v._size:1),
00110     _array(createArray())
00111     { memcpy(_array, v._array, _size*sizeof(_array[0])); }
00112     
00113     const RefVector<T>& operator=(const RefVector<T>& v)
00114     {
00115       if (this != &v)
00116       {
00117         assert(_array != NULL);
00118         _size = v._size;
00119         if (_capacity < _size)
00120         {
00121           delete[] _array;
00122           _capacity = _size!=0?_size:1;
00123           _array = createArray();
00124         }
00125         memcpy(_array, v._array, _size*sizeof(_array[0]));
00126       }
00127       return *this;
00128     }
00129 
00130     virtual ~RefVector() { delete[] _array; }
00131 
00132     void clear() { _size = 0; }
00133 
00139     void setCapacity(unsigned long newCapacity)
00140     {
00141       T** oldArray = _array;
00142       _capacity = newCapacity!=0?newCapacity:1;
00143       _array = createArray();
00144       memcpy(_array, oldArray, _size*sizeof(_array[0]));
00145       delete[] oldArray;
00146       if (_size > _capacity)
00147         _size = _capacity;
00148     }
00149 
00150     unsigned long size() const { return _size; }
00151     bool isEmpty() const{ return _size == 0; }
00152     
00157     unsigned long addObject(T& o)
00158     {
00159       assert(_array != NULL);
00160       if (_size == _capacity)
00161       {
00162         _capacity += _capacity;
00163         T** oldArray = _array;
00164         _array = createArray();
00165         memcpy(_array, oldArray, _size*sizeof(_array[0]));
00166         delete[] oldArray;
00167       }
00168       _array[_size] = &o;
00169       _size++;
00170       return _size-1;
00171     }
00172     
00178     unsigned long addObject(T& o, unsigned long i)
00179     {
00180       // TODO : à optimiser
00181       if (i>=_size)
00182         return addObject(o);
00183       addObject(getObject(_size-1));
00184       for (long t=_size-3; t>=(long)i; t--)
00185         setObject(getObject(t), t+1);
00186       setObject(o, i);
00187       return i;
00188     }
00189   
00196     void setObject(T& o, unsigned long index) const
00197     {
00198       assertIsInBounds(__FILE__, __LINE__, index, _size);
00199       assert(_array != NULL);
00200       _array[index] = &o;
00201     }
00202   
00208     T& getObject(unsigned long index) const
00209     {
00210       assertIsInBounds(__FILE__, __LINE__, index, _size);
00211       assert(_array != NULL);
00212       return *_array[index];
00213     }
00214 
00215     T& operator[](unsigned long index)
00216     {
00217       assertIsInBounds(__FILE__, __LINE__, index, _size);
00218       assert(_array != NULL);
00219       return *_array[index];
00220     }
00221     const T& operator[](unsigned long index) const
00222     {
00223       assertIsInBounds(__FILE__, __LINE__, index, _size);
00224       assert(_array != NULL);
00225       return *_array[index];
00226     }
00227     
00233     unsigned long getObjectIndex(const T& o) const
00234     {
00235       for (unsigned long i=0; i< _size; i++)
00236         if (_array[i] == &o)
00237           return i;
00238       throw Exception("Object not found", __FILE__, __LINE__);
00239       return 0; // never executed
00240     }
00241   
00248     void removeObjects(unsigned long first, unsigned long last,
00249                        DeleteFlag fl = DO_NOT_DELETE)
00250     {
00251       if (last>_size-1)
00252         last = _size-1;
00253       if (first > last)
00254         return;
00255       assert(_array != NULL);
00256       if (fl == DELETE)
00257         for (unsigned long i=first; i<=last; i++)
00258           delete _array[i];
00259       last++;
00260       for (; last<_size; first++, last++)
00261         _array[first] = _array[last];
00262       _size -= last-first;
00263     }
00264 
00269     T& removeObject(unsigned long i)
00270     {
00271       T& o = getObject(i);
00272       for (; i+1<_size; i++)
00273         _array[i] = _array[i+1];
00274       _size--;
00275       return o;
00276     }
00277     
00284     const T& removeObject(const T& o)
00285     {
00286       for (unsigned long i=0; i<_size; i++)
00287       {
00288         if (_array[i] == &o)
00289         {
00290           for (i++; i<_size; i++)
00291             _array[i-1] = _array[i];
00292           _size--;
00293           return o;
00294         }
00295       }
00296       throw Exception("Object not found in the vector", __FILE__, __LINE__);
00297     }
00298 
00303     T** getArray() const { return _array; }
00304 
00305     void deleteAllObjects(unsigned long first = 0)
00306     {
00307                 
00308       for (unsigned long i=first; i<_size; i++)
00309         delete _array[i];
00310       _size = first<_size?first:_size;
00311                 
00312     }
00313 
00314     virtual String getClassName() const { return "RefVector"; }
00315 
00316   private:
00317   
00318     unsigned long _size;
00319     unsigned long _capacity;
00320     T**    _array;
00321 
00322     T** createArray()
00323     {
00324       assert(_capacity != 0);
00325       T** p = new (std::nothrow) T*[_capacity];
00326       assertMemoryIsAllocated(p, __FILE__, __LINE__);
00327       return p;
00328     }
00329         //bool operator==(const RefVector<T>&) const; /*!Not implemented*/
00330     //bool operator!=(const RefVector<T>&) const; /*!Not implemented*/
00331   };
00332 
00333   typedef RefVector<Object> ObjectRefVector;
00334 
00335 #if defined(_WIN32)
00336   template class RefVector<Object>;
00337   template class RefVector<DoubleSquareMatrix>;
00338   template class RefVector<DoubleVector>;
00339   template class RefVector<SegCluster>;
00340   template class RefVector<MixtureGDStat>;
00341   template class RefVector<Feature>;
00342 #endif
00343 
00344 } // end namespace alize
00345 
00346 #endif // ALIZE_RefVector_h
00347 
00348 
00349 
00350 
00351 
00352 
00353 
00354 
00355 
00356