LibreOffice Module sc (master)  1
compressedarray.hxx
Go to the documentation of this file.
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 #ifndef INCLUDED_SC_INC_COMPRESSEDARRAY_HXX
21 #define INCLUDED_SC_INC_COMPRESSEDARRAY_HXX
22 
23 #include <cstddef>
24 #include <memory>
25 
26 #include "scdllapi.h"
27 
46 template< typename A, typename D > class ScCompressedArray
47 {
48 public:
49  class Iterator
50  {
53  size_t mnIndex = 0;
54  A mnRegion = 0;
55  Iterator(const ScCompressedArray& rArray) : mrArray(rArray) {}
56  Iterator(const ScCompressedArray& rArray, size_t nIndex, A nRegion) : mrArray(rArray), mnIndex(nIndex), mnRegion(nRegion) {}
57  public:
58  void operator++();
59  Iterator operator+(size_t) const;
60  const D & operator*() const { return mrArray.pData[mnIndex].aValue; }
61  };
62  struct DataEntry
63  {
64  A nEnd; // start is end of previous entry + 1
66  DataEntry() {}
67  };
68  struct RangeData
69  {
72  };
73 
76  const D& rValue );
77  virtual ~ScCompressedArray();
78  void Reset( const D& rValue );
79  void SetValue( A nPos, const D& rValue );
80  void SetValue( A nStart, A nEnd, const D& rValue );
81  [[nodiscard]]
82  const D& GetValue( A nPos ) const;
83  [[nodiscard]]
84  A GetLastPos() const { return pData[nCount-1].nEnd; }
85 
87  [[nodiscard]]
88  const D& GetValue( A nPos, size_t& nIndex, A& nEnd ) const;
89 
91  [[nodiscard]]
92  RangeData GetRangeData( A nPos ) const;
93 
97  [[nodiscard]]
98  const D& GetNextValue( size_t& nIndex, A& nEnd ) const;
99 
102  const D& Insert( A nStart, size_t nCount );
103  void InsertPreservingSize( A nStart, size_t nCount, const D& rFillValue );
104 
105  void Remove( A nStart, size_t nCount );
106  void RemovePreservingSize( A nStart, size_t nCount, const D& rFillValue );
107 
109  void CopyFrom( const ScCompressedArray& rArray,
110  A nStart, A nEnd )
111  { CopyFrom(rArray, nStart, nEnd, nStart); }
112  void CopyFrom( const ScCompressedArray& rArray,
113  A nDestStart, A nDestEnd, A nSrcStart );
114 
115  // methods public for the coupled array sum methods
117  SC_DLLPUBLIC size_t Search( A nPos ) const;
118 
119  Iterator begin() const { return Iterator(*this); }
120 
121 protected:
122  size_t nCount;
123  size_t nLimit;
124  std::unique_ptr<DataEntry[]> pData;
126 };
127 
128 template< typename A, typename D >
129 void ScCompressedArray<A,D>::Reset( const D& rValue )
130 {
131  // Create a temporary copy in case we got a reference passed that points to
132  // a part of the array to be reallocated.
133  D aTmpVal( rValue);
134  nCount = nLimit = 1;
135  pData.reset(new DataEntry[1]);
136  pData[0].aValue = aTmpVal;
137  pData[0].nEnd = nMaxAccess;
138 }
139 
140 template< typename A, typename D >
141 void ScCompressedArray<A,D>::SetValue( A nPos, const D& rValue )
142 {
143  SetValue( nPos, nPos, rValue);
144 }
145 
146 template< typename A, typename D >
148 {
149  size_t nIndex = Search( nPos);
150  return pData[nIndex].aValue;
151 }
152 
153 template< typename A, typename D >
154 const D& ScCompressedArray<A,D>::GetValue( A nPos, size_t& nIndex, A& nEnd ) const
155 {
156  nIndex = Search( nPos);
157  nEnd = pData[nIndex].nEnd;
158  return pData[nIndex].aValue;
159 }
160 
161 template< typename A, typename D >
163 {
164  size_t nIndex = Search( nPos);
166  aData.mnRow1 = nIndex == 0 ? 0 : pData[nIndex - 1].nEnd + 1;
167  aData.mnRow2 = pData[nIndex].nEnd;
168  aData.maValue = pData[nIndex].aValue;
169  return aData;
170 }
171 
172 template< typename A, typename D >
173 const D& ScCompressedArray<A,D>::GetNextValue( size_t& nIndex, A& nEnd ) const
174 {
175  if (nIndex < nCount)
176  ++nIndex;
177  size_t nEntry = (nIndex < nCount ? nIndex : nCount-1);
178  nEnd = pData[nEntry].nEnd;
179  return pData[nEntry].aValue;
180 }
181 
182 // ScBitMaskCompressedArray
186 template< typename A, typename D > class ScBitMaskCompressedArray final : public ScCompressedArray<A,D>
187 {
188 public:
189  ScBitMaskCompressedArray( A nMaxAccessP,
190  const D& rValue )
191  : ScCompressedArray<A,D>( nMaxAccessP, rValue )
192  {}
193  void AndValue( A nPos, const D& rValueToAnd );
194  void OrValue( A nPos, const D& rValueToOr );
195  void AndValue( A nStart, A nEnd, const D& rValueToAnd );
196  void OrValue( A nStart, A nEnd, const D& rValueToOr );
197 
199  void CopyFromAnded(
200  const ScBitMaskCompressedArray& rArray,
201  A nStart, A nEnd, const D& rValueToAnd );
202 
206  A GetLastAnyBitAccess( const D& rBitMask ) const;
207 };
208 
209 template< typename A, typename D >
210 void ScBitMaskCompressedArray<A,D>::AndValue( A nPos, const D& rValueToAnd )
211 {
212  const D& rValue = this->GetValue( nPos);
213  if ((rValue & rValueToAnd) != rValue)
214  this->SetValue( nPos, rValue & rValueToAnd);
215 }
216 
217 template< typename A, typename D >
218 void ScBitMaskCompressedArray<A,D>::OrValue( A nPos, const D& rValueToOr )
219 {
220  const D& rValue = this->GetValue( nPos);
221  if ((rValue | rValueToOr) != rValue)
222  this->SetValue( nPos, rValue | rValueToOr);
223 }
224 
225 #endif // INCLUDED_SC_INC_COMPRESSEDARRAY_HXX
226 
227 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const D & GetNextValue(size_t &nIndex, A &nEnd) const
Get next value and it's region end row.
sal_Int32 nIndex
const D & operator*() const
void CopyFromAnded(const ScBitMaskCompressedArray &rArray, A nStart, A nEnd, const D &rValueToAnd)
Copy values from rArray and bitwise AND them with rValueToAnd.
void Remove(A nStart, size_t nCount)
const char aData[]
std::unique_ptr< ContentProperties > pData
std::string GetValue
Iterator begin() const
void AndValue(A nPos, const D &rValueToAnd)
void InsertPreservingSize(A nStart, size_t nCount, const D &rFillValue)
virtual ~ScCompressedArray()
A GetLastAnyBitAccess(const D &rBitMask) const
Return the last row where an entry meets the condition: ((aValue & rBitMask) != 0), start searching at 0.
const sal_uInt8 A
Definition: xlformula.cxx:52
css::uno::Any const & rValue
int nCount
SC_DLLPUBLIC size_t Search(A nPos) const
Obtain index into entries for nPos.
const ScCompressedArray & mrArray
void SetValue(A nPos, const D &rValue)
void RemovePreservingSize(A nStart, size_t nCount, const D &rFillValue)
ScCompressedArray(A nMaxAccess, const D &rValue)
Construct with nMaxAccess=MAXROW, for example.
const D & GetValue(A nPos) const
Iterator operator+(size_t) const
void CopyFrom(const ScCompressedArray &rArray, A nStart, A nEnd)
Copy rArray.nStart+nSourceDy to this.nStart.
Iterator(const ScCompressedArray &rArray, size_t nIndex, A nRegion)
void Reset(const D &rValue)
ScBitMaskCompressedArray(A nMaxAccessP, const D &rValue)
RangeData GetRangeData(A nPos) const
Get range data for a row, i.e.
Compressed array of row (or column) entries, e.g.
The data type represents bits, manageable by bitwise operations.
void OrValue(A nPos, const D &rValueToOr)
const D & Insert(A nStart, size_t nCount)
Insert rows before nStart and copy value for inserted rows from nStart-1, return that value...
#define SC_DLLPUBLIC
Definition: scdllapi.h:28
Iterator(const ScCompressedArray &rArray)
std::unique_ptr< DataEntry[]> pData