ValueVectorOf.c
上传用户:huihehuasu
上传日期:2007-01-10
资源大小:6948k
文件大小:10k
源码类别:

xml/soap/webservice

开发平台:

C/C++

  1. /*
  2.  * The Apache Software License, Version 1.1
  3.  * 
  4.  * Copyright (c) 1999-2000 The Apache Software Foundation.  All rights
  5.  * reserved.
  6.  * 
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  * 
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer. 
  13.  * 
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  *    notice, this list of conditions and the following disclaimer in
  16.  *    the documentation and/or other materials provided with the
  17.  *    distribution.
  18.  * 
  19.  * 3. The end-user documentation included with the redistribution,
  20.  *    if any, must include the following acknowledgment:  
  21.  *       "This product includes software developed by the
  22.  *        Apache Software Foundation (http://www.apache.org/)."
  23.  *    Alternately, this acknowledgment may appear in the software itself,
  24.  *    if and wherever such third-party acknowledgments normally appear.
  25.  * 
  26.  * 4. The names "Xerces" and "Apache Software Foundation" must
  27.  *    not be used to endorse or promote products derived from this
  28.  *    software without prior written permission. For written 
  29.  *    permission, please contact apache@apache.org.
  30.  * 
  31.  * 5. Products derived from this software may not be called "Apache",
  32.  *    nor may "Apache" appear in their name, without prior written
  33.  *    permission of the Apache Software Foundation.
  34.  * 
  35.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36.  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38.  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39.  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42.  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44.  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45.  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46.  * SUCH DAMAGE.
  47.  * ====================================================================
  48.  * 
  49.  * This software consists of voluntary contributions made by many
  50.  * individuals on behalf of the Apache Software Foundation, and was
  51.  * originally based on software copyright (c) 1999, International
  52.  * Business Machines, Inc., http://www.ibm.com .  For more information
  53.  * on the Apache Software Foundation, please see
  54.  * <http://www.apache.org/>.
  55.  */
  56. /**
  57.  * $Log: ValueVectorOf.c,v $
  58.  * Revision 1.4  2001/08/09 15:24:37  knoaman
  59.  * add support for <anyAttribute> declaration.
  60.  *
  61.  * Revision 1.3  2000/03/02 19:54:47  roddey
  62.  * This checkin includes many changes done while waiting for the
  63.  * 1.1.0 code to be finished. I can't list them all here, but a list is
  64.  * available elsewhere.
  65.  *
  66.  * Revision 1.2  2000/02/06 07:48:05  rahulj
  67.  * Year 2K copyright swat.
  68.  *
  69.  * Revision 1.1.1.1  1999/11/09 01:05:31  twl
  70.  * Initial checkin
  71.  *
  72.  * Revision 1.2  1999/11/08 20:45:18  rahul
  73.  * Swat for adding in Product name and CVS comment log variable.
  74.  *
  75.  */
  76. // ---------------------------------------------------------------------------
  77. //  Includes
  78. // ---------------------------------------------------------------------------
  79. #if defined(XERCES_TMPLSINC)
  80. #include <util/ValueVectorOf.hpp>
  81. #endif
  82. // ---------------------------------------------------------------------------
  83. //  ValueVectorOf: Constructors and Destructor
  84. // ---------------------------------------------------------------------------
  85. template <class TElem> ValueVectorOf<TElem>::
  86. ValueVectorOf(const unsigned int maxElems) :
  87.     fCurCount(0)
  88.     , fMaxCount(maxElems)
  89.     , fElemList(0)
  90. {
  91.     fElemList = new TElem[fMaxCount];
  92. }
  93. template <class TElem> ValueVectorOf<TElem>::
  94. ValueVectorOf(const ValueVectorOf<TElem>& toCopy) :
  95.     fCurCount(toCopy.fCurCount)
  96.     , fMaxCount(toCopy.fMaxCount)
  97.     , fElemList(0)
  98. {
  99.     fElemList = new TElem[fMaxCount];
  100.     for (unsigned int index = 0; index < fCurCount; index++)
  101.         fElemList[index] = toCopy.fElemList[index];
  102. }
  103. template <class TElem> ValueVectorOf<TElem>::~ValueVectorOf()
  104. {
  105.     delete [] fElemList;
  106. }
  107. // ---------------------------------------------------------------------------
  108. //  ValueVectorOf: Operators
  109. // ---------------------------------------------------------------------------
  110. template <class TElem> ValueVectorOf<TElem>&
  111. ValueVectorOf<TElem>::operator=(const ValueVectorOf<TElem>& toAssign)
  112. {
  113.     if (this == &toAssign)
  114.         return *this;
  115.     // Reallocate if required
  116.     if (fMaxCount < toAssign.fCurCount)
  117.     {
  118.         delete [] fElemList;
  119.         fElemList = new TElem[toAssign.fMaxCount];
  120.         fMaxCount = toAssign.fMaxCount;
  121.     }
  122.     fCurCount = toAssign.fCurCount;
  123.     for (unsigned int index = 0; index < fCurCount; index++)
  124.         fElemList[index] = toAssign.fElemList[index];
  125.     return *this;
  126. }
  127. // ---------------------------------------------------------------------------
  128. //  ValueVectorOf: Element management
  129. // ---------------------------------------------------------------------------
  130. template <class TElem> void ValueVectorOf<TElem>::addElement(const TElem& toAdd)
  131. {
  132.     ensureExtraCapacity(1);
  133.     fElemList[fCurCount] = toAdd;
  134.     fCurCount++;
  135. }
  136. template <class TElem> void ValueVectorOf<TElem>::
  137. setElementAt(const TElem& toSet, const unsigned int setAt)
  138. {
  139.     if (setAt >= fCurCount)
  140.         ThrowXML(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex);
  141.     fElemList[setAt] = toSet;
  142. }
  143. template <class TElem> void ValueVectorOf<TElem>::
  144. insertElementAt(const TElem& toInsert, const unsigned int insertAt)
  145. {
  146.     if (insertAt == fCurCount)
  147.     {
  148.         addElement(toInsert);
  149.         return;
  150.     }
  151.     if (insertAt > fCurCount)
  152.         ThrowXML(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex);
  153.     // Make room for the newbie
  154.     for (unsigned int index = fCurCount; index > insertAt; index--)
  155.         fElemList[index] = fElemList[index-1];
  156.     // And stick it in and bump the count
  157.     fElemList[insertAt] = toInsert;
  158.     fCurCount++;
  159. }
  160. template <class TElem> void ValueVectorOf<TElem>::
  161. removeElementAt(const unsigned int removeAt)
  162. {
  163.     if (removeAt >= fCurCount)
  164.         ThrowXML(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex);
  165.     if (removeAt == fCurCount-1)
  166.     {
  167.         fCurCount--;
  168.         return;
  169.     }
  170.     // Copy down every element above remove point
  171.     for (unsigned int index = removeAt; index < fCurCount-1; index++)
  172.         fElemList[index] = fElemList[index+1];
  173.     // And bump down count
  174.     fCurCount--;
  175. }
  176. template <class TElem> void ValueVectorOf<TElem>::removeAllElements()
  177. {
  178.     fCurCount = 0;
  179. }
  180. template <class TElem> 
  181. bool ValueVectorOf<TElem>::containsElement(const TElem& toCheck) {
  182.     for (unsigned int i = 0; i < fCurCount; i++) {
  183.         if (fElemList[i] == toCheck) {
  184.             return true;
  185.         }
  186.     }
  187.     return false;
  188. }
  189. // ---------------------------------------------------------------------------
  190. //  ValueVectorOf: Getter methods
  191. // ---------------------------------------------------------------------------
  192. template <class TElem> const TElem& ValueVectorOf<TElem>::
  193. elementAt(const unsigned int getAt) const
  194. {
  195.     if (getAt >= fCurCount)
  196.         ThrowXML(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex);
  197.     return fElemList[getAt];
  198. }
  199. template <class TElem> TElem& ValueVectorOf<TElem>::
  200. elementAt(const unsigned int getAt)
  201. {
  202.     if (getAt >= fCurCount)
  203.         ThrowXML(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex);
  204.     return fElemList[getAt];
  205. }
  206. template <class TElem> unsigned int ValueVectorOf<TElem>::curCapacity() const
  207. {
  208.     return fMaxCount;
  209. }
  210. template <class TElem> unsigned int ValueVectorOf<TElem>::size() const
  211. {
  212.     return fCurCount;
  213. }
  214. // ---------------------------------------------------------------------------
  215. //  ValueVectorOf: Miscellaneous
  216. // ---------------------------------------------------------------------------
  217. template <class TElem> void ValueVectorOf<TElem>::
  218. ensureExtraCapacity(const unsigned int length)
  219. {
  220.     unsigned int newMax = fCurCount + length;
  221.     if (newMax < fMaxCount)
  222.         return;
  223.     // Avoid too many reallocations by expanding by a percentage
  224.     unsigned int minNewMax = (unsigned int)((double)fCurCount * 1.25);
  225.     if (newMax < minNewMax)
  226.         newMax = minNewMax;
  227.     TElem* newList = new TElem[newMax];
  228.     for (unsigned int index = 0; index < fCurCount; index++)
  229.         newList[index] = fElemList[index];
  230.     delete [] fElemList;
  231.     fElemList = newList;
  232.     fMaxCount = newMax;
  233. }
  234. template <class TElem> const TElem* ValueVectorOf<TElem>::rawData() const
  235. {
  236.     return fElemList;
  237. }
  238. // ---------------------------------------------------------------------------
  239. //  ValueVectorEnumerator: Constructors and Destructor
  240. // ---------------------------------------------------------------------------
  241. template <class TElem> ValueVectorEnumerator<TElem>::
  242. ValueVectorEnumerator(       ValueVectorOf<TElem>* const toEnum
  243.                      , const bool                        adopt) :
  244.     fAdopted(adopt)
  245.     , fCurIndex(0)
  246.     , fToEnum(toEnum)
  247. {
  248. }
  249. template <class TElem> ValueVectorEnumerator<TElem>::~ValueVectorEnumerator()
  250. {
  251.     if (fAdopted)
  252.         delete fToEnum;
  253. }
  254. // ---------------------------------------------------------------------------
  255. //  ValueVectorEnumerator: Enum interface
  256. // ---------------------------------------------------------------------------
  257. template <class TElem> bool
  258. ValueVectorEnumerator<TElem>::hasMoreElements() const
  259. {
  260.     if (fCurIndex >= fToEnum->size())
  261.         return false;
  262.     return true;
  263. }
  264. template <class TElem> TElem& ValueVectorEnumerator<TElem>::nextElement()
  265. {
  266.     return fToEnum->elementAt(fCurIndex++);
  267. }
  268. template <class TElem> void ValueVectorEnumerator<TElem>::Reset()
  269. {
  270.     fCurIndex = 0;
  271. }