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 0 : ScDataObject::~ScDataObject()
28 : {
29 0 : }
30 :
31 :
32 : // Collection
33 :
34 :
35 0 : static void lcl_DeleteScDataObjects( ScDataObject** p, sal_uInt16 nCount )
36 : {
37 0 : if ( p )
38 : {
39 0 : for (sal_uInt16 i = 0; i < nCount; i++) delete p[i];
40 0 : delete[] p;
41 0 : p = NULL;
42 : }
43 0 : }
44 :
45 0 : ScCollection::ScCollection(sal_uInt16 nLim, sal_uInt16 nDel) :
46 : nCount ( 0 ),
47 : nLimit ( nLim ),
48 : nDelta ( nDel ),
49 0 : pItems ( NULL )
50 : {
51 0 : if (nDelta > MAXDELTA)
52 0 : nDelta = MAXDELTA;
53 0 : else if (nDelta == 0)
54 0 : nDelta = 1;
55 0 : if (nLimit > MAXCOLLECTIONSIZE)
56 0 : nLimit = MAXCOLLECTIONSIZE;
57 0 : else if (nLimit < nDelta)
58 0 : nLimit = nDelta;
59 0 : pItems = new ScDataObject*[nLimit];
60 0 : }
61 :
62 0 : ScCollection::ScCollection(const ScCollection& rCollection)
63 : : ScDataObject(),
64 : nCount ( 0 ),
65 : nLimit ( 0 ),
66 : nDelta ( 0 ),
67 0 : pItems ( NULL )
68 : {
69 0 : *this = rCollection;
70 0 : }
71 :
72 0 : ScCollection::~ScCollection()
73 : {
74 0 : lcl_DeleteScDataObjects( pItems, nCount );
75 0 : }
76 :
77 :
78 0 : sal_uInt16 ScCollection::GetCount() const { return nCount; }
79 :
80 0 : bool ScCollection::AtInsert(sal_uInt16 nIndex, ScDataObject* pScDataObject)
81 : {
82 0 : if ((nCount < MAXCOLLECTIONSIZE) && (nIndex <= nCount) && pItems)
83 : {
84 0 : if (nCount == nLimit)
85 : {
86 0 : ScDataObject** pNewItems = new ScDataObject*[nLimit + nDelta];
87 0 : if (!pNewItems)
88 0 : return false;
89 0 : nLimit = sal::static_int_cast<sal_uInt16>( nLimit + nDelta );
90 0 : memcpy(pNewItems, pItems, nCount * sizeof(ScDataObject*));
91 0 : delete[] pItems;
92 0 : pItems = pNewItems;
93 : }
94 0 : if (nCount > nIndex)
95 0 : memmove(&pItems[nIndex + 1], &pItems[nIndex], (nCount - nIndex) * sizeof(ScDataObject*));
96 0 : pItems[nIndex] = pScDataObject;
97 0 : nCount++;
98 0 : return true;
99 : }
100 0 : return false;
101 : }
102 :
103 0 : bool ScCollection::Insert(ScDataObject* pScDataObject)
104 : {
105 0 : return AtInsert(nCount, pScDataObject);
106 : }
107 :
108 0 : ScDataObject* ScCollection::At(sal_uInt16 nIndex) const
109 : {
110 0 : if (nIndex < nCount)
111 0 : return pItems[nIndex];
112 : else
113 0 : return NULL;
114 : }
115 :
116 :
117 0 : ScCollection& ScCollection::operator=( const ScCollection& r )
118 : {
119 : // Check for self-assignment
120 0 : if (this == &r)
121 0 : return *this;
122 :
123 0 : lcl_DeleteScDataObjects( pItems, nCount );
124 :
125 0 : nCount = r.nCount;
126 0 : nLimit = r.nLimit;
127 0 : nDelta = r.nDelta;
128 0 : pItems = new ScDataObject*[nLimit];
129 0 : for ( sal_uInt16 i=0; i<nCount; i++ )
130 0 : pItems[i] = r.pItems[i]->Clone();
131 :
132 0 : return *this;
133 : }
134 :
135 0 : ScDataObject* ScCollection::Clone() const
136 : {
137 0 : return new ScCollection(*this);
138 : }
139 :
140 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|