| 1 | """ |
| 2 | Finite Enumerated Sets |
| 3 | """ |
| 4 | #***************************************************************************** |
| 5 | # Copyright (C) 2009 Florent Hivert <Florent.Hivert@univ-rouen.fr> |
| 6 | # |
| 7 | # Distributed under the terms of the GNU General Public License (GPL) |
| 8 | # |
| 9 | # This code is distributed in the hope that it will be useful, |
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | # General Public License for more details. |
| 13 | # |
| 14 | # The full text of the GPL is available at: |
| 15 | # |
| 16 | # http://www.gnu.org/licenses/ |
| 17 | #****************************************************************************** |
| 18 | |
| 19 | from sage.structure.parent import Parent |
| 20 | from sage.structure.unique_representation import UniqueRepresentation |
| 21 | from sage.categories.finite_enumerated_sets import FiniteEnumeratedSets |
| 22 | from sage.rings.integer import Integer |
| 23 | |
| 24 | ################################################################# |
| 25 | class FiniteEnumeratedSet(UniqueRepresentation, Parent): |
| 26 | """ |
| 27 | The class of finite enumerated set |
| 28 | |
| 29 | Returns the finite enumerated set with elements in ``elements`` where |
| 30 | ``element`` is any (finite) iterable object. There is no check that |
| 31 | ``elements`` does not contains twice the same value so that one can build |
| 32 | multisets. |
| 33 | |
| 34 | EXAMPLES:: |
| 35 | |
| 36 | sage: S = FiniteEnumeratedSet([1, 2, 3]) |
| 37 | sage: S |
| 38 | {1, 2, 3} |
| 39 | sage: S.list() |
| 40 | [1, 2, 3] |
| 41 | sage: S.cardinality() |
| 42 | 3 |
| 43 | sage: S.random_element() |
| 44 | 1 |
| 45 | sage: S.first() |
| 46 | 1 |
| 47 | sage: S.check() |
| 48 | |
| 49 | Note that being and enumerated set, the result depend on the order:: |
| 50 | |
| 51 | sage: S1 = FiniteEnumeratedSet((1, 2, 3)) |
| 52 | sage: S1 |
| 53 | {1, 2, 3} |
| 54 | sage: S1.list() |
| 55 | [1, 2, 3] |
| 56 | sage: S1 == S |
| 57 | True |
| 58 | sage: S2 = FiniteEnumeratedSet((2, 1, 3)) |
| 59 | sage: S2 == S |
| 60 | False |
| 61 | """ |
| 62 | |
| 63 | @staticmethod |
| 64 | def __classcall__(cls, iterable): |
| 65 | """ |
| 66 | TESTS:: |
| 67 | |
| 68 | sage: S1 = FiniteEnumeratedSet([1, 2, 3]) |
| 69 | sage: S2 = FiniteEnumeratedSet((1, 2, 3)) |
| 70 | sage: S1 is S2 |
| 71 | True |
| 72 | """ |
| 73 | return super(FiniteEnumeratedSet, cls).__classcall__(cls, tuple(iterable)) |
| 74 | |
| 75 | def __init__(self, elements): |
| 76 | """ |
| 77 | TESTS:: |
| 78 | |
| 79 | sage: S = FiniteEnumeratedSet([1,2,3]) |
| 80 | sage: S == loads(dumps(S)) |
| 81 | True |
| 82 | """ |
| 83 | self._elements = elements |
| 84 | Parent.__init__(self, category = FiniteEnumeratedSets()) |
| 85 | |
| 86 | def __repr__(self): |
| 87 | """ |
| 88 | TESTS:: |
| 89 | |
| 90 | sage: S = FiniteEnumeratedSet([1,2,3]) |
| 91 | sage: repr(S) |
| 92 | '{1, 2, 3}' |
| 93 | """ |
| 94 | return "{"+str(self._elements)[1:-1] + '}' |
| 95 | |
| 96 | def __contains__(self, x): |
| 97 | """ |
| 98 | TESTS:: |
| 99 | |
| 100 | sage: S = FiniteEnumeratedSet([1,2,3]) |
| 101 | sage: 1 in S |
| 102 | True |
| 103 | sage: 2 in S |
| 104 | True |
| 105 | sage: 4 in S |
| 106 | False |
| 107 | sage: ZZ in S |
| 108 | False |
| 109 | """ |
| 110 | return x in self._elements |
| 111 | |
| 112 | def list(self): |
| 113 | """ |
| 114 | TESTS:: |
| 115 | |
| 116 | sage: S = FiniteEnumeratedSet([1,2,3]) |
| 117 | sage: S.list() |
| 118 | [1, 2, 3] |
| 119 | """ |
| 120 | return list(self._elements) |
| 121 | |
| 122 | def an_element(self): |
| 123 | """ |
| 124 | TESTS:: |
| 125 | |
| 126 | sage: S = FiniteEnumeratedSet([1,2,3]) |
| 127 | sage: S.an_element() |
| 128 | 1 |
| 129 | """ |
| 130 | return self._elements[0] |
| 131 | |
| 132 | def cardinality(self): |
| 133 | """ |
| 134 | TESTS:: |
| 135 | |
| 136 | sage: S = FiniteEnumeratedSet([1,2,3]) |
| 137 | sage: S.cardinality() |
| 138 | 3 |
| 139 | """ |
| 140 | return Integer(len(self._elements)) |
| 141 | |