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  };
66  struct RangeData
67  {
70  };
71 
74  const D& rValue );
75  virtual ~ScCompressedArray();
76  void Reset( const D& rValue );
77  void SetValue( A nPos, const D& rValue );
78  void SetValue( A nStart, A nEnd, const D& rValue );
79  [[nodiscard]]
80  const D& GetValue( A nPos ) const;
81  [[nodiscard]]
82  A GetLastPos() const { return pData[nCount-1].nEnd; }
83 
85  [[nodiscard]]
86  const D& GetValue( A nPos, size_t& nIndex, A& nEnd ) const;
87 
89  [[nodiscard]]
90  RangeData GetRangeData( A nPos ) const;
91 
95  [[nodiscard]]
96  const D& GetNextValue( size_t& nIndex, A& nEnd ) const;
97 
100  const D& Insert( A nStart, size_t nCount );
101  void InsertPreservingSize( A nStart, size_t nCount, const D& rFillValue );
102 
103  void Remove( A nStart, size_t nCount );
104  void RemovePreservingSize( A nStart, size_t nCount, const D& rFillValue );
105 
107  void CopyFrom( const ScCompressedArray& rArray,
108  A nStart, A nEnd )
109  { CopyFrom(rArray, nStart, nEnd, nStart); }
110  void CopyFrom( const ScCompressedArray& rArray,
111  A nDestStart, A nDestEnd, A nSrcStart );
112 
113  // methods public for the coupled array sum methods
115  SC_DLLPUBLIC size_t Search( A nPos ) const;
116 
117  Iterator begin() const { return Iterator(*this); }
118 
119 protected:
120  size_t nCount;
121  size_t nLimit;
122  std::unique_ptr<DataEntry[]> pData;
124 };
125 
126 template< typename A, typename D >
127 void ScCompressedArray<A,D>::Reset( const D& rValue )
128 {
129  // Create a temporary copy in case we got a reference passed that points to
130  // a part of the array to be reallocated.
131  D aTmpVal( rValue);
132  nCount = nLimit = 1;
133  pData.reset(new DataEntry[1]);
134  pData[0].aValue = aTmpVal;
135  pData[0].nEnd = nMaxAccess;
136 }
137 
138 template< typename A, typename D >
139 void ScCompressedArray<A,D>::SetValue( A nPos, const D& rValue )
140 {
141  SetValue( nPos, nPos, rValue);
142 }
143 
144 template< typename A, typename D >
146 {
147  size_t nIndex = Search( nPos);
148  return pData[nIndex].aValue;
149 }
150 
151 template< typename A, typename D >
152 const D& ScCompressedArray<A,D>::GetValue( A nPos, size_t& nIndex, A& nEnd ) const
153 {
154  nIndex = Search( nPos);
155  nEnd = pData[nIndex].nEnd;
156  return pData[nIndex].aValue;
157 }
158 
159 template< typename A, typename D >
161 {
162  size_t nIndex = Search( nPos);
164  aData.mnRow1 = nIndex == 0 ? 0 : pData[nIndex - 1].nEnd + 1;
165  aData.mnRow2 = pData[nIndex].nEnd;
166  aData.maValue = pData[nIndex].aValue;
167  return aData;
168 }
169 
170 template< typename A, typename D >
171 const D& ScCompressedArray<A,D>::GetNextValue( size_t& nIndex, A& nEnd ) const
172 {
173  if (nIndex < nCount)
174  ++nIndex;
175  size_t nEntry = (nIndex < nCount ? nIndex : nCount-1);
176  nEnd = pData[nEntry].nEnd;
177  return pData[nEntry].aValue;
178 }
179 
180 // ScBitMaskCompressedArray
184 template< typename A, typename D > class ScBitMaskCompressedArray final : public ScCompressedArray<A,D>
185 {
186 public:
187  ScBitMaskCompressedArray( A nMaxAccessP,
188  const D& rValue )
189  : ScCompressedArray<A,D>( nMaxAccessP, rValue )
190  {}
191  void AndValue( A nPos, const D& rValueToAnd );
192  void OrValue( A nPos, const D& rValueToOr );
193  void AndValue( A nStart, A nEnd, const D& rValueToAnd );
194  void OrValue( A nStart, A nEnd, const D& rValueToOr );
195 
197  void CopyFromAnded(
198  const ScBitMaskCompressedArray& rArray,
199  A nStart, A nEnd, const D& rValueToAnd );
200 
204  A GetLastAnyBitAccess( const D& rBitMask ) const;
205 };
206 
207 template< typename A, typename D >
208 void ScBitMaskCompressedArray<A,D>::AndValue( A nPos, const D& rValueToAnd )
209 {
210  const D& rValue = this->GetValue( nPos);
211  if ((rValue & rValueToAnd) != rValue)
212  this->SetValue( nPos, rValue & rValueToAnd);
213 }
214 
215 template< typename A, typename D >
216 void ScBitMaskCompressedArray<A,D>::OrValue( A nPos, const D& rValueToOr )
217 {
218  const D& rValue = this->GetValue( nPos);
219  if ((rValue | rValueToOr) != rValue)
220  this->SetValue( nPos, rValue | rValueToOr);
221 }
222 
223 /* 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::string GetValue
std::unique_ptr< sal_Int32[]> pData
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
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
virtual void SetValue(tools::Long nNew) override