@@ -562,6 +562,7 @@ def flatten(seq, scalarp=is_scalar_or_string):
562562 yield from flatten (item , scalarp )
563563
564564
565+ @_api .deprecated ("3.8" )
565566class Stack :
566567 """
567568 Stack of elements with a movable cursor.
@@ -668,6 +669,61 @@ def remove(self, o):
668669 self .push (elem )
669670
670671
672+ class _Stack :
673+ """
674+ Stack of elements with a movable cursor.
675+
676+ Mimics home/back/forward in a web browser.
677+ """
678+
679+ def __init__ (self ):
680+ self ._pos = - 1
681+ self ._elements = []
682+
683+ def clear (self ):
684+ """Empty the stack."""
685+ self ._pos = - 1
686+ self ._elements = []
687+
688+ def __call__ (self ):
689+ """Return the current element, or None."""
690+ return self ._elements [self ._pos ] if self ._elements else None
691+
692+ def __len__ (self ):
693+ return len (self ._elements )
694+
695+ def __getitem__ (self , ind ):
696+ return self ._elements [ind ]
697+
698+ def forward (self ):
699+ """Move the position forward and return the current element."""
700+ self ._pos = min (self ._pos + 1 , len (self ._elements ) - 1 )
701+ return self ()
702+
703+ def back (self ):
704+ """Move the position back and return the current element."""
705+ self ._pos = max (self ._pos - 1 , 0 )
706+ return self ()
707+
708+ def push (self , o ):
709+ """
710+ Push *o* to the stack after the current position, and return *o*.
711+
712+ Discard all later elements.
713+ """
714+ self ._elements [self ._pos + 1 :] = [o ]
715+ self ._pos = len (self ._elements ) - 1
716+ return o
717+
718+ def home (self ):
719+ """
720+ Push the first element onto the top of the stack.
721+
722+ The first element is returned.
723+ """
724+ return self .push (self ._elements [0 ]) if self ._elements else None
725+
726+
671727def safe_masked_invalid (x , copy = False ):
672728 x = np .array (x , subok = True , copy = copy )
673729 if not x .dtype .isnative :
0 commit comments