|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# ***************************************************************************** |
| 3 | +# Copyright (c) 2021, Intel Corporation All rights reserved. |
| 4 | +# |
| 5 | +# Redistribution and use in source and binary forms, with or without |
| 6 | +# modification, are permitted provided that the following conditions are met: |
| 7 | +# |
| 8 | +# Redistributions of source code must retain the above copyright notice, |
| 9 | +# this list of conditions and the following disclaimer. |
| 10 | +# |
| 11 | +# Redistributions in binary form must reproduce the above copyright notice, |
| 12 | +# this list of conditions and the following disclaimer in the documentation |
| 13 | +# and/or other materials provided with the distribution. |
| 14 | +# |
| 15 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 16 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 17 | +# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 18 | +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 19 | +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 20 | +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | +# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | +# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 25 | +# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | +# ***************************************************************************** |
| 27 | + |
| 28 | +from numba import types |
| 29 | +from numba.extending import ( |
| 30 | + models, |
| 31 | + register_model, |
| 32 | + make_attribute_wrapper, |
| 33 | +) |
| 34 | + |
| 35 | + |
| 36 | +class MultiIndexIteratorType(types.SimpleIteratorType): |
| 37 | + def __init__(self, iterable): |
| 38 | + self.parent = iterable |
| 39 | + yield_type = iterable.dtype |
| 40 | + name = "iter[{}->{}],{}".format( |
| 41 | + iterable, yield_type, iterable.name |
| 42 | + ) |
| 43 | + super(MultiIndexIteratorType, self).__init__(name, yield_type) |
| 44 | + |
| 45 | + |
| 46 | +@register_model(MultiIndexIteratorType) |
| 47 | +class MultiIndexIterModel(models.StructModel): |
| 48 | + def __init__(self, dmm, fe_type): |
| 49 | + members = [ |
| 50 | + ('parent', fe_type.parent), # reference to the index object |
| 51 | + ('state', types.CPointer(types.int64)), # iterator state (i.e. counter) |
| 52 | + ] |
| 53 | + super(MultiIndexIterModel, self).__init__(dmm, fe_type, members) |
| 54 | + |
| 55 | + |
| 56 | +class MultiIndexType(types.IterableType): |
| 57 | + |
| 58 | + def __init__(self, levels, codes, is_named=False): |
| 59 | + self.levels = levels |
| 60 | + self.codes = codes |
| 61 | + self.is_named = is_named |
| 62 | + super(MultiIndexType, self).__init__( |
| 63 | + name='MultiIndexType({}, {}, {})'.format(levels, codes, is_named)) |
| 64 | + |
| 65 | + @property |
| 66 | + def iterator_type(self): |
| 67 | + return MultiIndexIteratorType(self).iterator_type |
| 68 | + |
| 69 | + @property |
| 70 | + def dtype(self): |
| 71 | + nlevels = len(self.levels) |
| 72 | + levels_types = [self.levels.dtype] * nlevels if isinstance(self.levels, types.UniTuple) else self.levels |
| 73 | + return types.Tuple.from_types([level.dtype for level in levels_types]) |
| 74 | + |
| 75 | + @property |
| 76 | + def nlevels(self): |
| 77 | + return len(self.levels) |
| 78 | + |
| 79 | + @property |
| 80 | + def levels_types(self): |
| 81 | + if isinstance(self.levels, types.UniTuple): |
| 82 | + return [self.levels.dtype] * self.levels.count |
| 83 | + |
| 84 | + return self.levels |
| 85 | + |
| 86 | + @property |
| 87 | + def codes_types(self): |
| 88 | + if isinstance(self.codes, types.UniTuple): |
| 89 | + return [self.codes.dtype] * self.codes.count |
| 90 | + |
| 91 | + return self.codes |
| 92 | + |
| 93 | + |
| 94 | +@register_model(MultiIndexType) |
| 95 | +class MultiIndexModel(models.StructModel): |
| 96 | + def __init__(self, dmm, fe_type): |
| 97 | + |
| 98 | + levels_type = fe_type.levels |
| 99 | + codes_type = fe_type.codes |
| 100 | + name_type = types.unicode_type if fe_type.is_named else types.none # TO-DO: change to types.Optional |
| 101 | + members = [ |
| 102 | + ('levels', levels_type), |
| 103 | + ('codes', codes_type), |
| 104 | + ('name', name_type), |
| 105 | + ] |
| 106 | + models.StructModel.__init__(self, dmm, fe_type, members) |
| 107 | + |
| 108 | + |
| 109 | +make_attribute_wrapper(MultiIndexType, 'levels', '_levels') |
| 110 | +make_attribute_wrapper(MultiIndexType, 'codes', '_codes') |
| 111 | +make_attribute_wrapper(MultiIndexType, 'name', '_name') |
0 commit comments