Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #include "collect.hxx"
30 : :
31 : : #include <string.h>
32 : :
33 : : #define MAXCOLLECTIONSIZE 16384
34 : : #define MAXDELTA 1024
35 : :
36 : : // -----------------------------------------------------------------------
37 : :
38 : 0 : ScDataObject::~ScDataObject()
39 : : {
40 [ # # ]: 0 : }
41 : :
42 : : //------------------------------------------------------------------------
43 : : // Collection
44 : : //------------------------------------------------------------------------
45 : :
46 : 0 : void lcl_DeleteScDataObjects( ScDataObject** p, sal_uInt16 nCount )
47 : : {
48 [ # # ]: 0 : if ( p )
49 : : {
50 [ # # ][ # # ]: 0 : for (sal_uInt16 i = 0; i < nCount; i++) delete p[i];
51 [ # # ]: 0 : delete[] p;
52 : 0 : p = NULL;
53 : : }
54 : 0 : }
55 : :
56 : 0 : ScCollection::ScCollection(sal_uInt16 nLim, sal_uInt16 nDel) :
57 : : nCount ( 0 ),
58 : : nLimit ( nLim ),
59 : : nDelta ( nDel ),
60 : 0 : pItems ( NULL )
61 : : {
62 [ # # ]: 0 : if (nDelta > MAXDELTA)
63 : 0 : nDelta = MAXDELTA;
64 [ # # ]: 0 : else if (nDelta == 0)
65 : 0 : nDelta = 1;
66 [ # # ]: 0 : if (nLimit > MAXCOLLECTIONSIZE)
67 : 0 : nLimit = MAXCOLLECTIONSIZE;
68 [ # # ]: 0 : else if (nLimit < nDelta)
69 : 0 : nLimit = nDelta;
70 [ # # ]: 0 : pItems = new ScDataObject*[nLimit];
71 : 0 : }
72 : :
73 : 0 : ScCollection::ScCollection(const ScCollection& rCollection)
74 : : : ScDataObject(),
75 : : nCount ( 0 ),
76 : : nLimit ( 0 ),
77 : : nDelta ( 0 ),
78 : 0 : pItems ( NULL )
79 : : {
80 [ # # ]: 0 : *this = rCollection;
81 : 0 : }
82 : :
83 : : //------------------------------------------------------------------------
84 : :
85 : 0 : ScCollection::~ScCollection()
86 : : {
87 [ # # ]: 0 : lcl_DeleteScDataObjects( pItems, nCount );
88 [ # # ]: 0 : }
89 : :
90 : : //------------------------------------------------------------------------
91 : 0 : sal_uInt16 ScCollection::GetCount() const { return nCount; }
92 : :
93 : : //------------------------------------------------------------------------
94 : :
95 : 0 : sal_Bool ScCollection::AtInsert(sal_uInt16 nIndex, ScDataObject* pScDataObject)
96 : : {
97 [ # # ][ # # ]: 0 : if ((nCount < MAXCOLLECTIONSIZE) && (nIndex <= nCount) && pItems)
[ # # ]
98 : : {
99 [ # # ]: 0 : if (nCount == nLimit)
100 : : {
101 : 0 : ScDataObject** pNewItems = new ScDataObject*[nLimit + nDelta];
102 [ # # ]: 0 : if (!pNewItems)
103 : 0 : return false;
104 : 0 : nLimit = sal::static_int_cast<sal_uInt16>( nLimit + nDelta );
105 : 0 : memmove(pNewItems, pItems, nCount * sizeof(ScDataObject*));
106 [ # # ]: 0 : delete[] pItems;
107 : 0 : pItems = pNewItems;
108 : : }
109 [ # # ]: 0 : if (nCount > nIndex)
110 : 0 : memmove(&pItems[nIndex + 1], &pItems[nIndex], (nCount - nIndex) * sizeof(ScDataObject*));
111 : 0 : pItems[nIndex] = pScDataObject;
112 : 0 : nCount++;
113 : 0 : return sal_True;
114 : : }
115 : 0 : return false;
116 : : }
117 : :
118 : : //------------------------------------------------------------------------
119 : :
120 : 0 : sal_Bool ScCollection::Insert(ScDataObject* pScDataObject)
121 : : {
122 : 0 : return AtInsert(nCount, pScDataObject);
123 : : }
124 : :
125 : : //------------------------------------------------------------------------
126 : :
127 : 0 : ScDataObject* ScCollection::At(sal_uInt16 nIndex) const
128 : : {
129 [ # # ]: 0 : if (nIndex < nCount)
130 : 0 : return pItems[nIndex];
131 : : else
132 : 0 : return NULL;
133 : : }
134 : :
135 : : //------------------------------------------------------------------------
136 : :
137 : 0 : sal_uInt16 ScCollection::IndexOf(ScDataObject* pScDataObject) const
138 : : {
139 : 0 : sal_uInt16 nIndex = 0xffff;
140 [ # # ][ # # ]: 0 : for (sal_uInt16 i = 0; ((i < nCount) && (nIndex == 0xffff)); i++)
[ # # ]
141 : : {
142 [ # # ]: 0 : if (pItems[i] == pScDataObject) nIndex = i;
143 : : }
144 : 0 : return nIndex;
145 : : }
146 : :
147 : : //------------------------------------------------------------------------
148 : :
149 : 0 : ScCollection& ScCollection::operator=( const ScCollection& r )
150 : : {
151 : : // Check for self-assignment
152 [ # # ]: 0 : if (this == &r)
153 : 0 : return *this;
154 : :
155 : 0 : lcl_DeleteScDataObjects( pItems, nCount );
156 : :
157 : 0 : nCount = r.nCount;
158 : 0 : nLimit = r.nLimit;
159 : 0 : nDelta = r.nDelta;
160 : 0 : pItems = new ScDataObject*[nLimit];
161 [ # # ]: 0 : for ( sal_uInt16 i=0; i<nCount; i++ )
162 : 0 : pItems[i] = r.pItems[i]->Clone();
163 : :
164 : 0 : return *this;
165 : : }
166 : :
167 : : //------------------------------------------------------------------------
168 : :
169 : 0 : ScDataObject* ScCollection::Clone() const
170 : : {
171 [ # # ]: 0 : return new ScCollection(*this);
172 : : }
173 : :
174 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|