/usr/share/axiom-20170501/src/algebra/MSET.spad is in axiom-source 20170501-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | )abbrev domain MSET Multiset
++ Author:Stephen M. Watt, William H. Burge, Richard D. Jenks, Frederic Lehobey
++ Date Created:NK
++ Date Last Updated: 14 June 1994
++ Description:
++ A multiset is a set with multiplicities.
Multiset(S) : SIG == CODE where
S : SetCategory
SIG ==> MultisetAggregate S with
finiteAggregate
shallowlyMutable
multiset : () -> %
++ multiset()$D creates an empty multiset of domain D.
++
++X m:=multiset()@Multiset(Integer)
multiset : S -> %
++ multiset(s) creates a multiset with singleton s.
++
++X multiset(3)
multiset : List S -> %
++ multiset(ls) creates a multiset with elements from \spad{ls}.
++
++X s := multiset [1,2,3,4,5,4,3,2,3,4,5,6,7,4,10]
members : % -> List S
++ members(ms) returns a list of the elements of \spad{ms}
++ without their multiplicity. See also \spadfun{parts}.
++
++X s := multiset [1,2,3,4,5,4,3,2,3,4,5,6,7,4,10]
++X members(s)
remove : (S,%,Integer) -> %
++ remove(x,ms,number) removes at most \spad{number} copies of
++ element x if \spad{number} is positive, all of them if
++ \spad{number} equals zero, and all but at most \spad{-number} if
++ \spad{number} is negative.
++
++X s := multiset [1,2,3,4,5,4,3,2,3,4,5,6,7,4,10]
++X remove(3,s,2)
++X remove(3,s,0)
++X remove(3,s,-2)
remove : ( S -> Boolean ,%,Integer) -> %
++ remove(p,ms,number) removes at most \spad{number} copies of
++ elements x such that \spad{p(x)} is \spadfun{true}
++ if \spad{number} is positive, all of them if
++ \spad{number} equals zero, and all but at most \spad{-number} if
++ \spad{number} is negative.
++
++X f(x) == x < 4
++X s := multiset [1,2,3,4,5,4,3,2,3,4,5,6,7,4,10]
++X remove(f,s,2)
++X remove(f,s,0)
++X remove(f,s,-2)
remove_! : (S,%,Integer) -> %
++ remove!(x,ms,number) removes destructively at most \spad{number}
++ copies of element x if \spad{number} is positive, all
++ of them if \spad{number} equals zero, and all but at most
++ \spad{-number} if \spad{number} is negative.
++
++X s := multiset [1,2,3,4,5,4,3,2,3,4,5,6,7,4,10]
++X remove!(3,s,2)
++X s := multiset [1,2,3,4,5,4,3,2,3,4,5,6,7,4,10]
++X remove!(3,s,0)
++X s := multiset [1,2,3,4,5,4,3,2,3,4,5,6,7,4,10]
++X remove!(3,s,-2)
remove_! : ( S -> Boolean ,%,Integer) -> %
++ remove!(p,ms,number) removes destructively at most \spad{number}
++ copies of elements x such that \spad{p(x)} is
++ \spadfun{true} if \spad{number} is positive, all of them if
++ \spad{number} equals zero, and all but at most \spad{-number} if
++ \spad{number} is negative.
++
++X f(x) == x < 4
++X s := multiset [1,2,3,4,5,4,3,2,3,4,5,6,7,4,10]
++X remove!(f,s,2)
++X s := multiset [1,2,3,4,5,4,3,2,3,4,5,6,7,4,10]
++X remove!(f,s,0)
++X s := multiset [1,2,3,4,5,4,3,2,3,4,5,6,7,4,10]
++X remove!(f,s,-2)
CODE ==> add
Tbl ==> Table(S, Integer)
tbl ==> table$Tbl
Rep := Record(count: Integer, table: Tbl)
n: Integer
ms, m1, m2: %
t, t1, t2: Tbl
D ==> Record(entry: S, count: NonNegativeInteger)
K ==> Record(key: S, entry: Integer)
elt(t:Tbl, s:S):Integer ==
a := search(s,t)$Tbl
a case "failed" => 0
a::Integer
empty():% == [0,tbl()]
multiset():% == empty()
dictionary():% == empty() -- DictionaryOperations
set():% == empty()
brace():% == empty()
construct(l:List S):% ==
t := tbl()
n := 0
for e in l repeat
t.e := inc t.e
n := inc n
[n, t]
multiset(l:List S):% == construct l
bag(l:List S):% == construct l -- BagAggregate
dictionary(l:List S):% == construct l -- DictionaryOperations
set(l:List S):% == construct l
brace(l:List S):% == construct l
multiset(s:S):% == construct [s]
if S has ConvertibleTo InputForm then
convert(ms:%):InputForm ==
convert [convert("multiset"::Symbol)@InputForm,
convert(parts ms)@InputForm]
members(ms:%):List S == keys ms.table
coerce(ms:%):OutputForm ==
l: List OutputForm := empty()
t := ms.table
colon := ": " :: OutputForm
for e in keys t repeat
ex := e::OutputForm
n := t.e
item :=
n > 1 => hconcat [n :: OutputForm,colon, ex]
ex
l := cons(item,l)
brace l
duplicates(ms:%):List D == -- MultiDictionary
ld : List D := empty()
t := ms.table
for e in keys t | (n := t.e) > 1 repeat
ld := cons([e,n::NonNegativeInteger],ld)
ld
extract_!(ms:%):S == -- BagAggregate
empty? ms => error "extract: Empty multiset"
ms.count := dec ms.count
t := ms.table
e := inspect(t).key
if (n := t.e) > 1 then t.e := dec n
else remove_!(e,t)
e
inspect(ms:%):S == inspect(ms.table).key -- BagAggregate
insert_!(e:S,ms:%):% == -- BagAggregate
ms.count := inc ms.count
ms.table.e := inc ms.table.e
ms
member?(e:S,ms:%):Boolean == member?(e,keys ms.table)
empty?(ms:%):Boolean == ms.count = 0
#(ms:%):NonNegativeInteger == ms.count::NonNegativeInteger
count(e:S, ms:%):NonNegativeInteger == ms.table.e::NonNegativeInteger
remove_!(e:S, ms:%, max:Integer):% ==
zero? max => remove_!(e,ms)
t := ms.table
if member?(e, keys t) then
((n := t.e) <= max) =>
remove_!(e,t)
ms.count := ms.count-n
max > 0 =>
t.e := n-max
ms.count := ms.count-max
(n := n+max) > 0 =>
t.e := -max
ms.count := ms.count-n
ms
remove_!(p: S -> Boolean, ms:%, max:Integer):% ==
zero? max => remove_!(p,ms)
t := ms.table
for e in keys t | p(e) repeat
((n := t.e) <= max) =>
remove_!(e,t)
ms.count := ms.count-n
max > 0 =>
t.e := n-max
ms.count := ms.count-max
(n := n+max) > 0 =>
t.e := -max
ms.count := ms.count-n
ms
remove(e:S, ms:%, max:Integer):% == remove_!(e, copy ms, max)
remove(p: S -> Boolean,ms:%,max:Integer):% == remove_!(p, copy ms, max)
remove_!(e:S, ms:%):% == -- DictionaryOperations
t := ms.table
if member?(e, keys t) then
ms.count := ms.count-t.e
remove_!(e, t)
ms
remove_!(p:S ->Boolean, ms:%):% == -- DictionaryOperations
t := ms.table
for e in keys t | p(e) repeat
ms.count := ms.count-t.e
remove_!(e, t)
ms
select_!(p: S -> Boolean, ms:%):% == -- DictionaryOperations
remove_!((s1:S):Boolean+->not p(s1), ms)
removeDuplicates_!(ms:%):% == -- MultiDictionary
t := ms.table
l := keys t
for e in l repeat t.e := 1
ms.count := #l
ms
insert_!(e:S,ms:%,more:NonNegativeInteger):% == -- MultiDictionary
ms.count := ms.count+more
ms.table.e := ms.table.e+more
ms
map_!(f: S->S, ms:%):% == -- HomogeneousAggregate
t := ms.table
t1 := tbl()
for e in keys t repeat
t1.f(e) := t.e
remove_!(e, t)
ms.table := t1
ms
map(f: S -> S, ms:%):% == map_!(f, copy ms) -- HomogeneousAggregate
parts(m:%):List S ==
l := empty()$List(S)
t := m.table
for e in keys t repeat
for i in 1..t.e repeat
l := cons(e,l)
l
union(m1:%, m2:%):% ==
t := tbl()
t1:= m1.table
t2:= m2.table
for e in keys t1 repeat t.e := t1.e
for e in keys t2 repeat t.e := t2.e + t.e
[m1.count + m2.count, t]
intersect(m1:%, m2:%):% ==
t := tbl()
t1:= m1.table
t2:= m2.table
n := 0
for e in keys t1 repeat
m := min(t1.e,t2.e)
m > 0 =>
m := t1.e + t2.e
t.e := m
n := n + m
[n, t]
difference(m1:%, m2:%):% ==
t := tbl()
t1:= m1.table
t2:= m2.table
n := 0
for e in keys t1 repeat
k1 := t1.e
k2 := t2.e
k1 > 0 and k2 = 0 =>
t.e := k1
n := n + k1
n = 0 => empty()
[n, t]
symmetricDifference(m1:%, m2:%):% ==
union(difference(m1,m2), difference(m2,m1))
m1 = m2 ==
m1.count ^= m2.count => false
t1 := m1.table
t2 := m2.table
for e in keys t1 repeat
t1.e ^= t2.e => return false
for e in keys t2 repeat
t1.e ^= t2.e => return false
true
m1 < m2 ==
m1.count >= m2.count => false
t1 := m1.table
t2 := m2.table
for e in keys t1 repeat
t1.e > t2.e => return false
m1.count < m2.count
subset?(m1:%, m2:%):Boolean ==
m1.count > m2.count => false
t1 := m1.table
t2 := m2.table
for e in keys t1 repeat t1.e > t2.e => return false
true
|