summaryrefslogtreecommitdiffstats
path: root/src/libixion/address.cpp
blob: 2b9b82cbb97d95fa23e3b0d9e358c1508827a261 (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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * 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 "ixion/address.hpp"

#include <sstream>
#include <limits>

namespace ixion {

static const row_t row_max = std::numeric_limits<row_t>::max();
static const col_t column_max = std::numeric_limits<col_t>::max();

const row_t row_unset = row_max - 9;
const row_t row_upper_bound = row_max - 10;

const col_t column_unset = column_max - 9;
const col_t column_upper_bound = column_max / 26 - 26;

abs_address_t::abs_address_t() : sheet(0), row(0), column(0) {}
abs_address_t::abs_address_t(init_invalid) : sheet(-1), row(-1), column(-1) {}

abs_address_t::abs_address_t(sheet_t _sheet, row_t _row, col_t _column) :
    sheet(_sheet), row(_row), column(_column) {}

abs_address_t::abs_address_t(const abs_address_t& r) :
    sheet(r.sheet), row(r.row), column(r.column) {}

bool abs_address_t::valid() const
{
    return sheet >= 0 && row >= 0 && column >= 0 && row <= row_unset && column <= column_unset;
}

std::string abs_address_t::get_name() const
{
    std::ostringstream os;
    os << "(sheet=" << sheet << "; row=" << row << "; column=" << column << ")";
    return os.str();
}

std::size_t abs_address_t::hash::operator()(const abs_address_t& addr) const
{
    return addr.sheet + addr.row + addr.column;
}

bool operator== (const abs_address_t& left, const abs_address_t& right)
{
    return left.sheet == right.sheet &&
        left.row == right.row &&
        left.column == right.column;
}

bool operator!= (const abs_address_t& left, const abs_address_t& right)
{
    return !operator==(left, right);
}

bool operator< (const abs_address_t& left, const abs_address_t& right)
{
    if (left.sheet != right.sheet)
        return left.sheet < right.sheet;

    if (left.row != right.row)
        return left.row < right.row;

    return left.column < right.column;
}

address_t::address_t() :
    sheet(0), row(0), column(0), abs_sheet(true), abs_row(true), abs_column(true) {}

address_t::address_t(sheet_t _sheet, row_t _row, col_t _column, bool _abs_sheet, bool _abs_row, bool _abs_column) :
    sheet(_sheet), row(_row), column(_column),
    abs_sheet(_abs_sheet), abs_row(_abs_row), abs_column(_abs_column) {}

address_t::address_t(const address_t& r) :
    sheet(r.sheet), row(r.row), column(r.column),
    abs_sheet(r.abs_sheet), abs_row(r.abs_row), abs_column(r.abs_column) {}

address_t::address_t(const abs_address_t& r) :
    sheet(r.sheet), row(r.row), column(r.column),
    abs_sheet(true), abs_row(true), abs_column(true) {}

bool address_t::valid() const
{
    if (abs_sheet)
    {
        if (sheet < 0)
            return false;
    }

    if (row > row_unset)
        return false;

    if (abs_row)
    {
        if (row < 0)
            return false;
    }
    else
    {
        if (row < -row_upper_bound)
            return false;
    }

    if (column > column_unset)
        return false;

    if (abs_column)
    {
        if (column < 0)
            return false;
    }
    else
    {
        if (column < -column_upper_bound)
            return false;
    }

    return true;
}

abs_address_t address_t::to_abs(const abs_address_t& origin) const
{
    abs_address_t abs_addr;
    abs_addr.sheet = sheet;
    abs_addr.row = row;
    abs_addr.column = column;

    if (!is_valid_sheet(origin.sheet))
        // If the origin sheet is invalid, then any sheet position relative to that should be invalid.
        abs_addr.sheet = origin.sheet;
    else if (!abs_sheet)
        abs_addr.sheet += origin.sheet;

    if (!abs_row && row <= row_upper_bound)
        abs_addr.row += origin.row;

    if (!abs_column && column <= column_upper_bound)
        abs_addr.column += origin.column;

    return abs_addr;
}

std::string address_t::get_name() const
{
    std::ostringstream os;
    os << "(row=" << row << " [";
    if (abs_row)
        os << "abs";
    else
        os << "rel";
    os << "]; column=" << column << " [";
    if (abs_column)
        os << "abs";
    else
        os << "rel";
    os << "])";
    return os.str();
}

void address_t::set_absolute(bool abs)
{
    abs_sheet = abs;
    abs_row = abs;
    abs_column = abs;
}

std::size_t address_t::hash::operator()(const address_t& addr) const
{
    return 0;
}

bool operator== (const address_t& left, const address_t& right)
{
    return left.sheet == right.sheet &&
        left.row == right.row &&
        left.column == right.column &&
        left.abs_sheet == right.abs_sheet &&
        left.abs_row == right.abs_row &&
        left.abs_column == right.abs_column;
}

bool operator!=(const address_t& left, const address_t& right)
{
    return !operator==(left, right);
}

bool operator< (const address_t& left, const address_t& right)
{
    // Not sure how to compare absolute and relative addresses, but let's make
    // absolute address always greater than relative one until we find a
    // better way.

    if (left.abs_sheet != right.abs_sheet)
        return left.abs_sheet < right.abs_sheet;

    if (left.abs_row != right.abs_row)
        return left.abs_row < right.abs_row;

    if (left.abs_column != right.abs_column)
        return left.abs_column < right.abs_column;

    if (left.sheet != right.sheet)
        return left.sheet < right.sheet;

    if (left.row != right.row)
        return left.row < right.row;

    return left.column < right.column;
}

abs_rc_address_t::abs_rc_address_t() : row(0), column(0)
{
}

abs_rc_address_t::abs_rc_address_t(init_invalid) :
    row(-1), column(-1) {}

abs_rc_address_t::abs_rc_address_t(row_t _row, col_t _column) :
    row(_row), column(_column) {}

abs_rc_address_t::abs_rc_address_t(const abs_rc_address_t& r) :
    row(r.row), column(r.column) {}

abs_rc_address_t::abs_rc_address_t(const abs_address_t& r) :
    row(r.row), column(r.column) {}

bool abs_rc_address_t::valid() const
{
    return row >= 0 && column >= 0 && row <= row_unset && column <= column_unset;
}

std::size_t abs_rc_address_t::hash::operator() (const abs_rc_address_t& addr) const
{
    size_t hv = addr.column;
    hv <<= 16;
    hv += addr.row;
    return hv;
}

bool operator== (const abs_rc_address_t& left, const abs_rc_address_t& right)
{
    return left.row == right.row && left.column == right.column;
}

bool operator!= (const abs_rc_address_t& left, const abs_rc_address_t& right)
{
    return !operator==(left, right);
}

bool operator< (const abs_rc_address_t& left, const abs_rc_address_t& right)
{
    if (left.row != right.row)
        return left.row < right.row;

    return left.column < right.column;
}

rc_address_t::rc_address_t() :
    row(0), column(0), abs_row(true), abs_column(true) {}

rc_address_t::rc_address_t(row_t _row, col_t _column, bool _abs_row, bool _abs_column) :
    row(_row), column(_column), abs_row(_abs_row), abs_column(_abs_column) {}

rc_address_t::rc_address_t(const rc_address_t& r) :
    row(r.row), column(r.column), abs_row(r.abs_row), abs_column(r.abs_column) {}

std::size_t rc_address_t::hash::operator()(const rc_address_t& addr) const
{
    std::size_t hv = addr.column;
    hv <<= 16;
    hv += addr.row;
    return hv;
}

abs_range_t::abs_range_t() {}
abs_range_t::abs_range_t(init_invalid) :
    first(abs_address_t::invalid), last(abs_address_t::invalid) {}

abs_range_t::abs_range_t(sheet_t _sheet, row_t _row, col_t _col) :
    first(_sheet, _row, _col), last(_sheet, _row, _col) {}

abs_range_t::abs_range_t(sheet_t _sheet, row_t _row, col_t _col, row_t _row_span, col_t _col_span) :
    first(_sheet, _row, _col), last(_sheet, _row + _row_span - 1, _col + _col_span - 1)
{
    if (_row_span < 1 || _col_span < 1)
    {
        std::ostringstream os;
        os << "abs_range_t: invalid span (row=" << _row_span << "; col=" << _col_span << ")";
        throw std::range_error(os.str());
    }
}

abs_range_t::abs_range_t(const abs_address_t& addr, row_t row_span, col_t col_span) :
    first(addr), last(addr)
{
    if (row_span > 0)
        last.row += row_span - 1;
    if (col_span > 0)
        last.column += col_span - 1;
}

abs_range_t::abs_range_t(const abs_address_t& addr) :
    first(addr), last(addr) {}

std::size_t abs_range_t::hash::operator() (const abs_range_t& range) const
{
    abs_address_t::hash adr_hash;
    return adr_hash(range.first) + 65536*adr_hash(range.last);
}

bool abs_range_t::valid() const
{
    return first.valid() && last.valid() &&
        first.sheet <= last.sheet &&
        first.column <= last.column &&
        first.row <= last.row;
}

void abs_range_t::set_all_columns()
{
    first.column = column_unset;
    last.column = column_unset;
}

void abs_range_t::set_all_rows()
{
    first.row = row_unset;
    last.row = row_unset;
}

bool abs_range_t::all_columns() const
{
    return first.column == column_unset && last.column == column_unset;
}

bool abs_range_t::all_rows() const
{
    return first.row == row_unset && last.row == row_unset;
}

bool abs_range_t::contains(const abs_address_t& addr) const
{
    return first.sheet <= addr.sheet && addr.sheet <= last.sheet &&
        first.row <= addr.row && addr.row <= last.row &&
        first.column <= addr.column && addr.column <= last.column;
}

void abs_range_t::reorder()
{
    if (first.sheet > last.sheet)
        std::swap(first.sheet, last.sheet);

    if (first.row > last.row)
        std::swap(first.row, last.row);

    if (first.column > last.column)
        std::swap(first.column, last.column);
}

bool operator==(const abs_range_t& left, const abs_range_t& right)
{
    return left.first == right.first && left.last == right.last;
}

bool operator!=(const abs_range_t& left, const abs_range_t& right)
{
    return !operator==(left, right);
}

bool operator<(const abs_range_t& left, const abs_range_t& right)
{
    if (left.first != right.first)
        return left.first < right.first;
    return left.last < right.last;
}

abs_rc_range_t::abs_rc_range_t() {}
abs_rc_range_t::abs_rc_range_t(init_invalid) :
    first(abs_rc_address_t::invalid), last(abs_rc_address_t::invalid) {}

abs_rc_range_t::abs_rc_range_t(const abs_rc_range_t& other) :
    first(other.first), last(other.last) {}

abs_rc_range_t::abs_rc_range_t(const abs_range_t& other) :
    first(other.first), last(other.last) {}

size_t abs_rc_range_t::hash::operator() (const abs_rc_range_t& range) const
{
    abs_rc_address_t::hash adr_hash;
    return adr_hash(range.first) + 65536*adr_hash(range.last);
}

bool abs_rc_range_t::valid() const
{
    if (!first.valid() || !last.valid())
        return false;

    if (first.row != row_unset && last.row != row_unset)
    {
        if (first.row > last.row)
            return false;
    }

    if (first.column != column_unset && last.column != column_unset)
    {
        if (first.column > last.column)
            return false;
    }

    return true;
}

void abs_rc_range_t::set_all_columns()
{
    first.column = column_unset;
    last.column = column_unset;
}

void abs_rc_range_t::set_all_rows()
{
    first.row = row_unset;
    last.row = row_unset;
}

bool abs_rc_range_t::all_columns() const
{
    return first.column == column_unset && last.column == column_unset;
}

bool abs_rc_range_t::all_rows() const
{
    return first.row == row_unset && last.row == row_unset;
}

bool abs_rc_range_t::contains(const abs_rc_address_t& addr) const
{
    return first.row <= addr.row && addr.row <= last.row &&
        first.column <= addr.column && addr.column <= last.column;
}

bool operator==(const abs_rc_range_t& left, const abs_rc_range_t& right)
{
    return left.first == right.first && left.last == right.last;
}

bool operator!=(const abs_rc_range_t& left, const abs_rc_range_t& right)
{
    return !operator==(left, right);
}

bool operator<(const abs_rc_range_t& left, const abs_rc_range_t& right)
{
    if (left.first != right.first)
        return left.first < right.first;
    return left.last < right.last;
}

range_t::range_t() {}
range_t::range_t(const address_t& _first, const address_t& _last) :
    first(_first), last(_last) {}

range_t::range_t(const range_t& r) : first(r.first), last(r.last) {}
range_t::range_t(const abs_range_t& r) : first(r.first), last(r.last) {}

bool range_t::valid() const
{
    return first.valid() && last.valid();
}

void range_t::set_all_columns()
{
    first.column = column_unset;
    last.column = column_unset;
}

void range_t::set_all_rows()
{
    first.row = row_unset;
    last.row = row_unset;
}

bool range_t::all_columns() const
{
    return first.column == column_unset && last.column == column_unset;
}

bool range_t::all_rows() const
{
    return first.row == row_unset && last.row == row_unset;
}

abs_range_t range_t::to_abs(const abs_address_t& origin) const
{
    abs_range_t ret;
    ret.first = first.to_abs(origin);
    ret.last = last.to_abs(origin);
    return ret;
}

void range_t::set_absolute(bool abs)
{
    first.set_absolute(abs);
    last.set_absolute(abs);
}

std::size_t range_t::hash::operator() (const range_t& range) const
{
    address_t::hash adr_hash;
    return adr_hash(range.first) + 65536*adr_hash(range.last);
}

bool operator==(const range_t& left, const range_t& right)
{
    return left.first == right.first && left.last == right.last;
}

bool operator!=(const range_t& left, const range_t& right)
{
    return !operator==(left, right);
}

std::ostream& operator<<(std::ostream& os, const abs_address_t& addr)
{
    os << "(sheet:" << addr.sheet << "; row:" << addr.row << "; column:" << addr.column << ")";
    return os;
}

std::ostream& operator<<(std::ostream& os, const abs_rc_address_t& addr)
{
    os << "(row:" << addr.row << "; column:" << addr.column << ")";
    return os;
}

std::ostream& operator<<(std::ostream& os, const address_t& addr)
{
    os << "(sheet:" << addr.sheet << " " << (addr.abs_sheet?"abs":"rel")
        << "; row:" << addr.row << " " << (addr.abs_row?"abs":"rel")
        <<"; column:" << addr.column << " " << (addr.abs_column?"abs":"rel") << ")";
    return os;
}

std::ostream& operator<<(std::ostream& os, const abs_range_t& range)
{
    os << range.first << "-" << range.last;
    return os;
}

std::ostream& operator<<(std::ostream& os, const abs_rc_range_t& range)
{
    os << range.first << "-" << range.last;
    return os;
}

std::ostream& operator<<(std::ostream& os, const range_t& range)
{
    os << range.first << "-" << range.last;
    return os;
}

}

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