1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "collect.hxx"
24 #define MAXCOLLECTIONSIZE 16384
27 // -----------------------------------------------------------------------
29 ScDataObject::~ScDataObject()
33 //------------------------------------------------------------------------
35 //------------------------------------------------------------------------
37 static void lcl_DeleteScDataObjects( ScDataObject
** p
, sal_uInt16 nCount
)
41 for (sal_uInt16 i
= 0; i
< nCount
; i
++) delete p
[i
];
47 ScCollection::ScCollection(sal_uInt16 nLim
, sal_uInt16 nDel
) :
53 if (nDelta
> MAXDELTA
)
57 if (nLimit
> MAXCOLLECTIONSIZE
)
58 nLimit
= MAXCOLLECTIONSIZE
;
59 else if (nLimit
< nDelta
)
61 pItems
= new ScDataObject
*[nLimit
];
64 ScCollection::ScCollection(const ScCollection
& rCollection
)
74 //------------------------------------------------------------------------
76 ScCollection::~ScCollection()
78 lcl_DeleteScDataObjects( pItems
, nCount
);
81 //------------------------------------------------------------------------
82 sal_uInt16
ScCollection::GetCount() const { return nCount
; }
84 //------------------------------------------------------------------------
86 sal_Bool
ScCollection::AtInsert(sal_uInt16 nIndex
, ScDataObject
* pScDataObject
)
88 if ((nCount
< MAXCOLLECTIONSIZE
) && (nIndex
<= nCount
) && pItems
)
92 ScDataObject
** pNewItems
= new ScDataObject
*[nLimit
+ nDelta
];
95 nLimit
= sal::static_int_cast
<sal_uInt16
>( nLimit
+ nDelta
);
96 memcpy(pNewItems
, pItems
, nCount
* sizeof(ScDataObject
*));
101 memmove(&pItems
[nIndex
+ 1], &pItems
[nIndex
], (nCount
- nIndex
) * sizeof(ScDataObject
*));
102 pItems
[nIndex
] = pScDataObject
;
109 //------------------------------------------------------------------------
111 sal_Bool
ScCollection::Insert(ScDataObject
* pScDataObject
)
113 return AtInsert(nCount
, pScDataObject
);
116 //------------------------------------------------------------------------
118 ScDataObject
* ScCollection::At(sal_uInt16 nIndex
) const
121 return pItems
[nIndex
];
126 //------------------------------------------------------------------------
128 sal_uInt16
ScCollection::IndexOf(ScDataObject
* pScDataObject
) const
130 sal_uInt16 nIndex
= 0xffff;
131 for (sal_uInt16 i
= 0; ((i
< nCount
) && (nIndex
== 0xffff)); i
++)
133 if (pItems
[i
] == pScDataObject
) nIndex
= i
;
138 //------------------------------------------------------------------------
140 ScCollection
& ScCollection::operator=( const ScCollection
& r
)
142 // Check for self-assignment
146 lcl_DeleteScDataObjects( pItems
, nCount
);
151 pItems
= new ScDataObject
*[nLimit
];
152 for ( sal_uInt16 i
=0; i
<nCount
; i
++ )
153 pItems
[i
] = r
.pItems
[i
]->Clone();
158 //------------------------------------------------------------------------
160 ScDataObject
* ScCollection::Clone() const
162 return new ScCollection(*this);
165 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */