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__

Initialize a RowCache.

__str__
cached_cell

Retrieve Cell in cache.

cell_idx

Find cell index in the map from the position.

cell_map_length

Return the length of the cell map.

clear_cell_indexes

Clear the indexed cached cells.

copy

Return a copy of the row cache.

delete_cell_in_cache

Delete the cell at position x from cache and row element.

insert_cell_in_cache

Insert a cell in cache and row element at position x.

insert_cell_map_once

Insert a repeated cell count into the cell map.

make_cell_map

Build the cell map from repeated sequence data.

set_cell_in_cache

Update the cache and row element with a cell at position x.

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
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
class RowCache:
    """Cache for Row (internal)."""

    __slots__ = ("cell_elements", "cell_map")

    def __init__(self) -> None:
        """Initialize a RowCache."""
        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:
        """Return a copy of the row cache."""
        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:
        """Clear the indexed cached cells."""
        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 the length of the cell map."""
        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:
        """Insert a repeated cell count into the cell map."""
        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:
        """Build the cell map from repeated sequence data."""
        self.cell_map = _make_cache_map(idx_repeated_sequence)

    def set_cell_in_cache(
        self,
        x: int,
        cell: Cell,
        vault: Row,
        clone: bool,
    ) -> Cell:
        """Update the cache and row element with a cell at position x."""
        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)  # ty: ignore[invalid-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  # ty: ignore[invalid-return-type]

    def insert_cell_in_cache(
        self,
        x: int,
        cell: Cell,
        vault: Row,
    ) -> Cell:
        """Insert a cell in cache and row element at position x."""
        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)  # ty: ignore[invalid-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  # ty: ignore[invalid-return-type]

    def delete_cell_in_cache(
        self,
        x: int,
        vault: Row,
    ) -> None:
        """Delete the cell at position x from cache and row element."""
        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)  # ty: ignore[invalid-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

Initialize a RowCache.

Source code in odfdo/table_cache.py
267
268
269
270
def __init__(self) -> None:
    """Initialize a RowCache."""
    self.cell_map: list[int] = []
    self.cell_elements: dict[int, Cell] = {}

__str__

__str__() -> str
Source code in odfdo/table_cache.py
272
273
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
310
311
312
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
299
300
301
302
303
304
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

Return the length of the cell map.

Source code in odfdo/table_cache.py
306
307
308
def cell_map_length(self) -> int:
    """Return the length of the cell map."""
    return len(self.cell_map)

clear_cell_indexes

clear_cell_indexes() -> None

Clear the indexed cached cells.

Source code in odfdo/table_cache.py
295
296
297
def clear_cell_indexes(self) -> None:
    """Clear the indexed cached cells."""
    self.cell_elements = {}

copy classmethod

copy(source: RowCache) -> RowCache

Return a copy of the row cache.

Source code in odfdo/table_cache.py
275
276
277
278
279
280
@classmethod
def copy(cls, source: RowCache) -> RowCache:
    """Return a copy of the row cache."""
    rc = cls()
    rc.cell_map = source.cell_map[:]
    return rc

delete_cell_in_cache

delete_cell_in_cache(x: int, vault: Row) -> None

Delete the cell at position x from cache and row element.

Source code in odfdo/table_cache.py
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
def delete_cell_in_cache(
    self,
    x: int,
    vault: Row,
) -> None:
    """Delete the cell at position x from cache and row element."""
    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)  # ty: ignore[invalid-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

Insert a cell in cache and row element at position x.

Source code in odfdo/table_cache.py
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
def insert_cell_in_cache(
    self,
    x: int,
    cell: Cell,
    vault: Row,
) -> Cell:
    """Insert a cell in cache and row element at position x."""
    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)  # ty: ignore[invalid-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  # ty: ignore[invalid-return-type]

insert_cell_map_once

insert_cell_map_once(repeated: int) -> None

Insert a repeated cell count into the cell map.

Source code in odfdo/table_cache.py
318
319
320
def insert_cell_map_once(self, repeated: int) -> None:
    """Insert a repeated cell count into the cell map."""
    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

Build the cell map from repeated sequence data.

Source code in odfdo/table_cache.py
325
326
327
def make_cell_map(self, idx_repeated_sequence: list[tuple[int, int]]) -> None:
    """Build the cell map from repeated sequence data."""
    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

Update the cache and row element with a cell at position x.

Source code in odfdo/table_cache.py
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
def set_cell_in_cache(
    self,
    x: int,
    cell: Cell,
    vault: Row,
    clone: bool,
) -> Cell:
    """Update the cache and row element with a cell at position x."""
    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)  # ty: ignore[invalid-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  # ty: ignore[invalid-return-type]

store_cell

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

Store Cell in cache.

Source code in odfdo/table_cache.py
314
315
316
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
282
283
284
285
286
287
288
289
290
291
292
293
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__

Initialize a TableCache.

__str__
cached_col

Retrieve Column in cache.

cached_row

Retrieve Row in cache.

clear_col_indexes

Clear the indexed cached columns.

clear_row_indexes

Clear the indexed cached rows.

col_idx

Find column index in the map from the position.

col_map_length

Return the length of the column map.

copy

Return a copy of the table cache.

delete_col_in_cache

Delete the column at position x from cache and table element.

delete_row_in_cache

Delete the row at position y from cache and table element.

height

Get the current height of the table.

insert_col_in_cache

Insert a column in cache and table element at position x.

insert_col_map_once

Insert a repeated column count into the column map.

insert_row_in_cache

Insert a row in cache and table element at position y.

insert_row_map_once

Insert a repeated row count into the row map.

make_col_map

Build the column map from repeated sequence data.

make_row_map

Build the row map from repeated sequence data.

row_idx

Find row index in the map from the position.

set_col_in_cache

Update the cache and table element with a column at position x.

set_row_in_cache

Update the cache and table element with a row at position y.

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__
cached_width int | None
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
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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
class TableCache:
    """Cache for Table (internal)."""

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

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

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

    @classmethod
    def copy(cls, source: TableCache) -> TableCache:
        """Return a copy of the table cache."""
        tc = cls()
        tc.row_map = source.row_map[:]
        tc.col_map = source.col_map[:]
        tc.cached_width = source.cached_width
        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:
        """Clear the indexed cached rows."""
        self.row_elements = {}
        self.cached_width = None

    def clear_col_indexes(self) -> None:
        """Clear the indexed cached columns."""
        self.col_elements = {}
        self.cached_width = None

    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 the length of the column map."""
        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:
        """Insert a repeated row count into the row map."""
        self.row_map = _insert_map_once(self.row_map, len(self.row_map), repeated)
        self.cached_width = None

    # 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:
        """Insert a repeated column count into the column map."""
        self.col_map = _insert_map_once(self.col_map, len(self.col_map), repeated)
        self.cached_width = None

    # 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:
        """Build the row map from repeated sequence data."""
        self.row_map = _make_cache_map(idx_repeated_sequence)
        self.cached_width = None

    def make_col_map(self, idx_repeated_sequence: list[tuple[int, int]]) -> None:
        """Build the column map from repeated sequence data."""
        self.col_map = _make_cache_map(idx_repeated_sequence)
        self.cached_width = None

    def set_row_in_cache(
        self,
        y: int,
        row: Row,
        vault: Table,
        clone: bool,
    ) -> Row:
        """Update the cache and table element with a row at position y."""
        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)  # ty: ignore[invalid-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  # ty: ignore[invalid-return-type]

    def insert_row_in_cache(
        self,
        y: int,
        row: Row,
        vault: Table,
    ) -> Row:
        """Insert a row in cache and table element at position y."""
        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)  # ty: ignore[invalid-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  # ty: ignore[invalid-return-type]

    def delete_row_in_cache(
        self,
        y: int,
        vault: Table,
    ) -> None:
        """Delete the row at position y from cache and table element."""
        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)  # ty: ignore[invalid-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:
        """Update the cache and table element with a column at position x."""
        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)  # ty: ignore[invalid-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  # ty: ignore[invalid-return-type]

    def insert_col_in_cache(
        self,
        x: int,
        column: Column,
        vault: Table,
    ) -> Column:
        """Insert a column in cache and table element at position x."""
        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)  # ty: ignore[invalid-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  # ty: ignore[invalid-return-type]

    def delete_col_in_cache(
        self,
        x: int,
        vault: Table,
    ) -> None:
        """Delete the column at position x from cache and table element."""
        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)  # ty: ignore[invalid-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__ = (
    "cached_width",
    "col_elements",
    "col_map",
    "row_elements",
    "row_map",
)

cached_width instance-attribute

cached_width: int | None = None

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

Initialize a TableCache.

Source code in odfdo/table_cache.py
421
422
423
424
425
426
427
def __init__(self) -> None:
    """Initialize a TableCache."""
    self.row_map: list[int] = []
    self.col_map: list[int] = []
    self.row_elements: dict[int, Row] = {}
    self.col_elements: dict[int, Column] = {}
    self.cached_width: int | None = None

__str__

__str__() -> str
Source code in odfdo/table_cache.py
429
430
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
500
501
502
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
496
497
498
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

Clear the indexed cached columns.

Source code in odfdo/table_cache.py
473
474
475
476
def clear_col_indexes(self) -> None:
    """Clear the indexed cached columns."""
    self.col_elements = {}
    self.cached_width = None

clear_row_indexes

clear_row_indexes() -> None

Clear the indexed cached rows.

Source code in odfdo/table_cache.py
468
469
470
471
def clear_row_indexes(self) -> None:
    """Clear the indexed cached rows."""
    self.row_elements = {}
    self.cached_width = None

col_idx

col_idx(position: int) -> int | None

Find column index in the map from the position.

Source code in odfdo/table_cache.py
485
486
487
488
489
490
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

Return the length of the column map.

Source code in odfdo/table_cache.py
492
493
494
def col_map_length(self) -> int:
    """Return the length of the column map."""
    return len(self.col_map)

copy classmethod

copy(source: TableCache) -> TableCache

Return a copy of the table cache.

Source code in odfdo/table_cache.py
432
433
434
435
436
437
438
439
@classmethod
def copy(cls, source: TableCache) -> TableCache:
    """Return a copy of the table cache."""
    tc = cls()
    tc.row_map = source.row_map[:]
    tc.col_map = source.col_map[:]
    tc.cached_width = source.cached_width
    return tc

delete_col_in_cache

delete_col_in_cache(x: int, vault: Table) -> None

Delete the column at position x from cache and table element.

Source code in odfdo/table_cache.py
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
def delete_col_in_cache(
    self,
    x: int,
    vault: Table,
) -> None:
    """Delete the column at position x from cache and table element."""
    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)  # ty: ignore[invalid-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

Delete the row at position y from cache and table element.

Source code in odfdo/table_cache.py
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
def delete_row_in_cache(
    self,
    y: int,
    vault: Table,
) -> None:
    """Delete the row at position y from cache and table element."""
    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)  # ty: ignore[invalid-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
441
442
443
444
445
446
447
448
449
450
451
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

Insert a column in cache and table element at position x.

Source code in odfdo/table_cache.py
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
def insert_col_in_cache(
    self,
    x: int,
    column: Column,
    vault: Table,
) -> Column:
    """Insert a column in cache and table element at position x."""
    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)  # ty: ignore[invalid-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  # ty: ignore[invalid-return-type]

insert_col_map_once

insert_col_map_once(repeated: int) -> None

Insert a repeated column count into the column map.

Source code in odfdo/table_cache.py
520
521
522
523
def insert_col_map_once(self, repeated: int) -> None:
    """Insert a repeated column count into the column map."""
    self.col_map = _insert_map_once(self.col_map, len(self.col_map), repeated)
    self.cached_width = None

insert_row_in_cache

insert_row_in_cache(y: int, row: Row, vault: Table) -> Row

Insert a row in cache and table element at position y.

Source code in odfdo/table_cache.py
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
def insert_row_in_cache(
    self,
    y: int,
    row: Row,
    vault: Table,
) -> Row:
    """Insert a row in cache and table element at position y."""
    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)  # ty: ignore[invalid-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  # ty: ignore[invalid-return-type]

insert_row_map_once

insert_row_map_once(repeated: int) -> None

Insert a repeated row count into the row map.

Source code in odfdo/table_cache.py
512
513
514
515
def insert_row_map_once(self, repeated: int) -> None:
    """Insert a repeated row count into the row map."""
    self.row_map = _insert_map_once(self.row_map, len(self.row_map), repeated)
    self.cached_width = None

make_col_map

make_col_map(
    idx_repeated_sequence: list[tuple[int, int]],
) -> None

Build the column map from repeated sequence data.

Source code in odfdo/table_cache.py
533
534
535
536
def make_col_map(self, idx_repeated_sequence: list[tuple[int, int]]) -> None:
    """Build the column map from repeated sequence data."""
    self.col_map = _make_cache_map(idx_repeated_sequence)
    self.cached_width = None

make_row_map

make_row_map(
    idx_repeated_sequence: list[tuple[int, int]],
) -> None

Build the row map from repeated sequence data.

Source code in odfdo/table_cache.py
528
529
530
531
def make_row_map(self, idx_repeated_sequence: list[tuple[int, int]]) -> None:
    """Build the row map from repeated sequence data."""
    self.row_map = _make_cache_map(idx_repeated_sequence)
    self.cached_width = None

row_idx

row_idx(position: int) -> int | None

Find row index in the map from the position.

Source code in odfdo/table_cache.py
478
479
480
481
482
483
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

Update the cache and table element with a column at position x.

Source code in odfdo/table_cache.py
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
def set_col_in_cache(
    self,
    x: int,
    column: Column,
    vault: Table,
) -> Column:
    """Update the cache and table element with a column at position x."""
    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)  # ty: ignore[invalid-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  # ty: ignore[invalid-return-type]

set_row_in_cache

set_row_in_cache(
    y: int, row: Row, vault: Table, clone: bool
) -> Row

Update the cache and table element with a row at position y.

Source code in odfdo/table_cache.py
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
def set_row_in_cache(
    self,
    y: int,
    row: Row,
    vault: Table,
    clone: bool,
) -> Row:
    """Update the cache and table element with a row at position y."""
    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)  # ty: ignore[invalid-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  # ty: ignore[invalid-return-type]

store_col

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

Store Column in cache.

Source code in odfdo/table_cache.py
508
509
510
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
504
505
506
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
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
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
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
    return cache_map[:odf_idx] + [(x - repeated) for x in cache_map[odf_idx + 1 :]]

_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
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
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
86
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
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
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
            is_repeated += deleting
            if is_repeated > 1:
                delete_item._set_repeated(is_repeated)
            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