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