QCodeEdit  2.2
lib/qnfa/light_vector.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2006-2009 fullmetalcoder <fullmetalcoder@hotmail.fr>
00004 **
00005 ** This file is part of the Edyuk project <http://edyuk.org>
00006 ** 
00007 ** This file may be used under the terms of the GNU General Public License
00008 ** version 3 as published by the Free Software Foundation and appearing in the
00009 ** file GPL.txt included in the packaging of this file.
00010 **
00011 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00012 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00013 **
00014 ****************************************************************************/
00015 
00016 #ifndef _LIGHT_VECTOR_H_
00017 #define _LIGHT_VECTOR_H_
00018 
00019 #include <qglobal.h>
00020 
00026 template <typename T>
00027 class light_vector
00028 {
00029     public:
00030         light_vector() : m_data(0), size(0) {}
00031         ~light_vector() { free(m_data); }
00032         
00033         light_vector& operator = (const light_vector& o)
00034         {
00035             size = o.size;
00036             m_data = o.m_data;
00037             
00038             return *this;
00039         }
00040         
00041         light_vector& operator << (const T& v)
00042         {
00043             append(v);
00044             
00045             return *this;
00046         }
00047         
00048         inline quint16 length() const
00049         { return size; }
00050         
00051         inline quint16 count() const
00052         { return size; }
00053         
00054         inline T* data()
00055         { return m_data; }
00056         
00057         void alloc(int pos, size_t n)
00058         {
00059             size += n;
00060             m_data = !m_data ? (T*)malloc(size * sizeof(T)) : (T*)realloc(m_data, size * sizeof(T));
00061             
00062             for ( int i = size - 1; (i > pos) && (i >= (int)n); --i )
00063                 m_data[i] = m_data[i - n];
00064             
00065             //for ( int i = pos; (i < (pos + n)) && ((i + n) < size); ++i )
00066             //  m_data[i + n] = m_data[i];
00067             
00068         }
00069         
00070         inline void prepend(const T& v)
00071         {
00072             insert(0, v);
00073         }
00074         
00075         void insert(int i, const T& v)
00076         {
00077             i = qBound(0, i, (int)size);
00078             
00079             alloc(i, 1);
00080             m_data[i] = v;
00081         }
00082         
00083         void append(const T& v)
00084         {
00085             ++size;
00086             m_data = !m_data ? (T*)malloc(size * sizeof(T)) : (T*)realloc(m_data, size * sizeof(T));
00087             m_data[size - 1] = v;
00088         }
00089         
00090         inline const T& at(quint16 i)
00091         {
00092             return *(m_data + i);
00093         }
00094         
00095         inline T& operator [] (quint16 i)
00096         {
00097             return *(m_data + i);
00098         }
00099         
00100         bool contains(const T& v) const
00101         {
00102             for ( int i = 0; i < size; i++ )
00103                 if ( m_data[i] == v )
00104                     return true;
00105             
00106             return false;
00107         }
00108         
00109     private:
00110         T* m_data;
00111         quint16 size;
00112 };
00113 
00114 #endif // _LIGHT_VECTOR_H_