summaryrefslogtreecommitdiff
path: root/sc/source/core/data/cellvalues.cxx
blob: 290dc0d091a957dbaa07292ee17fb966e31e3d72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <memory>
#include <cellvalues.hxx>
#include <column.hxx>
#include <formulacell.hxx>

#include <cassert>

namespace sc {

namespace {

struct BlockPos
{
    size_t mnStart;
    size_t mnEnd;
};

}

CellValueSpan::CellValueSpan( SCROW nRow1, SCROW nRow2 ) :
    mnRow1(nRow1), mnRow2(nRow2) {}

struct CellValuesImpl
{
    CellStoreType maCells;
    CellTextAttrStoreType maCellTextAttrs;
    CellStoreType::iterator miCellPos;
    CellTextAttrStoreType::iterator miAttrPos;

    CellValuesImpl() = default;

    CellValuesImpl(const CellValuesImpl&) = delete;
    const CellValuesImpl& operator=(const CellValuesImpl&) = delete;
};

CellValues::CellValues() :
    mpImpl(new CellValuesImpl) {}

CellValues::~CellValues()
{
}

void CellValues::transferFrom( ScColumn& rCol, SCROW nRow, size_t nLen )
{
    mpImpl->maCells.resize(nLen);
    mpImpl->maCellTextAttrs.resize(nLen);
    rCol.maCells.transfer(nRow, nRow+nLen-1, mpImpl->maCells, 0);
    rCol.maCellTextAttrs.transfer(nRow, nRow+nLen-1, mpImpl->maCellTextAttrs, 0);
}


void CellValues::copyTo( ScColumn& rCol, SCROW nRow ) const
{
    copyCellsTo(rCol, nRow);
    copyCellTextAttrsTo(rCol, nRow);
}

void CellValues::swapNonEmpty( ScColumn& rCol )
{
    std::vector<BlockPos> aBlocksToSwap;

    // Go through static value blocks and record their positions and sizes.
    for (const auto& rCell : mpImpl->maCells)
    {
        if (rCell.type == sc::element_type_empty)
            continue;

        BlockPos aPos;
        aPos.mnStart = rCell.position;
        aPos.mnEnd = aPos.mnStart + rCell.size - 1;
        aBlocksToSwap.push_back(aPos);
    }

    // Do the swapping.  The undo storage will store the replaced formula cells after this.
    for (const auto& rBlock : aBlocksToSwap)
    {
        rCol.maCells.swap(rBlock.mnStart, rBlock.mnEnd, mpImpl->maCells, rBlock.mnStart);
        rCol.maCellTextAttrs.swap(rBlock.mnStart, rBlock.mnEnd, mpImpl->maCellTextAttrs, rBlock.mnStart);
    }
}

void CellValues::assign( const std::vector<double>& rVals )
{
    mpImpl->maCells.resize(rVals.size());
    mpImpl->maCells.set(0, rVals.begin(), rVals.end());

    // Set default text attributes.
    std::vector<CellTextAttr> aDefaults(rVals.size(), CellTextAttr());
    mpImpl->maCellTextAttrs.resize(rVals.size());
    mpImpl->maCellTextAttrs.set(0, aDefaults.begin(), aDefaults.end());
}

void CellValues::assign( const std::vector<ScFormulaCell*>& rVals )
{
    std::vector<ScFormulaCell*> aCopyVals(rVals.size());
    size_t nIdx = 0;
    for (const auto* pCell : rVals)
    {
        aCopyVals[nIdx] = pCell->Clone();
        ++nIdx;
    }

    mpImpl->maCells.resize(aCopyVals.size());
    mpImpl->maCells.set(0, aCopyVals.begin(), aCopyVals.end());

    // Set default text attributes.
    std::vector<CellTextAttr> aDefaults(rVals.size(), CellTextAttr());
    mpImpl->maCellTextAttrs.resize(rVals.size());
    mpImpl->maCellTextAttrs.set(0, aDefaults.begin(), aDefaults.end());
}

size_t CellValues::size() const
{
    assert(mpImpl->maCells.size() == mpImpl->maCellTextAttrs.size());
    return mpImpl->maCells.size();
}

void CellValues::reset( size_t nSize )
{
    mpImpl->maCells.clear();
    mpImpl->maCells.resize(nSize);
    mpImpl->maCellTextAttrs.clear();
    mpImpl->maCellTextAttrs.resize(nSize);

    mpImpl->miCellPos = mpImpl->maCells.begin();
    mpImpl->miAttrPos = mpImpl->maCellTextAttrs.begin();
}

void CellValues::setValue( size_t nRow, double fVal )
{
    mpImpl->miCellPos = mpImpl->maCells.set(mpImpl->miCellPos, nRow, fVal);
    mpImpl->miAttrPos = mpImpl->maCellTextAttrs.set(mpImpl->miAttrPos, nRow, sc::CellTextAttr());
}

void CellValues::setValue( size_t nRow, const svl::SharedString& rStr )
{
    mpImpl->miCellPos = mpImpl->maCells.set(mpImpl->miCellPos, nRow, rStr);
    mpImpl->miAttrPos = mpImpl->maCellTextAttrs.set(mpImpl->miAttrPos, nRow, sc::CellTextAttr());
}

void CellValues::swap( CellValues& r )
{
    std::swap(mpImpl, r.mpImpl);
}

std::vector<CellValueSpan> CellValues::getNonEmptySpans() const
{
    std::vector<CellValueSpan> aRet;
    for (const auto& rCell : mpImpl->maCells)
    {
        if (rCell.type != element_type_empty)
        {
            // Record this span.
            size_t nRow1 = rCell.position;
            size_t nRow2 = nRow1 + rCell.size - 1;
            aRet.emplace_back(nRow1, nRow2);
        }
    }
    return aRet;
}

void CellValues::copyCellsTo( ScColumn& rCol, SCROW nRow ) const
{
    CellStoreType& rDest = rCol.maCells;
    const CellStoreType& rSrc = mpImpl->maCells;

    // Caller must ensure the destination is long enough.
    assert(rSrc.size() + static_cast<size_t>(nRow) <= rDest.size());

    SCROW nCurRow = nRow;
    CellStoreType::iterator itPos = rDest.begin();

    for (const auto& rBlk : rSrc)
    {
        switch (rBlk.type)
        {
            case element_type_numeric:
            {
                numeric_block::const_iterator it = numeric_block::begin(*rBlk.data);
                numeric_block::const_iterator itEnd = numeric_block::end(*rBlk.data);
                itPos = rDest.set(itPos, nCurRow, it, itEnd);
            }
            break;
            case element_type_string:
            {
                string_block::const_iterator it = string_block::begin(*rBlk.data);
                string_block::const_iterator itEnd = string_block::end(*rBlk.data);
                itPos = rDest.set(itPos, nCurRow, it, itEnd);
            }
            break;
            case element_type_edittext:
            {
                edittext_block::const_iterator it = edittext_block::begin(*rBlk.data);
                edittext_block::const_iterator itEnd = edittext_block::end(*rBlk.data);
                std::vector<EditTextObject*> aVals;
                aVals.reserve(rBlk.size);
                for (; it != itEnd; ++it)
                {
                    const EditTextObject* p = *it;
                    aVals.push_back(p->Clone().release());
                }
                itPos = rDest.set(itPos, nCurRow, aVals.begin(), aVals.end());
            }
            break;
            case element_type_formula:
            {
                formula_block::const_iterator it = formula_block::begin(*rBlk.data);
                formula_block::const_iterator itEnd = formula_block::end(*rBlk.data);
                std::vector<ScFormulaCell*> aVals;
                aVals.reserve(rBlk.size);
                for (; it != itEnd; ++it)
                {
                    const ScFormulaCell* p = *it;
                    aVals.push_back(p->Clone());
                }
                itPos = rDest.set(itPos, nCurRow, aVals.begin(), aVals.end());
            }
            break;
            default:
                itPos = rDest.set_empty(itPos, nCurRow, nCurRow+rBlk.size-1);
        }

        nCurRow += rBlk.size;
    }
}

void CellValues::copyCellTextAttrsTo( ScColumn& rCol, SCROW nRow ) const
{
    CellTextAttrStoreType& rDest = rCol.maCellTextAttrs;
    const CellTextAttrStoreType& rSrc = mpImpl->maCellTextAttrs;

    // Caller must ensure the destination is long enough.
    assert(rSrc.size() + static_cast<size_t>(nRow) <= rDest.size());

    SCROW nCurRow = nRow;
    CellTextAttrStoreType::iterator itPos = rDest.begin();

    for (const auto& rBlk : rSrc)
    {
        switch (rBlk.type)
        {
            case element_type_celltextattr:
            {
                celltextattr_block::const_iterator it = celltextattr_block::begin(*rBlk.data);
                celltextattr_block::const_iterator itEnd = celltextattr_block::end(*rBlk.data);
                itPos = rDest.set(itPos, nCurRow, it, itEnd);
            }
            break;
            default:
                itPos = rDest.set_empty(itPos, nCurRow, nCurRow+rBlk.size-1);
        }

        nCurRow += rBlk.size;
    }
}

typedef std::vector<std::unique_ptr<CellValues>> TableType;
typedef std::vector<std::unique_ptr<TableType>> TablesType;

struct TableValues::Impl
{
    ScRange maRange;
    TablesType m_Tables;

    explicit Impl( const ScRange& rRange ) : maRange(rRange)
    {
        size_t nTabs = rRange.aEnd.Tab() - rRange.aStart.Tab() + 1;
        size_t nCols = rRange.aEnd.Col() - rRange.aStart.Col() + 1;

        for (size_t nTab = 0; nTab < nTabs; ++nTab)
        {
            m_Tables.push_back(std::make_unique<TableType>());
            std::unique_ptr<TableType>& rTab2 = m_Tables.back();
            for (size_t nCol = 0; nCol < nCols; ++nCol)
                rTab2->push_back(std::make_unique<CellValues>());
        }
    }

    CellValues* getCellValues( SCTAB nTab, SCCOL nCol )
    {
        if (nTab < maRange.aStart.Tab() || maRange.aEnd.Tab() < nTab)
            // sheet index out of bound.
            return nullptr;
        if (nCol < maRange.aStart.Col() || maRange.aEnd.Col() < nCol)
            // column index out of bound.
            return nullptr;
        size_t nTabOffset = nTab - maRange.aStart.Tab();
        if (nTabOffset >= m_Tables.size())
            return nullptr;
        std::unique_ptr<TableType>& rTab2 = m_Tables[nTab-maRange.aStart.Tab()];
        size_t nColOffset = nCol - maRange.aStart.Col();
        if (nColOffset >= rTab2->size())
            return nullptr;
        return &rTab2.get()[0][nColOffset].get()[0];
    }
};

TableValues::TableValues() :
    mpImpl(new Impl(ScRange(ScAddress::INITIALIZE_INVALID))) {}

TableValues::TableValues( const ScRange& rRange ) :
    mpImpl(new Impl(rRange)) {}

TableValues::~TableValues()
{
}

const ScRange& TableValues::getRange() const
{
    return mpImpl->maRange;
}

void TableValues::swap( SCTAB nTab, SCCOL nCol, CellValues& rColValue )
{
    CellValues* pCol = mpImpl->getCellValues(nTab, nCol);
    if (!pCol)
        return;

    pCol->swap(rColValue);
}

void TableValues::swapNonEmpty( SCTAB nTab, SCCOL nCol, ScColumn& rCol )
{
    CellValues* pCol = mpImpl->getCellValues(nTab, nCol);
    if (!pCol)
        return;

    pCol->swapNonEmpty(rCol);
}

std::vector<CellValueSpan> TableValues::getNonEmptySpans( SCTAB nTab, SCCOL nCol ) const
{
    std::vector<CellValueSpan> aRet;
    CellValues* pCol = mpImpl->getCellValues(nTab, nCol);
    if (pCol)
        aRet = pCol->getNonEmptySpans();

    return aRet;
}

void TableValues::swap( TableValues& rOther )
{
    std::swap(mpImpl, rOther.mpImpl);
}

}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */