Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "collect.hxx"
21 :
22 : #include <string.h>
23 :
24 : #define MAXCOLLECTIONSIZE 16384
25 : #define MAXDELTA 1024
26 :
27 : // -----------------------------------------------------------------------
28 :
29 0 : ScDataObject::~ScDataObject()
30 : {
31 0 : }
32 :
33 : //------------------------------------------------------------------------
34 : // Collection
35 : //------------------------------------------------------------------------
36 :
37 0 : static void lcl_DeleteScDataObjects( ScDataObject** p, sal_uInt16 nCount )
38 : {
39 0 : if ( p )
40 : {
41 0 : for (sal_uInt16 i = 0; i < nCount; i++) delete p[i];
42 0 : delete[] p;
43 0 : p = NULL;
44 : }
45 0 : }
46 :
47 0 : ScCollection::ScCollection(sal_uInt16 nLim, sal_uInt16 nDel) :
48 : nCount ( 0 ),
49 : nLimit ( nLim ),
50 : nDelta ( nDel ),
51 0 : pItems ( NULL )
52 : {
53 0 : if (nDelta > MAXDELTA)
54 0 : nDelta = MAXDELTA;
55 0 : else if (nDelta == 0)
56 0 : nDelta = 1;
57 0 : if (nLimit > MAXCOLLECTIONSIZE)
58 0 : nLimit = MAXCOLLECTIONSIZE;
59 0 : else if (nLimit < nDelta)
60 0 : nLimit = nDelta;
61 0 : pItems = new ScDataObject*[nLimit];
62 0 : }
63 :
64 0 : ScCollection::ScCollection(const ScCollection& rCollection)
65 : : ScDataObject(),
66 : nCount ( 0 ),
67 : nLimit ( 0 ),
68 : nDelta ( 0 ),
69 0 : pItems ( NULL )
70 : {
71 0 : *this = rCollection;
72 0 : }
73 :
74 : //------------------------------------------------------------------------
75 :
76 0 : ScCollection::~ScCollection()
77 : {
78 0 : lcl_DeleteScDataObjects( pItems, nCount );
79 0 : }
80 :
81 : //------------------------------------------------------------------------
82 0 : sal_uInt16 ScCollection::GetCount() const { return nCount; }
83 :
84 : //------------------------------------------------------------------------
85 :
86 0 : sal_Bool ScCollection::AtInsert(sal_uInt16 nIndex, ScDataObject* pScDataObject)
87 : {
88 0 : if ((nCount < MAXCOLLECTIONSIZE) && (nIndex <= nCount) && pItems)
89 : {
90 0 : if (nCount == nLimit)
91 : {
92 0 : ScDataObject** pNewItems = new ScDataObject*[nLimit + nDelta];
93 0 : if (!pNewItems)
94 0 : return false;
95 0 : nLimit = sal::static_int_cast<sal_uInt16>( nLimit + nDelta );
96 0 : memcpy(pNewItems, pItems, nCount * sizeof(ScDataObject*));
97 0 : delete[] pItems;
98 0 : pItems = pNewItems;
99 : }
100 0 : if (nCount > nIndex)
101 0 : memmove(&pItems[nIndex + 1], &pItems[nIndex], (nCount - nIndex) * sizeof(ScDataObject*));
102 0 : pItems[nIndex] = pScDataObject;
103 0 : nCount++;
104 0 : return sal_True;
105 : }
106 0 : return false;
107 : }
108 :
109 : //------------------------------------------------------------------------
110 :
111 0 : sal_Bool ScCollection::Insert(ScDataObject* pScDataObject)
112 : {
113 0 : return AtInsert(nCount, pScDataObject);
114 : }
115 :
116 : //------------------------------------------------------------------------
117 :
118 0 : ScDataObject* ScCollection::At(sal_uInt16 nIndex) const
119 : {
120 0 : if (nIndex < nCount)
121 0 : return pItems[nIndex];
122 : else
123 0 : return NULL;
124 : }
125 :
126 : //------------------------------------------------------------------------
127 :
128 0 : sal_uInt16 ScCollection::IndexOf(ScDataObject* pScDataObject) const
129 : {
130 0 : sal_uInt16 nIndex = 0xffff;
131 0 : for (sal_uInt16 i = 0; ((i < nCount) && (nIndex == 0xffff)); i++)
132 : {
133 0 : if (pItems[i] == pScDataObject) nIndex = i;
134 : }
135 0 : return nIndex;
136 : }
137 :
138 : //------------------------------------------------------------------------
139 :
140 0 : ScCollection& ScCollection::operator=( const ScCollection& r )
141 : {
142 : // Check for self-assignment
143 0 : if (this == &r)
144 0 : return *this;
145 :
146 0 : lcl_DeleteScDataObjects( pItems, nCount );
147 :
148 0 : nCount = r.nCount;
149 0 : nLimit = r.nLimit;
150 0 : nDelta = r.nDelta;
151 0 : pItems = new ScDataObject*[nLimit];
152 0 : for ( sal_uInt16 i=0; i<nCount; i++ )
153 0 : pItems[i] = r.pItems[i]->Clone();
154 :
155 0 : return *this;
156 : }
157 :
158 : //------------------------------------------------------------------------
159 :
160 0 : ScDataObject* ScCollection::Clone() const
161 : {
162 0 : return new ScCollection(*this);
163 : }
164 :
165 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|