55# The MIT License (MIT)
66#
77# Copyright (c) 2017-2020 Kestrel Technology LLC
8- # Copyright (c) 2020-2022 Henny Sipma
9- # Copyright (c) 2023 Aarno Labs LLC
8+ # Copyright (c) 2020-2022 Henny B. Sipma
9+ # Copyright (c) 2023-2024 Aarno Labs LLC
1010#
1111# Permission is hereby granted, free of charge, to any person obtaining a copy
1212# of this software and associated documentation files (the "Software"), to deal
2626# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2727# SOFTWARE.
2828# ------------------------------------------------------------------------------
29+ """Dictionary of indexed expressions for an individual function."""
2930
3031import os
3132
3233import xml .etree .ElementTree as ET
3334
34- from typing import List , TYPE_CHECKING
35+ from typing import Callable , Dict , List , Mapping , TYPE_CHECKING
3536
3637import chc .util .fileutil as UF
38+ from chc .util .IndexedTable import IndexedTableValue
3739import chc .util .IndexedTable as IT
3840
3941from chc .invariants .CFunDictionaryRecord import xprregistry
@@ -71,6 +73,15 @@ def __init__(self, vd: "CFunVarDictionary", xnode: ET.Element) -> None:
7173 self .xpr_list_table ,
7274 self .xpr_list_list_table
7375 ]
76+ self ._objmaps : Dict [
77+ str , Callable [[], Mapping [int , IndexedTableValue ]]] = {
78+ "numerical" : self .get_numerical_map ,
79+ "symbol" : self .get_symbol_map ,
80+ "variable" : self .get_variable_map ,
81+ "xcst" : self .get_xcst_map ,
82+ "xpr" : self .get_xpr_map ,
83+ "xprlist" : self .get_xpr_list_map ,
84+ "xprlistlist" : self .get_xpr_list_list_map }
7485 self .initialize (xnode )
7586
7687 @property
@@ -85,18 +96,27 @@ def get_numerical(self, ix: int) -> CXNumerical:
8596 else :
8697 raise UF .CHCError ("Illegal numerical index value: " + str (ix ))
8798
99+ def get_numerical_map (self ) -> Dict [int , IndexedTableValue ]:
100+ return self .numerical_table .objectmap (self .get_numerical )
101+
88102 def get_symbol (self , ix : int ) -> CXSymbol :
89103 if ix > 0 :
90104 return CXSymbol (self , self .symbol_table .retrieve (ix ))
91105 else :
92106 raise UF .CHCError ("Illegal symbol index value: " + str (ix ))
93107
108+ def get_symbol_map (self ) -> Dict [int , IndexedTableValue ]:
109+ return self .symbol_table .objectmap (self .get_symbol )
110+
94111 def get_variable (self , ix : int ) -> CXVariable :
95112 if ix > 0 :
96113 return CXVariable (self , self .variable_table .retrieve (ix ))
97114 else :
98115 raise UF .CHCError ("Illegal variable index value: " + str (ix ))
99116
117+ def get_variable_map (self ) -> Dict [int , IndexedTableValue ]:
118+ return self .variable_table .objectmap (self .get_variable )
119+
100120 def get_xcst (self , ix : int ) -> CXConstant :
101121 if ix > 0 :
102122 return xprregistry .mk_instance (
@@ -106,6 +126,9 @@ def get_xcst(self, ix: int) -> CXConstant:
106126 else :
107127 raise UF .CHCError ("Illegal constant index value: " + str (ix ))
108128
129+ def get_xcst_map (self ) -> Dict [int , IndexedTableValue ]:
130+ return self .xcst_table .objectmap (self .get_xcst )
131+
109132 def get_xpr (self , ix : int ) -> CXXpr :
110133 if ix > 0 :
111134 return xprregistry .mk_instance (
@@ -115,18 +138,27 @@ def get_xpr(self, ix: int) -> CXXpr:
115138 else :
116139 raise UF .CHCError ("Illegal xpr index value: " + str (ix ))
117140
141+ def get_xpr_map (self ) -> Dict [int , IndexedTableValue ]:
142+ return self .xpr_table .objectmap (self .get_xpr )
143+
118144 def get_xpr_list (self , ix : int ) -> CXprList :
119145 if ix > 0 :
120146 return CXprList (self , self .xpr_list_table .retrieve (ix ))
121147 else :
122148 raise UF .CHCError ("Illegal xpr-list index value: " + str (ix ))
123149
150+ def get_xpr_list_map (self ) -> Dict [int , IndexedTableValue ]:
151+ return self .xpr_list_table .objectmap (self .get_xpr_list )
152+
124153 def get_xpr_list_list (self , ix : int ) -> CXprListList :
125154 if ix > 0 :
126155 return CXprListList (self , self .xpr_list_list_table .retrieve (ix ))
127156 else :
128157 raise UF .CHCError ("Illegal xpr-list-list index value: " + str (ix ))
129158
159+ def get_xpr_list_list_map (self ) -> Dict [int , IndexedTableValue ]:
160+ return self .xpr_list_list_table .objectmap (self .get_xpr_list_list )
161+
130162 # ------------ Provide read_xml service ----------------------------------
131163
132164 # TBD
@@ -149,6 +181,21 @@ def initialize(self, xnode: ET.Element, force: bool = False) -> None:
149181
150182 # ------------------ Printing --------------------------------------------
151183
184+ def objectmap_to_string (self , name : str ) -> str :
185+ if name in self ._objmaps :
186+ objmap = self ._objmaps [name ]()
187+ lines : List [str ] = []
188+ if len (objmap ) == 0 :
189+ lines .append (f"\n Table for { name } is empty\n " )
190+ else :
191+ lines .append ("index value" )
192+ lines .append ("-" * 80 )
193+ for (ix , obj ) in objmap .items ():
194+ lines .append (str (ix ).rjust (3 ) + " " + str (obj ))
195+ return "\n " .join (lines )
196+ else :
197+ raise UF .CHCError (f"Name: { name } does not correspond to a table" )
198+
152199 def __str__ (self ) -> str :
153200 lines : List [str ] = []
154201 for t in self .tables :
0 commit comments