Skip to content

Table Cache

Cache classes for Table and Row.

Classes:

Name Description
RowCache

Cache for Row (internal).

TableCache

Cache for Table (internal).

_XP_CELL_IDX module-attribute

_XP_CELL_IDX = xpath_compile(
    "(table:table-cell|table:covered-table-cell)[$idx]"
)

_XP_COLUMN_IDX module-attribute

_XP_COLUMN_IDX = xpath_compile(
    "(table:table-column|table:table-columns/table:table-column|table:table-header-columns/table:table-column)[$idx]"
)

_XP_ROW_IDX module-attribute

_XP_ROW_IDX = xpath_compile(
    "(table:table-row|table:table-rows/table:table-row|table:table-header-rows/table:table-row|table:table-row-group/child::table:table-row)[$idx]"
)

RowCache

Cache for Row (internal).

Methods:

Name Description
__init__
__str__
cached_cell

Retrieve Cell in cache.

cell_idx

Find cell index in the map from the position.

cell_map_length
clear_cell_indexes
copy
delete_cell_in_cache
insert_cell_in_cache
insert_cell_map_once
make_cell_map
set_cell_in_cache
store_cell

Store Cell in cache.

width

Get the number of expected cells in the row, i.e. addition

Attributes:

Name Type Description
__slots__
cell_elements dict[int, Cell]
cell_map list[int]
Source code in odfdo/table_cache.py
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
class RowCache:
    """Cache for Row (internal)."""

    __slots__ = ("cell_elements", "cell_map")

    def __init__(self) -> None:
        self.cell_map: list[int] = []
        self.cell_elements: dict[int, Cell] = {}

    def __str__(self) -> str:
        return f"RC cell:{self.cell_map!r}"

    @classmethod
    def copy(cls, source: RowCache) -> RowCache:
        rc = cls()
        rc.cell_map = source.cell_map[:]
        return rc

    def width(self) -> int:
        """Get the number of expected cells in the row, i.e. addition
        repetitions.

        Returns:
            int: The number of expected cells in the row.
        """
        try:
            return self.cell_map[-1] + 1
        except IndexError:
            return 0

    def clear_cell_indexes(self) -> None:
        self.cell_elements = {}

    def cell_idx(self, position: int) -> int | None:
        """Find cell index in the map from the position."""
        idx = bisect_left(self.cell_map, position)
        if idx < len(self.cell_map):
            return idx
        return None

    def cell_map_length(self) -> int:
        return len(self.cell_map)

    def cached_cell(self, idx: int) -> Cell | None:
        """Retrieve Cell in cache."""
        return self.cell_elements.get(idx)

    def store_cell(self, cell: Cell, idx: int) -> None:
        """Store Cell in cache."""
        self.cell_elements[idx] = cell

    def insert_cell_map_once(self, repeated: int) -> None:
        self.cell_map = _insert_map_once(self.cell_map, len(self.cell_map), repeated)

    # def erase_cell_map_once(self, odf_idx: int) -> None:
    #     self.cell_map = _erase_map_once(self.cell_map, odf_idx)

    def make_cell_map(self, idx_repeated_sequence: list[tuple[int, int]]) -> None:
        self.cell_map = _make_cache_map(idx_repeated_sequence)

    def set_cell_in_cache(
        self,
        x: int,
        cell: Cell,
        vault: Row,
        clone: bool,
    ) -> Cell:
        idx = self.cell_idx(x)
        if idx is None:
            raise ValueError
        current_cached_cell: Cell | None = self.cached_cell(idx)
        if current_cached_cell is None:
            current_cached_cell = vault._get_element_idx2(_XP_CELL_IDX, idx)  # type: ignore[assignment]
        if not current_cached_cell:
            raise ValueError  # pragma: nocover
        self.clear_cell_indexes()
        emap, new_cell = _set_item_in_vault(
            x,
            cell,
            idx,
            current_cached_cell,
            vault,
            self.cell_map,
            _XP_CELL_IDX,
            clone,
        )
        self.cell_map = emap
        return new_cell  # type: ignore[return-value]

    def insert_cell_in_cache(
        self,
        x: int,
        cell: Cell,
        vault: Row,
    ) -> Cell:
        idx = self.cell_idx(x)
        if idx is None:
            raise ValueError
        current_cached_cell: Cell | None = self.cached_cell(idx)
        if current_cached_cell is None:
            current_cached_cell = vault._get_element_idx2(_XP_CELL_IDX, idx)  # type: ignore[assignment]
        if not current_cached_cell:
            raise ValueError  # pragma: nocover
        self.clear_cell_indexes()
        emap, new_cell = _insert_item_in_vault(
            x,
            cell,
            idx,
            current_cached_cell,
            vault,
            self.cell_map,
        )
        self.cell_map = emap
        return new_cell  # type: ignore[return-value]

    def delete_cell_in_cache(
        self,
        x: int,
        vault: Row,
    ) -> None:
        idx = self.cell_idx(x)
        if idx is None:
            raise ValueError
        current_cached_cell: Cell | None = self.cached_cell(idx)
        if current_cached_cell is None:
            current_cached_cell = vault._get_element_idx2(_XP_CELL_IDX, idx)  # type: ignore[assignment]
        if not current_cached_cell:
            raise ValueError  # pragma: nocover
        self.clear_cell_indexes()
        emap = _delete_item_in_vault(
            idx,
            current_cached_cell,
            vault,
            self.cell_map,
        )
        self.cell_map = emap

__slots__ class-attribute instance-attribute

__slots__ = ('cell_elements', 'cell_map')

cell_elements instance-attribute

cell_elements: dict[int, Cell] = {}

cell_map instance-attribute

cell_map: list[int] = []

__init__

__init__() -> None
Source code in odfdo/table_cache.py
265
266
267
def __init__(self) -> None:
    self.cell_map: list[int] = []
    self.cell_elements: dict[int, Cell] = {}

__str__

__str__() -> str
Source code in odfdo/table_cache.py
269
270
def __str__(self) -> str:
    return f"RC cell:{self.cell_map!r}"

cached_cell

cached_cell(idx: int) -> Cell | None

Retrieve Cell in cache.

Source code in odfdo/table_cache.py
303
304
305
def cached_cell(self, idx: int) -> Cell | None:
    """Retrieve Cell in cache."""
    return self.cell_elements.get(idx)

cell_idx

cell_idx(position: int) -> int | None

Find cell index in the map from the position.

Source code in odfdo/table_cache.py
293
294
295
296
297
298
def cell_idx(self, position: int) -> int | None:
    """Find cell index in the map from the position."""
    idx = bisect_left(self.cell_map, position)
    if idx < len(self.cell_map):
        return idx
    return None

cell_map_length

cell_map_length() -> int
Source code in odfdo/table_cache.py
300
301
def cell_map_length(self) -> int:
    return len(self.cell_map)

clear_cell_indexes

clear_cell_indexes() -> None
Source code in odfdo/table_cache.py
290
291
def clear_cell_indexes(self) -> None:
    self.cell_elements = {}

copy classmethod

copy(source: RowCache) -> RowCache
Source code in odfdo/table_cache.py
272
273
274
275
276
@classmethod
def copy(cls, source: RowCache) -> RowCache:
    rc = cls()
    rc.cell_map = source.cell_map[:]
    return rc

delete_cell_in_cache

delete_cell_in_cache(x: int, vault: Row) -> None
Source code in odfdo/table_cache.py
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
def delete_cell_in_cache(
    self,
    x: int,
    vault: Row,
) -> None:
    idx = self.cell_idx(x)
    if idx is None:
        raise ValueError
    current_cached_cell: Cell | None = self.cached_cell(idx)
    if current_cached_cell is None:
        current_cached_cell = vault._get_element_idx2(_XP_CELL_IDX, idx)  # type: ignore[assignment]
    if not current_cached_cell:
        raise ValueError  # pragma: nocover
    self.clear_cell_indexes()
    emap = _delete_item_in_vault(
        idx,
        current_cached_cell,
        vault,
        self.cell_map,
    )
    self.cell_map = emap

insert_cell_in_cache

insert_cell_in_cache(
    x: int, cell: Cell, vault: Row
) -> Cell
Source code in odfdo/table_cache.py
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
def insert_cell_in_cache(
    self,
    x: int,
    cell: Cell,
    vault: Row,
) -> Cell:
    idx = self.cell_idx(x)
    if idx is None:
        raise ValueError
    current_cached_cell: Cell | None = self.cached_cell(idx)
    if current_cached_cell is None:
        current_cached_cell = vault._get_element_idx2(_XP_CELL_IDX, idx)  # type: ignore[assignment]
    if not current_cached_cell:
        raise ValueError  # pragma: nocover
    self.clear_cell_indexes()
    emap, new_cell = _insert_item_in_vault(
        x,
        cell,
        idx,
        current_cached_cell,
        vault,
        self.cell_map,
    )
    self.cell_map = emap
    return new_cell  # type: ignore[return-value]

insert_cell_map_once

insert_cell_map_once(repeated: int) -> None
Source code in odfdo/table_cache.py
311
312
def insert_cell_map_once(self, repeated: int) -> None:
    self.cell_map = _insert_map_once(self.cell_map, len(self.cell_map), repeated)

make_cell_map

make_cell_map(
    idx_repeated_sequence: list[tuple[int, int]],
) -> None
Source code in odfdo/table_cache.py
317
318
def make_cell_map(self, idx_repeated_sequence: list[tuple[int, int]]) -> None:
    self.cell_map = _make_cache_map(idx_repeated_sequence)

set_cell_in_cache

set_cell_in_cache(
    x: int, cell: Cell, vault: Row, clone: bool
) -> Cell
Source code in odfdo/table_cache.py
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
def set_cell_in_cache(
    self,
    x: int,
    cell: Cell,
    vault: Row,
    clone: bool,
) -> Cell:
    idx = self.cell_idx(x)
    if idx is None:
        raise ValueError
    current_cached_cell: Cell | None = self.cached_cell(idx)
    if current_cached_cell is None:
        current_cached_cell = vault._get_element_idx2(_XP_CELL_IDX, idx)  # type: ignore[assignment]
    if not current_cached_cell:
        raise ValueError  # pragma: nocover
    self.clear_cell_indexes()
    emap, new_cell = _set_item_in_vault(
        x,
        cell,
        idx,
        current_cached_cell,
        vault,
        self.cell_map,
        _XP_CELL_IDX,
        clone,
    )
    self.cell_map = emap
    return new_cell  # type: ignore[return-value]

store_cell

store_cell(cell: Cell, idx: int) -> None

Store Cell in cache.

Source code in odfdo/table_cache.py
307
308
309
def store_cell(self, cell: Cell, idx: int) -> None:
    """Store Cell in cache."""
    self.cell_elements[idx] = cell

width

width() -> int

Get the number of expected cells in the row, i.e. addition repetitions.

Returns:

Name Type Description
int int

The number of expected cells in the row.

Source code in odfdo/table_cache.py
278
279
280
281
282
283
284
285
286
287
288
def width(self) -> int:
    """Get the number of expected cells in the row, i.e. addition
    repetitions.

    Returns:
        int: The number of expected cells in the row.
    """
    try:
        return self.cell_map[-1] + 1
    except IndexError:
        return 0

TableCache

Cache for Table (internal).

Methods:

Name Description
__init__
__str__
cached_col

Retrieve Column in cache.

cached_row

Retrieve Row in cache.

clear_col_indexes
clear_row_indexes
col_idx

Find column index in the map from the position.

col_map_length
copy
delete_col_in_cache
delete_row_in_cache
height

Get the current height of the table.

insert_col_in_cache
insert_col_map_once
insert_row_in_cache
insert_row_map_once
make_col_map
make_row_map
row_idx

Find row index in the map from the position.

set_col_in_cache
set_row_in_cache
store_col

Store Column in cache.

store_row

Store Row in cache.

width

Get the current width of the table, measured on columns.

Attributes:

Name Type Description
__slots__
col_elements dict[int, Column]
col_map list[int]
row_elements dict[int, Row]
row_map list[int]
Source code in odfdo/table_cache.py
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
class TableCache:
    """Cache for Table (internal)."""

    __slots__ = ("col_elements", "col_map", "row_elements", "row_map")

    def __init__(self) -> None:
        self.row_map: list[int] = []
        self.col_map: list[int] = []
        self.row_elements: dict[int, Row] = {}
        self.col_elements: dict[int, Column] = {}

    def __str__(self) -> str:
        return f"TC row:{self.row_map!r} col:{self.col_map!r}"

    @classmethod
    def copy(cls, source: TableCache) -> TableCache:
        tc = cls()
        tc.row_map = source.row_map[:]
        tc.col_map = source.col_map[:]
        return tc

    def height(self) -> int:
        """Get the current height of the table.

        Returns:
            int: The current height of the table.
        """
        try:
            return self.row_map[-1] + 1
        except IndexError:
            return 0

    def width(self) -> int:
        """Get the current width of the table, measured on columns.

        Rows may have different widths, use the Table API to ensure width
        consistency.

        Returns:
            int: The current width of the table.
        """
        try:
            return self.col_map[-1] + 1
        except IndexError:
            return 0

    def clear_row_indexes(self) -> None:
        self.row_elements = {}

    def clear_col_indexes(self) -> None:
        self.col_elements = {}

    def row_idx(self, position: int) -> int | None:
        """Find row index in the map from the position."""
        idx = bisect_left(self.row_map, position)
        if idx < len(self.row_map):
            return idx
        return None

    def col_idx(self, position: int) -> int | None:
        """Find column index in the map from the position."""
        idx = bisect_left(self.col_map, position)
        if idx < len(self.col_map):
            return idx
        return None

    def col_map_length(self) -> int:
        return len(self.col_map)

    def cached_row(self, idx: int) -> Row | None:
        """Retrieve Row in cache."""
        return self.row_elements.get(idx)

    def cached_col(self, idx: int) -> Column | None:
        """Retrieve Column in cache."""
        return self.col_elements.get(idx)

    def store_row(self, row: Row, idx: int) -> None:
        """Store Row in cache."""
        self.row_elements[idx] = row

    def store_col(self, col: Column, idx: int) -> None:
        """Store Column in cache."""
        self.col_elements[idx] = col

    def insert_row_map_once(self, repeated: int) -> None:
        self.row_map = _insert_map_once(self.row_map, len(self.row_map), repeated)

    # def erase_row_map_once(self, odf_idx: int) -> None:
    #     self.row_map = _erase_map_once(self.row_map, odf_idx)

    def insert_col_map_once(self, repeated: int) -> None:
        self.col_map = _insert_map_once(self.col_map, len(self.col_map), repeated)

    # def erase_col_map_once(self, odf_idx: int) -> None:
    #     self.col_map = _erase_map_once(self.col_map, odf_idx)

    def make_row_map(self, idx_repeated_sequence: list[tuple[int, int]]) -> None:
        self.row_map = _make_cache_map(idx_repeated_sequence)

    def make_col_map(self, idx_repeated_sequence: list[tuple[int, int]]) -> None:
        self.col_map = _make_cache_map(idx_repeated_sequence)

    def set_row_in_cache(
        self,
        y: int,
        row: Row,
        vault: Table,
        clone: bool,
    ) -> Row:
        idx = self.row_idx(y)
        if idx is None:
            raise ValueError
        current_cached_row: Row | None = self.cached_row(idx)
        if current_cached_row is None:
            current_cached_row = vault._get_element_idx2(_XP_ROW_IDX, idx)  # type: ignore[assignment]
        if not current_cached_row:
            raise ValueError  # pragma: nocover
        self.clear_row_indexes()
        emap, new_row = _set_item_in_vault(
            y,
            row,
            idx,
            current_cached_row,
            vault,
            self.row_map,
            _XP_ROW_IDX,
            clone,
        )
        self.row_map = emap
        return new_row  # type: ignore[return-value]

    def insert_row_in_cache(
        self,
        y: int,
        row: Row,
        vault: Table,
    ) -> Row:
        idx = self.row_idx(y)
        if idx is None:
            raise ValueError
        current_cached_row: Row | None = self.cached_row(idx)
        if current_cached_row is None:
            current_cached_row = vault._get_element_idx2(_XP_ROW_IDX, idx)  # type: ignore[assignment]
        if not current_cached_row:
            raise ValueError  # pragma: nocover
        self.clear_row_indexes()
        emap, new_row = _insert_item_in_vault(
            y,
            row,
            idx,
            current_cached_row,
            vault,
            self.row_map,
        )
        self.row_map = emap
        return new_row  # type: ignore[return-value]

    def delete_row_in_cache(
        self,
        y: int,
        vault: Table,
    ) -> None:
        idx = self.row_idx(y)
        if idx is None:
            raise ValueError
        current_cached_row: Row | None = self.cached_row(idx)
        if current_cached_row is None:
            current_cached_row = vault._get_element_idx2(_XP_ROW_IDX, idx)  # type: ignore[assignment]
        if not current_cached_row:
            raise ValueError  # pragma: nocover
        self.clear_row_indexes()
        emap = _delete_item_in_vault(
            idx,
            current_cached_row,
            vault,
            self.row_map,
        )
        self.row_map = emap

    def set_col_in_cache(
        self,
        x: int,
        column: Column,
        vault: Table,
    ) -> Column:
        idx = self.col_idx(x)
        if idx is None:
            raise ValueError
        current_cached_col: Column | None = self.cached_col(idx)
        if current_cached_col is None:
            current_cached_col = vault._get_element_idx2(_XP_COLUMN_IDX, idx)  # type: ignore[assignment]
        if not current_cached_col:
            raise ValueError  # pragma: nocover
        self.clear_col_indexes()
        emap, new_col = _set_item_in_vault(
            x,
            column,
            idx,
            current_cached_col,
            vault,
            self.col_map,
            _XP_COLUMN_IDX,
        )
        self.col_map = emap
        return new_col  # type: ignore[return-value]

    def insert_col_in_cache(
        self,
        x: int,
        column: Column,
        vault: Table,
    ) -> Column:
        idx = self.col_idx(x)
        if idx is None:
            raise ValueError
        current_cached_col: Column | None = self.cached_col(idx)
        if current_cached_col is None:
            current_cached_col = vault._get_element_idx2(_XP_COLUMN_IDX, idx)  # type: ignore[assignment]
        if not current_cached_col:
            raise ValueError  # pragma: nocover
        self.clear_col_indexes()
        emap, new_col = _insert_item_in_vault(
            x,
            column,
            idx,
            current_cached_col,
            vault,
            self.col_map,
        )
        self.col_map = emap
        return new_col  # type: ignore[return-value]

    def delete_col_in_cache(
        self,
        x: int,
        vault: Table,
    ) -> None:
        idx = self.col_idx(x)
        if idx is None:
            raise ValueError
        current_cached_col: Column | None = self.cached_col(idx)
        if current_cached_col is None:
            current_cached_col = vault._get_element_idx2(_XP_COLUMN_IDX, idx)  # type: ignore[assignment]
        if not current_cached_col:
            raise ValueError  # pragma: nocover
        self.clear_col_indexes()
        emap = _delete_item_in_vault(
            idx,
            current_cached_col,
            vault,
            self.col_map,
        )
        self.col_map = emap

__slots__ class-attribute instance-attribute

__slots__ = (
    "col_elements",
    "col_map",
    "row_elements",
    "row_map",
)

col_elements instance-attribute

col_elements: dict[int, Column] = {}

col_map instance-attribute

col_map: list[int] = []

row_elements instance-attribute

row_elements: dict[int, Row] = {}

row_map instance-attribute

row_map: list[int] = []

__init__

__init__() -> None
Source code in odfdo/table_cache.py
403
404
405
406
407
def __init__(self) -> None:
    self.row_map: list[int] = []
    self.col_map: list[int] = []
    self.row_elements: dict[int, Row] = {}
    self.col_elements: dict[int, Column] = {}

__str__

__str__() -> str
Source code in odfdo/table_cache.py
409
410
def __str__(self) -> str:
    return f"TC row:{self.row_map!r} col:{self.col_map!r}"

cached_col

cached_col(idx: int) -> Column | None

Retrieve Column in cache.

Source code in odfdo/table_cache.py
471
472
473
def cached_col(self, idx: int) -> Column | None:
    """Retrieve Column in cache."""
    return self.col_elements.get(idx)

cached_row

cached_row(idx: int) -> Row | None

Retrieve Row in cache.

Source code in odfdo/table_cache.py
467
468
469
def cached_row(self, idx: int) -> Row | None:
    """Retrieve Row in cache."""
    return self.row_elements.get(idx)

clear_col_indexes

clear_col_indexes() -> None
Source code in odfdo/table_cache.py
447
448
def clear_col_indexes(self) -> None:
    self.col_elements = {}

clear_row_indexes

clear_row_indexes() -> None
Source code in odfdo/table_cache.py
444
445
def clear_row_indexes(self) -> None:
    self.row_elements = {}

col_idx

col_idx(position: int) -> int | None

Find column index in the map from the position.

Source code in odfdo/table_cache.py
457
458
459
460
461
462
def col_idx(self, position: int) -> int | None:
    """Find column index in the map from the position."""
    idx = bisect_left(self.col_map, position)
    if idx < len(self.col_map):
        return idx
    return None

col_map_length

col_map_length() -> int
Source code in odfdo/table_cache.py
464
465
def col_map_length(self) -> int:
    return len(self.col_map)

copy classmethod

copy(source: TableCache) -> TableCache
Source code in odfdo/table_cache.py
412
413
414
415
416
417
@classmethod
def copy(cls, source: TableCache) -> TableCache:
    tc = cls()
    tc.row_map = source.row_map[:]
    tc.col_map = source.col_map[:]
    return tc

delete_col_in_cache

delete_col_in_cache(x: int, vault: Table) -> None
Source code in odfdo/table_cache.py
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
def delete_col_in_cache(
    self,
    x: int,
    vault: Table,
) -> None:
    idx = self.col_idx(x)
    if idx is None:
        raise ValueError
    current_cached_col: Column | None = self.cached_col(idx)
    if current_cached_col is None:
        current_cached_col = vault._get_element_idx2(_XP_COLUMN_IDX, idx)  # type: ignore[assignment]
    if not current_cached_col:
        raise ValueError  # pragma: nocover
    self.clear_col_indexes()
    emap = _delete_item_in_vault(
        idx,
        current_cached_col,
        vault,
        self.col_map,
    )
    self.col_map = emap

delete_row_in_cache

delete_row_in_cache(y: int, vault: Table) -> None
Source code in odfdo/table_cache.py
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
def delete_row_in_cache(
    self,
    y: int,
    vault: Table,
) -> None:
    idx = self.row_idx(y)
    if idx is None:
        raise ValueError
    current_cached_row: Row | None = self.cached_row(idx)
    if current_cached_row is None:
        current_cached_row = vault._get_element_idx2(_XP_ROW_IDX, idx)  # type: ignore[assignment]
    if not current_cached_row:
        raise ValueError  # pragma: nocover
    self.clear_row_indexes()
    emap = _delete_item_in_vault(
        idx,
        current_cached_row,
        vault,
        self.row_map,
    )
    self.row_map = emap

height

height() -> int

Get the current height of the table.

Returns:

Name Type Description
int int

The current height of the table.

Source code in odfdo/table_cache.py
419
420
421
422
423
424
425
426
427
428
def height(self) -> int:
    """Get the current height of the table.

    Returns:
        int: The current height of the table.
    """
    try:
        return self.row_map[-1] + 1
    except IndexError:
        return 0

insert_col_in_cache

insert_col_in_cache(
    x: int, column: Column, vault: Table
) -> Column
Source code in odfdo/table_cache.py
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
def insert_col_in_cache(
    self,
    x: int,
    column: Column,
    vault: Table,
) -> Column:
    idx = self.col_idx(x)
    if idx is None:
        raise ValueError
    current_cached_col: Column | None = self.cached_col(idx)
    if current_cached_col is None:
        current_cached_col = vault._get_element_idx2(_XP_COLUMN_IDX, idx)  # type: ignore[assignment]
    if not current_cached_col:
        raise ValueError  # pragma: nocover
    self.clear_col_indexes()
    emap, new_col = _insert_item_in_vault(
        x,
        column,
        idx,
        current_cached_col,
        vault,
        self.col_map,
    )
    self.col_map = emap
    return new_col  # type: ignore[return-value]

insert_col_map_once

insert_col_map_once(repeated: int) -> None
Source code in odfdo/table_cache.py
489
490
def insert_col_map_once(self, repeated: int) -> None:
    self.col_map = _insert_map_once(self.col_map, len(self.col_map), repeated)

insert_row_in_cache

insert_row_in_cache(y: int, row: Row, vault: Table) -> Row
Source code in odfdo/table_cache.py
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
def insert_row_in_cache(
    self,
    y: int,
    row: Row,
    vault: Table,
) -> Row:
    idx = self.row_idx(y)
    if idx is None:
        raise ValueError
    current_cached_row: Row | None = self.cached_row(idx)
    if current_cached_row is None:
        current_cached_row = vault._get_element_idx2(_XP_ROW_IDX, idx)  # type: ignore[assignment]
    if not current_cached_row:
        raise ValueError  # pragma: nocover
    self.clear_row_indexes()
    emap, new_row = _insert_item_in_vault(
        y,
        row,
        idx,
        current_cached_row,
        vault,
        self.row_map,
    )
    self.row_map = emap
    return new_row  # type: ignore[return-value]

insert_row_map_once

insert_row_map_once(repeated: int) -> None
Source code in odfdo/table_cache.py
483
484
def insert_row_map_once(self, repeated: int) -> None:
    self.row_map = _insert_map_once(self.row_map, len(self.row_map), repeated)

make_col_map

make_col_map(
    idx_repeated_sequence: list[tuple[int, int]],
) -> None
Source code in odfdo/table_cache.py
498
499
def make_col_map(self, idx_repeated_sequence: list[tuple[int, int]]) -> None:
    self.col_map = _make_cache_map(idx_repeated_sequence)

make_row_map

make_row_map(
    idx_repeated_sequence: list[tuple[int, int]],
) -> None
Source code in odfdo/table_cache.py
495
496
def make_row_map(self, idx_repeated_sequence: list[tuple[int, int]]) -> None:
    self.row_map = _make_cache_map(idx_repeated_sequence)

row_idx

row_idx(position: int) -> int | None

Find row index in the map from the position.

Source code in odfdo/table_cache.py
450
451
452
453
454
455
def row_idx(self, position: int) -> int | None:
    """Find row index in the map from the position."""
    idx = bisect_left(self.row_map, position)
    if idx < len(self.row_map):
        return idx
    return None

set_col_in_cache

set_col_in_cache(
    x: int, column: Column, vault: Table
) -> Column
Source code in odfdo/table_cache.py
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
def set_col_in_cache(
    self,
    x: int,
    column: Column,
    vault: Table,
) -> Column:
    idx = self.col_idx(x)
    if idx is None:
        raise ValueError
    current_cached_col: Column | None = self.cached_col(idx)
    if current_cached_col is None:
        current_cached_col = vault._get_element_idx2(_XP_COLUMN_IDX, idx)  # type: ignore[assignment]
    if not current_cached_col:
        raise ValueError  # pragma: nocover
    self.clear_col_indexes()
    emap, new_col = _set_item_in_vault(
        x,
        column,
        idx,
        current_cached_col,
        vault,
        self.col_map,
        _XP_COLUMN_IDX,
    )
    self.col_map = emap
    return new_col  # type: ignore[return-value]

set_row_in_cache

set_row_in_cache(
    y: int, row: Row, vault: Table, clone: bool
) -> Row
Source code in odfdo/table_cache.py
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
def set_row_in_cache(
    self,
    y: int,
    row: Row,
    vault: Table,
    clone: bool,
) -> Row:
    idx = self.row_idx(y)
    if idx is None:
        raise ValueError
    current_cached_row: Row | None = self.cached_row(idx)
    if current_cached_row is None:
        current_cached_row = vault._get_element_idx2(_XP_ROW_IDX, idx)  # type: ignore[assignment]
    if not current_cached_row:
        raise ValueError  # pragma: nocover
    self.clear_row_indexes()
    emap, new_row = _set_item_in_vault(
        y,
        row,
        idx,
        current_cached_row,
        vault,
        self.row_map,
        _XP_ROW_IDX,
        clone,
    )
    self.row_map = emap
    return new_row  # type: ignore[return-value]

store_col

store_col(col: Column, idx: int) -> None

Store Column in cache.

Source code in odfdo/table_cache.py
479
480
481
def store_col(self, col: Column, idx: int) -> None:
    """Store Column in cache."""
    self.col_elements[idx] = col

store_row

store_row(row: Row, idx: int) -> None

Store Row in cache.

Source code in odfdo/table_cache.py
475
476
477
def store_row(self, row: Row, idx: int) -> None:
    """Store Row in cache."""
    self.row_elements[idx] = row

width

width() -> int

Get the current width of the table, measured on columns.

Rows may have different widths, use the Table API to ensure width consistency.

Returns:

Name Type Description
int int

The current width of the table.

Source code in odfdo/table_cache.py
430
431
432
433
434
435
436
437
438
439
440
441
442
def width(self) -> int:
    """Get the current width of the table, measured on columns.

    Rows may have different widths, use the Table API to ensure width
    consistency.

    Returns:
        int: The current width of the table.
    """
    try:
        return self.col_map[-1] + 1
    except IndexError:
        return 0

_delete_item_in_vault

_delete_item_in_vault(
    idx: int,
    current_item: Cell | Row | Column,
    vault: Row | Table,
    vault_map: list[int],
) -> list[int]
Source code in odfdo/table_cache.py
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
def _delete_item_in_vault(
    idx: int,
    current_item: Cell | Row | Column,
    vault: Row | Table,
    vault_map: list[int],
) -> list[int]:
    current_cache = vault_map[idx]
    if idx > 0:
        before_cache = vault_map[idx - 1]
    else:
        before_cache = -1
    # current_pos = before_cache + 1
    current_repeated = current_cache - before_cache
    new_repeated = current_repeated - 1
    if new_repeated >= 1:
        current_item._set_repeated(new_repeated)
        emap = vault_map[:idx] + [(x - 1) for x in vault_map[idx:]]
    else:
        # actual erase
        vault.delete(current_item)
        emap = vault_map[:idx] + [(x - 1) for x in vault_map[idx + 1 :]]
    return emap

_erase_map_once

_erase_map_once(
    cache_map: list[int], odf_idx: int
) -> list[int]

Remove an item (cell or row) from the map.

Parameters:

Name Type Description Default
cache_map list[int]

Cache map.

required
odf_idx int

Index in ODF XML.

required
Source code in odfdo/table_cache.py
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
def _erase_map_once(cache_map: list[int], odf_idx: int) -> list[int]:
    """Remove an item (cell or row) from the map.

    Args:
        cache_map: Cache map.
        odf_idx: Index in ODF XML.
    """
    if odf_idx >= len(cache_map):
        raise IndexError
    if odf_idx > 0:
        before = cache_map[odf_idx - 1]
    else:
        before = -1
    current = cache_map[odf_idx]
    repeated = current - before
    cache_map = cache_map[:odf_idx] + [(x - repeated) for x in cache_map[odf_idx + 1 :]]
    return cache_map

_insert_item_in_vault

_insert_item_in_vault(
    position: int,
    item: Cell | Row | Column,
    idx: int,
    current_item: Cell | Row | Column,
    vault: Row | Table,
    vault_map: list[int],
) -> tuple[list[int], Cell | Row | Column]
Source code in odfdo/table_cache.py
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
def _insert_item_in_vault(
    position: int,
    item: Cell | Row | Column,
    idx: int,
    current_item: Cell | Row | Column,
    vault: Row | Table,
    vault_map: list[int],
) -> tuple[list[int], Cell | Row | Column]:
    repeated = item.repeated or 1
    target_idx = vault.index(current_item)
    current_cache = vault_map[idx]
    if idx > 0:
        before_cache = vault_map[idx - 1]
    else:
        before_cache = -1
    current_pos = before_cache + 1
    current_repeated = current_cache - before_cache
    repeated_before = position - current_pos
    repeated_after = current_repeated - repeated_before
    new_item = item.clone
    if repeated_before >= 1:
        current_item._set_repeated(repeated_before)
        vault.insert(new_item, position=target_idx + 1)
        after_item = current_item.clone
        after_item._set_repeated(repeated_after)
        vault.insert(after_item, position=target_idx + 2)
    else:
        # only insert new cell
        vault.insert(new_item, position=target_idx)
    # update cache
    if repeated_before >= 1:
        emap = _erase_map_once(vault_map, idx)
        emap = _insert_map_once(emap, idx, repeated_before)
        emap = _insert_map_once(emap, idx + 1, repeated)
        emap = _insert_map_once(emap, idx + 2, repeated_after)
    else:
        emap = _insert_map_once(vault_map, idx, repeated)
    return emap, new_item

_insert_map_once

_insert_map_once(
    cache_map: list[int], odf_idx: int, repeated: int
) -> list[int]

Add an item (cell or row) to the map.

Parameters:

Name Type Description Default
cache_map list[int]

Cache map.

required
odf_idx int

Index in ODF XML.

required
repeated int

Repeated value of item, 1 or more.

required

odf_idx is NOT position (col or row), neither raw XML position, but ODF index

Source code in odfdo/table_cache.py
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
def _insert_map_once(
    cache_map: list[int],
    odf_idx: int,
    repeated: int,
) -> list[int]:
    """Add an item (cell or row) to the map.

    Args:
        cache_map: Cache map.
        odf_idx: Index in ODF XML.
        repeated: Repeated value of item, 1 or more.

    odf_idx is NOT position (col or row), neither raw XML position, but ODF index
    """
    repeated = repeated or 1
    if odf_idx > len(cache_map):
        raise IndexError
    if odf_idx > 0:
        before = cache_map[odf_idx - 1]
    else:
        before = -1
    juska = before + repeated  # aka max position value for item
    if odf_idx == len(cache_map):
        insort(cache_map, juska)
        return cache_map
    new_map = cache_map[:odf_idx]
    new_map.append(juska)
    new_map.extend([(x + repeated) for x in cache_map[odf_idx:]])
    return new_map

_make_cache_map

_make_cache_map(
    idx_repeated_seq: list[tuple[int, int]],
) -> list[int]

Build the initial cache map of the table.

Source code in odfdo/table_cache.py
49
50
51
52
53
54
def _make_cache_map(idx_repeated_seq: list[tuple[int, int]]) -> list[int]:
    """Build the initial cache map of the table."""
    cache_map: list[int] = []
    for odf_idx, repeated in idx_repeated_seq:
        cache_map = _insert_map_once(cache_map, odf_idx, repeated)
    return cache_map

_set_item_in_vault

_set_item_in_vault(
    position: int,
    item: Cell | Row | Column,
    idx: int,
    current_item: Cell | Row | Column,
    vault: Row | Table,
    vault_map: list[int],
    vault_scheme: XPath,
    clone: bool = True,
) -> tuple[list[int], Cell | Row | Column]

Set the item (cell, row) in its vault (row, table), updating the cache map.

Parameters:

Name Type Description Default
position int

Position of the item.

required
item Cell | Row | Column

The item to set.

required
idx int

Index in ODF XML.

required
current_item Cell | Row | Column

Current item.

required
vault Row | Table

Vault.

required
vault_map list[int]

Vault map.

required
vault_scheme XPath

Vault scheme.

required
clone bool

Whether to clone the item.

True
Source code in odfdo/table_cache.py
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
def _set_item_in_vault(
    position: int,
    item: Cell | Row | Column,
    idx: int,
    current_item: Cell | Row | Column,
    vault: Row | Table,
    vault_map: list[int],
    vault_scheme: XPath,
    clone: bool = True,
) -> tuple[list[int], Cell | Row | Column]:
    """Set the item (cell, row) in its vault (row, table), updating the cache
    map.

    Args:
        position: Position of the item.
        item: The item to set.
        idx: Index in ODF XML.
        current_item: Current item.
        vault: Vault.
        vault_map: Vault map.
        vault_scheme: Vault scheme.
        clone: Whether to clone the item.
    """
    repeated = item.repeated or 1
    target_idx = vault.index(current_item)
    current_cache = vault_map[idx]
    if idx > 0:
        before_cache = vault_map[idx - 1]
    else:
        before_cache = -1
    current_pos = before_cache + 1
    current_repeated = current_cache - before_cache
    repeated_before = position - current_pos
    repeated_after = current_repeated - repeated_before - repeated
    if repeated_before >= 1:
        # Update repetition
        current_item._set_repeated(repeated_before)
        target_idx += 1
    else:
        # Replacing the first occurrence
        vault.delete(current_item)
    # Insert new element
    if clone:
        new_item = item.clone
    else:
        new_item = item
    vault.insert(new_item, position=target_idx)
    # Insert the remaining repetitions
    if repeated_after >= 1:
        after_item = current_item.clone
        after_item._set_repeated(repeated_after)
        vault.insert(after_item, position=target_idx + 1)
    # setting a repeated item !
    if repeated_after < 0:
        # deleting some overlapped items
        deleting = repeated_after
        while deleting < 0:
            delete_item = vault._get_element_idx2(vault_scheme, target_idx + 1)
            if delete_item is None:
                break
            is_repeated = delete_item.repeated or 1  # type: ignore
            is_repeated += deleting
            if is_repeated > 1:
                delete_item._set_repeated(is_repeated)  # type: ignore
            else:
                vault.delete(delete_item)
            deleting = is_repeated
    # update cache
    # remove existing
    emap = _erase_map_once(vault_map, idx)
    # add before if any:
    if repeated_before >= 1:
        emap = _insert_map_once(emap, idx, repeated_before)
        idx += 1
    # add our slot
    emap = _insert_map_once(emap, idx, repeated)
    # add after if any::
    if repeated_after >= 1:
        idx += 1
        emap = _insert_map_once(emap, idx, repeated_after)
    if repeated_after < 0:
        idx += 1
        while repeated_after < 0:
            if idx < len(emap):
                emap = _erase_map_once(emap, idx)
            repeated_after += 1
    return emap, new_item