
import sequences.util.*;


//
// reversed
//

inline reversed(forward seq) = Reversed(wrapSequence(seq));

private record Reversed[A] (
    seq : A
);

[A | LightweightSequence?(A)]
overload LightweightSequence?(static Reversed[A]) = true;

[A | CallDefined?(UnderlyingContainerConstructor, Static[A])]
overload UnderlyingContainerConstructor(static Reversed[A]) =
    UnderlyingContainerConstructor(A);

[A]
inline overload iterator(a:Reversed[A]) = reverseIterator(a.seq);

[A]
inline overload reverseIterator(a:Reversed[A]) = iterator(a.seq);



//
// size, index
//

[A | CallDefined?(size, A)]
inline overload size(a:Reversed[A]) = size(a.seq);

[A | CallDefined?(StaticSize, Static[A])]
overload StaticSize(static Reversed[A]) = StaticSize(A);

[A, I | CallDefined?(index, A, I) and CallDefined?(size, A)]
inline overload index(a:Reversed[A], i:I) =
    forward ..index(a.seq, size(a.seq) - i - 1);
