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 #pragma once
21 
22 #include <cstddef>
23 #include <memory>
24 
25 #include "scdllapi.h"
26 
45 template< typename A, typename D > class ScCompressedArray
46 {
47 public:
48  class Iterator
49  {
52  size_t mnIndex = 0;
53  A mnRegion = 0;
54  Iterator(const ScCompressedArray& rArray) : mrArray(rArray) {}
55  Iterator(const ScCompressedArray& rArray, size_t nIndex, A nRegion) : mrArray(rArray), mnIndex(nIndex), mnRegion(nRegion) {}
56  public:
57  void operator++();
58  Iterator operator+(size_t) const;
59  const D & operator*() const { return mrArray.pData[mnIndex].aValue; }
60  };
61  struct DataEntry
62  {
63  A nEnd; // start is end of previous entry + 1
65  DataEntry() {}
66  };
67  struct RangeData
68  {
71  };
72 
75  const D& rValue );
76  virtual ~ScCompressedArray();
77  void Reset( const D& rValue );
78  void SetValue( A nPos, const D& rValue );
79  void SetValue( A nStart, A nEnd, const D& rValue );
80  [[nodiscard]]
81  const D& GetValue( A nPos ) const;
82  [[nodiscard]]
83  A GetLastPos() const { return pData[nCount-1].nEnd; }
84 
86  [[nodiscard]]
87  const D& GetValue( A nPos, size_t& nIndex, A& nEnd ) const;
88 
90  [[nodiscard]]
91  RangeData GetRangeData( A nPos ) const;
92 
96  [[nodiscard]]
97  const D& GetNextValue( size_t& nIndex, A& nEnd ) const;
98 
101  const D& Insert( A nStart, size_t nCount );
102  void InsertPreservingSize( A nStart, size_t nCount, const D& rFillValue );
103 
104  void Remove( A nStart, size_t nCount );
105  void RemovePreservingSize( A nStart, size_t nCount, const D& rFillValue );
106 
108  void CopyFrom( const ScCompressedArray& rArray,
109  A nStart, A nEnd )
110  { CopyFrom(rArray, nStart, nEnd, nStart); }
111  void CopyFrom( const ScCompressedArray& rArray,
112  A nDestStart, A nDestEnd, A nSrcStart );
113 
114  // methods public for the coupled array sum methods
116  SC_DLLPUBLIC size_t Search( A nPos ) const;
117 
118  Iterator begin() const { return Iterator(*this); }
119 
120 protected:
121  size_t nCount;
122  size_t nLimit;
123  std::unique_ptr<DataEntry[]> pData;
125 };
126 
127 template< typename A, typename D >
128 void ScCompressedArray<A,D>::Reset( const D& rValue )
129 {
130  // Create a temporary copy in case we got a reference passed that points to
131  // a part of the array to be reallocated.
132  D aTmpVal( rValue);
133  nCount = nLimit = 1;
134  pData.reset(new DataEntry[1]);
135  pData[0].aValue = aTmpVal;
136  pData[0].nEnd = nMaxAccess;
137 }
138 
139 template< typename A, typename D >
140 void ScCompressedArray<A,D>::SetValue( A nPos, const D& rValue )
141 {
142  SetValue( nPos, nPos, rValue);
143 }
144 
145 template< typename A, typename D >
147 {
148  size_t nIndex = Search( nPos);
149  return pData[nIndex].aValue;
150 }
151 
152 template< typename A, typename D >
153 const D& ScCompressedArray<A,D>::GetValue( A nPos, size_t& nIndex, A& nEnd ) const
154 {
155  nIndex = Search( nPos);
156  nEnd = pData[nIndex].nEnd;
157  return pData[nIndex].aValue;
158 }
159 
160 template< typename A, typename D >
162 {
163  size_t nIndex = Search( nPos);
165  aData.mnRow1 = nIndex == 0 ? 0 : pData[nIndex - 1].nEnd + 1;
166  aData.mnRow2 = pData[nIndex].nEnd;
167  aData.maValue = pData[nIndex].aValue;
168  return aData;
169 }
170 
171 template< typename A, typename D >
172 const D& ScCompressedArray<A,D>::GetNextValue( size_t& nIndex, A& nEnd ) const
173 {
174  if (nIndex < nCount)
175  ++nIndex;
176  size_t nEntry = (nIndex < nCount ? nIndex : nCount-1);
177  nEnd = pData[nEntry].nEnd;
178  return pData[nEntry].aValue;
179 }
180 
181 // ScBitMaskCompressedArray
185 template< typename A, typename D > class ScBitMaskCompressedArray final : public ScCompressedArray<A,D>
186 {
187 public:
188  ScBitMaskCompressedArray( A nMaxAccessP,
189  const D& rValue )
190  : ScCompressedArray<A,D>( nMaxAccessP, rValue )
191  {}
192  void AndValue( A nPos, const D& rValueToAnd );
193  void OrValue( A nPos, const D& rValueToOr );
194  void AndValue( A nStart, A nEnd, const D& rValueToAnd );
195  void OrValue( A nStart, A nEnd, const D& rValueToOr );
196 
198  void CopyFromAnded(
199  const ScBitMaskCompressedArray& rArray,
200  A nStart, A nEnd, const D& rValueToAnd );
201 
205  A GetLastAnyBitAccess( const D& rBitMask ) const;
206 };
207 
208 template< typename A, typename D >
209 void ScBitMaskCompressedArray<A,D>::AndValue( A nPos, const D& rValueToAnd )
210 {
211  const D& rValue = this->GetValue( nPos);
212  if ((rValue & rValueToAnd) != rValue)
213  this->SetValue( nPos, rValue & rValueToAnd);
214 }
215 
216 template< typename A, typename D >
217 void ScBitMaskCompressedArray<A,D>::OrValue( A nPos, const D& rValueToOr )
218 {
219  const D& rValue = this->GetValue( nPos);
220  if ((rValue | rValueToOr) != rValue)
221  this->SetValue( nPos, rValue | rValueToOr);
222 }
223 
224 /* 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)
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.
constexpr OUStringLiteral aData
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:27
Iterator(const ScCompressedArray &rArray)
std::unique_ptr< DataEntry[]> pData