@@ -31,17 +31,32 @@ class HaloSchemeEntry(EnrichedTuple):
3131
3232 __rargs__ = ('loc_indices' , 'loc_dirs' , 'halos' , 'dims' )
3333
34- def __init__ ( self , loc_indices , loc_dirs , halos , dims , getters = None ):
35- self . loc_indices = frozendict (loc_indices )
36- self . loc_dirs = frozendict ( loc_dirs )
37- self . halos = frozenset ( halos )
38- self . dims = frozenset ( dims )
34+ def __new__ ( cls , loc_indices , loc_dirs , halos , dims , getters = None ):
35+ items = [ frozendict (loc_indices ), frozendict ( loc_dirs ),
36+ frozenset ( halos ), frozenset ( dims )]
37+ kwargs = dict ( zip ( cls . __rargs__ , items ) )
38+ return super (). __new__ ( cls , * items , getters = cls . __rargs__ , ** kwargs )
3939
4040 def __hash__ (self ):
41- return hash ((self .loc_indices ,
42- self .loc_dirs ,
43- self .halos ,
44- self .dims ))
41+ return hash ((self .loc_indices , self .loc_dirs , self .halos , self .dims ))
42+
43+ def union (self , other ):
44+ """
45+ Return a new HaloSchemeEntry that is the union of this and `other`.
46+ The `loc_indices` and `loc_dirs` must be the same, otherwise an
47+ exception is raised.
48+ """
49+ if self .loc_indices != other .loc_indices or \
50+ self .loc_dirs != other .loc_dirs :
51+ raise HaloSchemeException (
52+ "Inconsistency found while building a HaloScheme"
53+ )
54+
55+ halos = self .halos | other .halos
56+ dims = self .dims | other .dims
57+
58+ return HaloSchemeEntry (self .loc_indices , self .loc_dirs , halos , dims ,
59+ getters = self .getters )
4560
4661
4762Halo = namedtuple ('Halo' , 'dim side' )
@@ -410,9 +425,11 @@ def add(self, f, hse):
410425 """
411426 Create a new HaloScheme that contains all entries in `self` plus the one
412427 passed in input. If `f` already exists in `self`, the old value is
413- overridden .
428+ augmented with `hse` (i.e., the halos are unioned) .
414429 """
415- fmapper = dict (self .fmapper .items ())
430+ fmapper = dict (self .fmapper )
431+ if f in fmapper :
432+ hse = fmapper [f ].union (hse )
416433 fmapper [f ] = hse
417434 return HaloScheme .build (fmapper , self .honored )
418435
0 commit comments