summaryrefslogtreecommitdiff
path: root/sc/source/core/tool/sharedformula.cxx
blob: d9f3a2595f67c69a98861570c48ca12b7ea10023 (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
/* -*- 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 "sharedformula.hxx"
#include "calcmacros.hxx"
#include "tokenarray.hxx"

namespace sc {

void SharedFormulaUtil::splitFormulaCellGroup(const CellStoreType::position_type& aPos)
{
    SCROW nRow = aPos.first->position + aPos.second;

    if (aPos.first->type != sc::element_type_formula)
        // Not a formula cell block.
        return;

    if (aPos.second == 0)
        // Split position coincides with the block border. Nothing to do.
        return;

    sc::formula_block::iterator it = sc::formula_block::begin(*aPos.first->data);
    std::advance(it, aPos.second);
    ScFormulaCell& rTop = **it;
    if (!rTop.IsShared())
        // Not a shared formula.
        return;

    if (nRow == rTop.GetSharedTopRow())
        // Already the top cell of a shared group.
        return;

    ScFormulaCellGroupRef xGroup = rTop.GetCellGroup();

    ScFormulaCellGroupRef xGroup2(new ScFormulaCellGroup);
    xGroup2->mbInvariant = xGroup->mbInvariant;
    xGroup2->mnStart = nRow;
    xGroup2->mnLength = xGroup->mnStart + xGroup->mnLength - nRow;
    xGroup2->mpCode = xGroup->mpCode->Clone();

    xGroup->mnLength = nRow - xGroup->mnStart;

    // Apply the lower group object to the lower cells.
#if DEBUG_COLUMN_STORAGE
        if (xGroup2->mnStart + xGroup2->mnLength > aPos.first->position + aPos.first->size)
        {
            cerr << "ScColumn::SplitFormulaCellGroup: Shared formula region goes beyond the formula block. Not good." << endl;
            cerr.flush();
            abort();
        }
#endif
    sc::formula_block::iterator itEnd = it;
    std::advance(itEnd, xGroup2->mnLength);
    for (; it != itEnd; ++it)
    {
        ScFormulaCell& rCell = **it;
        rCell.SetCellGroup(xGroup2);
    }
}

void SharedFormulaUtil::splitFormulaCellGroups(CellStoreType& rCells, const std::vector<SCROW>& rBounds)
{
    // TODO: Implement this.
}

void SharedFormulaUtil::joinFormulaCells(const CellStoreType::position_type& rPos, ScFormulaCell& rCell1, ScFormulaCell& rCell2)
{
    ScFormulaCell::CompareState eState = rCell1.CompareByTokenArray(rCell2);
    if (eState == ScFormulaCell::NotEqual)
        return;

    SCROW nRow = rPos.first->position + rPos.second;

    // Formula tokens equal those of the previous formula cell.
    ScFormulaCellGroupRef xGroup1 = rCell1.GetCellGroup();
    ScFormulaCellGroupRef xGroup2 = rCell2.GetCellGroup();
    if (xGroup1)
    {
        if (xGroup2)
        {
            // Both cell 1 and cell 2 are shared. Merge them together.
            if (xGroup1.get() == xGroup2.get())
                // They belong to the same group.
                return;

            // Set the group object from cell 1 to all cells in group 2.
            xGroup1->mnLength += xGroup2->mnLength;
            size_t nOffset = rPos.second + 1; // position of cell 2
            for (size_t i = 0, n = xGroup2->mnLength; i < n; ++i)
            {
                ScFormulaCell& rCell = *sc::formula_block::at(*rPos.first->data, nOffset+i);
                rCell.SetCellGroup(xGroup1);
            }
        }
        else
        {
            // cell 1 is shared but cell 2 is not.
            rCell2.SetCellGroup(xGroup1);
            ++xGroup1->mnLength;
        }
    }
    else
    {
        if (xGroup2)
        {
            // cell 1 is not shared, but cell 2 is already shared.
            rCell1.SetCellGroup(xGroup2);
            xGroup2->mnStart = nRow;
            ++xGroup2->mnLength;
        }
        else
        {
            // neither cells are shared.
            xGroup1 = rCell1.CreateCellGroup(nRow, 2, eState == ScFormulaCell::EqualInvariant);
            rCell2.SetCellGroup(xGroup1);
        }
    }
}

void SharedFormulaUtil::joinFormulaCellAbove(const CellStoreType::position_type& aPos)
{
    if (aPos.first->type != sc::element_type_formula)
        // This is not a formula cell.
        return;

    if (aPos.second == 0)
        // This cell is already the top cell in a formula block; the previous
        // cell is not a formula cell.
        return;

    ScFormulaCell& rPrev = *sc::formula_block::at(*aPos.first->data, aPos.second-1);
    ScFormulaCell& rCell = *sc::formula_block::at(*aPos.first->data, aPos.second);
    sc::CellStoreType::position_type aPosPrev = aPos;
    --aPosPrev.second;
    joinFormulaCells(aPosPrev, rPrev, rCell);
}

void SharedFormulaUtil::unshareFormulaCell(const CellStoreType::position_type& aPos, ScFormulaCell& rCell)
{
    if (!rCell.IsShared())
        return;

    ScFormulaCellGroupRef xNone;
    sc::CellStoreType::iterator it = aPos.first;

    // This formula cell is shared. Adjust the shared group.
    if (rCell.aPos.Row() == rCell.GetSharedTopRow())
    {
        // Top of the shared range.
        ScFormulaCellGroupRef xGroup = rCell.GetCellGroup();
        if (xGroup->mnLength == 2)
        {
            // Group consists only only two cells. Mark the second one non-shared.
#if DEBUG_COLUMN_STORAGE
            if (aPos.second+1 >= aPos.first->size)
            {
                cerr << "ScColumn::UnshareFormulaCell: There is no next formula cell but there should be!" << endl;
                cerr.flush();
                abort();
            }
#endif
            ScFormulaCell& rNext = *sc::formula_block::at(*it->data, aPos.second+1);
            rNext.SetCellGroup(xNone);
        }
        else
        {
            // Move the top cell to the next formula cell down.
            --xGroup->mnLength;
            ++xGroup->mnStart;
        }
    }
    else if (rCell.aPos.Row() == rCell.GetSharedTopRow() + rCell.GetSharedLength() - 1)
    {
        // Bottom of the shared range.
        ScFormulaCellGroupRef xGroup = rCell.GetCellGroup();
        if (xGroup->mnLength == 2)
        {
            // Mark the top cell non-shared.
#if DEBUG_COLUMN_STORAGE
            if (aPos.second == 0)
            {
                cerr << "ScColumn::UnshareFormulaCell: There is no previous formula cell but there should be!" << endl;
                cerr.flush();
                abort();
            }
#endif
            ScFormulaCell& rPrev = *sc::formula_block::at(*it->data, aPos.second-1);
            rPrev.SetCellGroup(xNone);
        }
        else
        {
            // Just shortern the shared range length by one.
            --xGroup->mnLength;
        }
    }
    else
    {
        // In the middle of the shared range. Split it into two groups.
        ScFormulaCellGroupRef xGroup = rCell.GetCellGroup();
        SCROW nEndRow = xGroup->mnStart + xGroup->mnLength - 1;
        xGroup->mnLength = rCell.aPos.Row() - xGroup->mnStart; // Shorten the top group.
        if (xGroup->mnLength == 1)
        {
            // Make the top cell non-shared.
#if DEBUG_COLUMN_STORAGE
            if (aPos.second == 0)
            {
                cerr << "ScColumn::UnshareFormulaCell: There is no previous formula cell but there should be!" << endl;
                cerr.flush();
                abort();
            }
#endif
            ScFormulaCell& rPrev = *sc::formula_block::at(*it->data, aPos.second-1);
            rPrev.SetCellGroup(xNone);
        }

        SCROW nLength2 = nEndRow - rCell.aPos.Row();
        if (nLength2 >= 2)
        {
            ScFormulaCellGroupRef xGroup2;
            xGroup2.reset(new ScFormulaCellGroup);
            xGroup2->mnStart = rCell.aPos.Row() + 1;
            xGroup2->mnLength = nLength2;
            xGroup2->mbInvariant = xGroup->mbInvariant;
            xGroup2->mpCode = xGroup->mpCode->Clone();
#if DEBUG_COLUMN_STORAGE
            if (xGroup2->mnStart + xGroup2->mnLength > it->position + it->size)
            {
                cerr << "ScColumn::UnshareFormulaCell: Shared formula region goes beyond the formula block. Not good." << endl;
                cerr.flush();
                abort();
            }
#endif
            sc::formula_block::iterator itCell = sc::formula_block::begin(*it->data);
            std::advance(itCell, aPos.second+1);
            sc::formula_block::iterator itCellEnd = itCell;
            std::advance(itCellEnd, xGroup2->mnLength);
            for (; itCell != itCellEnd; ++itCell)
            {
                ScFormulaCell& rCell2 = **itCell;
                rCell2.SetCellGroup(xGroup2);
            }
        }
        else
        {
            // Make the next cell non-shared.
            sc::formula_block::iterator itCell = sc::formula_block::begin(*it->data);
            std::advance(itCell, aPos.second+1);
            ScFormulaCell& rCell2 = **itCell;
            rCell2.SetCellGroup(xNone);
        }
    }

    rCell.SetCellGroup(xNone);
}

}

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