Main Page Class List Class Members

- Main Page
- User's Guide
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

# sdk/extensions/authoring/source/VHACD/inc/vhacdSArray.h

Go to the documentation of this file.

    00001 /* Copyright (c) 2011 Khaled Mamou (kmamou at gmail dot com)
    00002  All rights reserved.
    00003
    00004
    00005  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
    00006
    00007  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    00008
    00009  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    00010
    00011  3. The names of the contributors may not be used to endorse or promote products derived from this software without specific prior written permission.
    00012
    00013  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    00014  */
    00015 #pragma once
    00016 #ifndef VHACD_SARRAY_H
    00017 #define VHACD_SARRAY_H
    00018 #include <stdio.h>
    00019 #include <stdlib.h>
    00020 #include <string.h>
    00021
    00022 #define SARRAY_DEFAULT_MIN_SIZE 16
    00023
    00024 namespace VHACD {
    00026 template <typename T, size_t N = 64>
    00027 class SArray {
    00028 public:
    00029     T& operator[](size_t i)
    00030     {
    00031         T* const data = Data();
    00032         return data[i];
    00033     }
    00034     const T& operator[](size_t i) const
    00035     {
    00036         const T* const data = Data();
    00037         return data[i];
    00038     }
    00039     size_t Size() const
    00040     {
    00041         return m_size;
    00042     }
    00043     T* const Data()
    00044     {
    00045         return (m_maxSize == N) ? m_data0 : m_data;
    00046     }
    00047     const T* const Data() const
    00048     {
    00049         return (m_maxSize == N) ? m_data0 : m_data;
    00050     }
    00051     void Clear()
    00052     {
    00053         m_size = 0;
    00054         delete[] m_data;
    00055         m_data = 0;
    00056         m_maxSize = N;
    00057     }
    00058     void PopBack()
    00059     {
    00060         --m_size;
    00061     }
    00062     void Allocate(size_t size)
    00063     {
    00064         if (size > m_maxSize) {
    00065             T* temp = new T[size];
    00066             memcpy(temp, Data(), m_size * sizeof(T));
    00067             delete[] m_data;
    00068             m_data = temp;
    00069             m_maxSize = size;
    00070         }
    00071     }
    00072     void Resize(size_t size)
    00073     {
    00074         Allocate(size);
    00075         m_size = size;
    00076     }
    00077
    00078     void PushBack(const T& value)
    00079     {
    00080         if (m_size == m_maxSize) {
    00081             size_t maxSize = (m_maxSize << 1);
    00082             T* temp = new T[maxSize];
    00083             memcpy(temp, Data(), m_maxSize * sizeof(T));
    00084             delete[] m_data;
    00085             m_data = temp;
    00086             m_maxSize = maxSize;
    00087         }
    00088         T* const data = Data();
    00089         data[m_size++] = value;
    00090     }
    00091     bool Find(const T& value, size_t& pos)
    00092     {
    00093         T* const data = Data();
    00094         for (pos = 0; pos < m_size; ++pos)
    00095             if (value == data[pos])
    00096                 return true;
    00097         return false;
    00098     }
    00099     bool Insert(const T& value)
    00100     {
    00101         size_t pos;
    00102         if (Find(value, pos))
    00103             return false;
    00104         PushBack(value);
    00105         return true;
    00106     }
    00107     bool Erase(const T& value)
    00108     {
    00109         size_t pos;
    00110         T* const data = Data();
    00111         if (Find(value, pos)) {
    00112             for (size_t j = pos + 1; j < m_size; ++j)
    00113                 data[j - 1] = data[j];
    00114             --m_size;
    00115             return true;
    00116         }
    00117         return false;
    00118     }
    00119     void operator=(const SArray& rhs)
    00120     {
    00121         if (m_maxSize < rhs.m_size) {
    00122             delete[] m_data;
    00123             m_maxSize = rhs.m_maxSize;
    00124             m_data = new T[m_maxSize];
    00125         }
    00126         m_size = rhs.m_size;
    00127         memcpy(Data(), rhs.Data(), m_size * sizeof(T));
    00128     }
    00129     void Initialize()
    00130     {
    00131         m_data = 0;
    00132         m_size = 0;
    00133         m_maxSize = N;
    00134     }
    00135     SArray(const SArray& rhs)
    00136     {
    00137         m_data = 0;
    00138         m_size = 0;
    00139         m_maxSize = N;
    00140         *this = rhs;
    00141     }
    00142     SArray()
    00143     {
    00144         Initialize();
    00145     }
    00146     ~SArray()
    00147     {
    00148         delete[] m_data;
    00149     }
    00150
    00151 private:
    00152     T m_data0[N];
    00153     T* m_data;
    00154     size_t m_size;
    00155     size_t m_maxSize;
    00156 };
    00157 }
    00158 #endif

Copyright © 2015-2017 NVIDIA Corporation, 2701 San Tomas Expressway, Santa Clara, CA 95050 U.S.A. All rights reserved. www.nvidia.com