@@ -14,15 +14,15 @@ const One = StaticInt{1}
14
14
15
15
Base. show (io:: IO , :: StaticInt{N} ) where {N} = print (io, " static($N )" )
16
16
17
- @pure StaticInt (N:: Int ) = StaticInt {N} ()
17
+ Base . @pure StaticInt (N:: Int ) = StaticInt {N} ()
18
18
StaticInt (N:: Integer ) = StaticInt (convert (Int, N))
19
19
StaticInt (:: StaticInt{N} ) where {N} = StaticInt {N} ()
20
20
StaticInt (:: Val{N} ) where {N} = StaticInt {N} ()
21
21
# Base.Val(::StaticInt{N}) where {N} = Val{N}()
22
22
Base. convert (:: Type{T} , :: StaticInt{N} ) where {T<: Number ,N} = convert (T, N)
23
23
Base. Bool (x:: StaticInt{N} ) where {N} = Bool (N)
24
24
Base. BigInt (x:: StaticInt{N} ) where {N} = BigInt (N)
25
- Base. Integer (x:: StaticInt{N} ) where {N} = x:: Int
25
+ Base. Integer (x:: StaticInt{N} ) where {N} = x
26
26
(:: Type{T} )(x:: StaticInt{N} ) where {T<: Integer ,N} = T (N)
27
27
(:: Type{T} )(x:: Int ) where {T<: StaticInt } = StaticInt (x)
28
28
Base. convert (:: Type{StaticInt{N}} , :: StaticInt{N} ) where {N} = StaticInt {N} ()
@@ -262,6 +262,11 @@ Base.any(::Tuple{Vararg{False}}) = false
262
262
263
263
Base. @pure nstatic (:: Val{N} ) where {N} = ntuple (i -> StaticInt (i), Val (N))
264
264
265
+ # I is a tuple of Int
266
+ @pure function _val_to_static (:: Val{I} ) where {I}
267
+ return ntuple (i -> StaticInt (getfield (I, i)), Val (length (I)))
268
+ end
269
+
265
270
@pure is_permuting (perm:: Tuple{Vararg{StaticInt,N}} ) where {N} = perm != = nstatic (Val (N))
266
271
267
272
permute (x:: Tuple , perm:: Tuple ) = eachop (getindex, x, perm)
@@ -272,7 +277,7 @@ function permute(x::Tuple{Vararg{Any,N}}, perm::Tuple{Vararg{Any,N}}) where {N}
272
277
return x
273
278
end
274
279
end
275
- permute (x:: Tuple , perm:: Val ) = permute (x, static (perm))
280
+ permute (x:: Tuple , perm:: Val ) = permute (x, _val_to_static (perm))
276
281
277
282
@generated function eachop (op, x, y, :: I ) where {I}
278
283
t = Expr (:tuple )
@@ -361,100 +366,11 @@ IfElse.ifelse(::True, x, y) = x
361
366
362
367
IfElse. ifelse (:: False , x, y) = y
363
368
364
- """
365
- StaticSymbol
366
-
367
- A statically typed `Symbol`.
368
- """
369
- struct StaticSymbol{s}
370
- StaticSymbol {s} () where {s} = new {s::Symbol} ()
371
- StaticSymbol (s:: Symbol ) = new {s} ()
372
- end
373
-
374
- Base. Symbol (:: StaticSymbol{s} ) where {s} = s:: Symbol
375
-
376
- Base. show (io:: IO , :: StaticSymbol{s} ) where {s} = print (io, " static(:$s )" )
377
-
378
- is_static (x) = is_static (typeof (x))
379
- is_static (:: Type{T} ) where {T<: StaticInt } = True ()
380
- is_static (:: Type{T} ) where {T<: StaticBool } = True ()
381
- is_static (:: Type{T} ) where {T<: StaticSymbol } = True ()
382
- is_static (:: Type{T} ) where {T} = False ()
383
-
384
- function _no_static_type (@nospecialize (x))
385
- error (" There is no static alternative for type $(typeof (x)) ." )
386
- end
387
-
388
369
"""
389
370
static(x)
390
371
391
372
Returns a static form of `x`.
392
373
"""
393
- function static (x)
394
- if is_static (x)
395
- return x
396
- else
397
- _no_static_type (x)
398
- end
399
- end
400
374
static (x:: Int ) = StaticInt (x)
401
375
static (x:: Bool ) = StaticBool (x)
402
- static (x:: Symbol ) = StaticSymbol (x)
403
- static (x:: Tuple{Vararg{Any}} ) = map (static, x)
404
- @generated static (:: Val{V} ) where {V} = :($ (static (V)))
405
-
406
-
407
- #=
408
- static_issubset
409
-
410
- A version of `issubset` sepecifically for `Tuple`s of static types, that is generated at
411
- compile time.
412
- =#
413
- @generated function static_issubset (:: L , :: R ) where {L<: Tuple ,R<: Tuple }
414
- N = length (L. parameters)
415
- R = length (R. parameters)
416
- if N < + M
417
- return :(ArrayInterface. False ())
418
- else
419
- for l in L. parameters
420
- found = false
421
- for b in rhs
422
- found |= a === b
423
- end
424
- found || return :(ArrayInterface. False ())
425
- end
426
- return :(ArrayInterface. True ())
427
- end
428
- end
429
-
430
- #=
431
- static_find_first_eq(::EQ, ::T) -> StaticInt
432
-
433
- Finds the position in the tuple `T` that is exactly equal to `EQ`. If `EQ` is not found
434
- then `Zero()` is returned.
435
- =#
436
- @generated function static_find_first_eq (:: EQ , :: T ) where {T<: Tuple }
437
- loop = true
438
- i = 1
439
- out = 0
440
- while loop
441
- p = T. parameters[i]
442
- if p === EQ
443
- out = i
444
- loop = false
445
- else
446
- i += 1
447
- end
448
- end
449
- return :(StaticInt ($ i))
450
- end
451
-
452
- #=
453
- static_find_all_in(x::Tuple, collection::Tuple) -> StaticInt
454
-
455
- Finds the position in the tuple `collection` that is exactly equal to each element of `x`.
456
- =#
457
- @inline function static_find_all_in (x:: Tuple{Vararg{Any,N}} , collection:: Tuple ) where {N}
458
- return ntuple (i -> static_find_first_eq (getfield (x, i), collection), Val (N))
459
- end
460
376
0 commit comments