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
// SPDX-FileCopyrightText: 2022 Thomas Kramer
//
// SPDX-License-Identifier: AGPL-3.0-or-later

//! # Experimental
//!
//! Simplify usage of cell libraries.

// TODO: Remove when implemented.
#![allow(unused)]

use crate::{prelude::HierarchyBase, traits::HierarchyIds};

/// Identifier of a library.
#[derive(Copy, Clone, Debug, Hash, PartialOrd, PartialEq, Eq, Ord)]
pub struct LibraryId(usize);

pub struct LibraryWrapper<'a, T> {
    libraries: Vec<&'a T>,
    owned: T,
}

impl<'a, T> LibraryWrapper<'a, T> {
    /// Register a library.
    pub fn add_library(&mut self, library: &'a T) -> LibraryId {
        self.libraries.push(library);
        LibraryId(self.libraries.len())
    }

    fn get_library(&self, library_id: &LibraryId) -> &T {
        if library_id == &LibraryId(0) {
            &self.owned
        } else {
            self.libraries[library_id.0]
        }
    }

    /// Get reference to owned data and to libraries.
    fn libraries(&self) -> impl Iterator<Item = (LibraryId, &T)> {
        std::iter::once((LibraryId(0), &self.owned)).chain(
            self.libraries
                .iter()
                .enumerate()
                .map(|(id, lib)| (LibraryId(id + 1), *lib)),
        )
    }
}

impl<'a, T> HierarchyIds for LibraryWrapper<'a, T>
where
    T: HierarchyIds,
{
    type CellId = (LibraryId, T::CellId);
    type CellInstId = (LibraryId, T::CellInstId);
}

impl<'a, T> HierarchyBase for LibraryWrapper<'a, T>
where
    T: HierarchyBase,
{
    type NameType = T::NameType;

    /// Find a cell by name.
    /// Precedence is: Current data base, then libraries in the order where they where added.
    fn cell_by_name(&self, name: &str) -> Option<Self::CellId> {
        // Find in libraries.
        self.libraries()
            .flat_map(|(lib_id, lib)| lib.cell_by_name(name).map(|cell| (lib_id, cell)))
            .next()
    }

    fn cell_instance_by_name(
        &self,
        (lib_id, parent_cell): &Self::CellId,
        name: &str,
    ) -> Option<Self::CellInstId> {
        self.get_library(lib_id)
            .cell_instance_by_name(parent_cell, name)
            .map(|inst| (*lib_id, inst))
    }

    fn cell_name(&self, (lib_id, cell): &Self::CellId) -> Self::NameType {
        self.get_library(lib_id).cell_name(cell)
    }

    fn cell_instance_name(&self, (lib_id, cell_inst): &Self::CellInstId) -> Option<Self::NameType> {
        self.get_library(lib_id).cell_instance_name(cell_inst)
    }

    fn parent_cell(&self, (lib_id, cell_instance): &Self::CellInstId) -> Self::CellId {
        // self.get_library(lib_id)
        //     .parent_cell(cell_instance)
        unimplemented!()
    }

    fn template_cell(&self, (lib_id, cell_instance): &Self::CellInstId) -> Self::CellId {
        // self.get_library(lib_id)
        //     .template_cell(cell_instance)
        unimplemented!()
    }

    fn for_each_cell<F>(&self, f: F)
    where
        F: FnMut(Self::CellId) -> (),
    {
        unimplemented!()
    }

    fn for_each_cell_instance<F>(&self, cell: &Self::CellId, f: F)
    where
        F: FnMut(Self::CellInstId) -> (),
    {
        unimplemented!()
    }

    fn for_each_cell_dependency<F>(&self, cell: &Self::CellId, f: F)
    where
        F: FnMut(Self::CellId) -> (),
    {
        unimplemented!()
    }

    fn for_each_dependent_cell<F>(&self, cell: &Self::CellId, f: F)
    where
        F: FnMut(Self::CellId) -> (),
    {
        unimplemented!()
    }

    fn for_each_cell_reference<F>(&self, cell: &Self::CellId, f: F)
    where
        F: FnMut(Self::CellInstId) -> (),
    {
        unimplemented!()
    }

    fn num_child_instances(&self, cell: &Self::CellId) -> usize {
        unimplemented!()
    }

    fn num_cells(&self) -> usize {
        unimplemented!()
    }
}