|
39 | 39 | else:
|
40 | 40 | string_types = (str, bytes)
|
41 | 41 |
|
| 42 | +def _dimsprod(dims): |
| 43 | + out = 1 |
| 44 | + for x in dims: |
| 45 | + out *= x |
| 46 | + return out |
| 47 | + |
42 | 48 | class asdtype(Interpretation):
|
43 | 49 | # makes __doc__ attribute mutable before Python 3.3
|
44 | 50 | __metaclass__ = type.__new__(type, "type", (Interpretation.__metaclass__,), {})
|
@@ -100,7 +106,8 @@ def empty(self):
|
100 | 106 | return numpy.empty((0,) + self.todims, dtype=self.todtype)
|
101 | 107 |
|
102 | 108 | def compatible(self, other):
|
103 |
| - return isinstance(other, (asdtype, asarray)) and self.todtype == other.todtype and self.todims == other.todims |
| 109 | + return (isinstance(other, (asdtype, asarray)) and self.todtype == other.todtype and self.todims == other.todims) or \ |
| 110 | + (isinstance(other, asstlbitset) and self.todtype == other.dtype and self.todims == (other.numbytes,)) |
104 | 111 |
|
105 | 112 | def numitems(self, numbytes, numentries):
|
106 | 113 | return numbytes // self.fromdtype.itemsize
|
@@ -140,6 +147,8 @@ def clip(self, destination, itemstart, itemstop, entrystart, entrystop):
|
140 | 147 | assert itemstart % product == 0
|
141 | 148 | assert itemstop % product == 0
|
142 | 149 | return destination[itemstart // product : itemstop // product]
|
| 150 | + # FIXME: isn't the above equivalent to the following? |
| 151 | + # return destination[entrystart:entrystop] |
143 | 152 |
|
144 | 153 | def finalize(self, destination):
|
145 | 154 | return destination
|
@@ -195,8 +204,46 @@ def finalize(self, destination):
|
195 | 204 | array, stop = destination
|
196 | 205 | return array[:stop]
|
197 | 206 |
|
198 |
| -def _dimsprod(dims): |
199 |
| - out = 1 |
200 |
| - for x in dims: |
201 |
| - out *= x |
202 |
| - return out |
| 207 | +class asstlbitset(Interpretation): |
| 208 | + # makes __doc__ attribute mutable before Python 3.3 |
| 209 | + __metaclass__ = type.__new__(type, "type", (Interpretation.__metaclass__,), {}) |
| 210 | + |
| 211 | + dtype = numpy.dtype(numpy.bool_) |
| 212 | + |
| 213 | + def __init__(self, numbytes): |
| 214 | + self.numbytes = numbytes |
| 215 | + |
| 216 | + def __repr__(self): |
| 217 | + return self.identifier |
| 218 | + |
| 219 | + @property |
| 220 | + def identifier(self): |
| 221 | + return "asstlbitset({0})".format(self.numbytes) |
| 222 | + |
| 223 | + def empty(self): |
| 224 | + return numpy.empty((0,), dtype=self.dtype) |
| 225 | + |
| 226 | + def compatible(self, other): |
| 227 | + return (isinstance(other, asstlbitset) and self.numbytes == other.numbytes) or \ |
| 228 | + (isinstance(other, (asdtype, asarray)) and self.dtype == other.todtype and (self.numbytes,) == other.todims) |
| 229 | + |
| 230 | + def numitems(self, numbytes, numentries): |
| 231 | + return max(0, numbytes // self.dtype.itemsize - 10*numentries) |
| 232 | + |
| 233 | + def source_numitems(self, source): |
| 234 | + return _dimsprod(source.shape) |
| 235 | + |
| 236 | + def fromroot(self, data, offsets, local_entrystart, local_entrystop): |
| 237 | + return data.view(self.dtype).reshape((-1, self.numbytes + 10))[local_entrystart:local_entrystop,10:] |
| 238 | + |
| 239 | + def destination(self, numitems, numentries): |
| 240 | + return numpy.empty((numitems // self.numbytes, self.numbytes), dtype=self.dtype) |
| 241 | + |
| 242 | + def fill(self, source, destination, itemstart, itemstop, entrystart, entrystop): |
| 243 | + destination[entrystart:entrystop] = source |
| 244 | + |
| 245 | + def clip(self, destination, itemstart, itemstop, entrystart, entrystop): |
| 246 | + return destination[entrystart:entrystop] |
| 247 | + |
| 248 | + def finalize(self, destination): |
| 249 | + return destination |
0 commit comments