github.com/qioalice/ekago/v3@v3.3.2-0.20221202205325-5c262d586ee4/ekatyp/stack.go (about)

     1  /*
     2  MIT License
     3  
     4  Copyright (c) 2018 ef-ds
     5  
     6  Permission is hereby granted, free of charge, to any person obtaining a copy
     7  of this software and associated documentation files (the "Software"), to deal
     8  in the Software without restriction, including without limitation the rights
     9  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    10  copies of the Software, and to permit persons to whom the Software is
    11  furnished to do so, subject to the following conditions:
    12  
    13  The above copyright notice and this permission notice shall be included in all
    14  copies or substantial portions of the Software.
    15  
    16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    19  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    21  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    22  SOFTWARE.
    23  */
    24  
    25  /*
    26  Original package: https://github.com/ef-ds/stack
    27  Design draft about queue + thread safe:
    28  https://github.com/golang/proposal/blob/master/design/27935-unbounded-queue-package.md
    29  */
    30  
    31  package ekatyp
    32  
    33  import (
    34  	"sync"
    35  
    36  	"github.com/ef-ds/stack"
    37  )
    38  
    39  type (
    40  	// Stack is LIFO data structure.
    41  	// Thanks to https://github.com/ef-ds/stack it's blazing fast.
    42  	// Stack must not be used by value, only by reference.
    43  	// Stack is thread UNSAFE. Use StackSafe if you need thread safety version.
    44  	Stack = stack.Stack
    45  
    46  	// StackSafe is the same as Stack but provides thread-safety operations
    47  	// protecting them by sync.Mutex.
    48  	// StackSafe must not be used by value, only by reference.
    49  	StackSafe struct {
    50  		s Stack
    51  		m sync.Mutex
    52  	}
    53  )
    54  
    55  // Init initializes or clears thread safe stack.
    56  func (s *StackSafe) Init() *StackSafe {
    57  	if s == nil {
    58  		return NewStackSafe()
    59  	}
    60  	s.m.Lock()
    61  	s.s.Init()
    62  	s.m.Unlock()
    63  	return s
    64  }
    65  
    66  // Len returns the number of elements of thread safe stack.
    67  // The complexity is O(1).
    68  // StackSafe must be not nil. Panic otherwise.
    69  func (s *StackSafe) Len() int {
    70  	s.m.Lock()
    71  	ret := s.s.Len()
    72  	s.m.Unlock()
    73  	return ret
    74  }
    75  
    76  // Back returns the last element of thread safe stack or nil if the StackSafe is empty.
    77  // The second, bool result indicates whether a valid value was returned;
    78  // if the stack is empty, false will be returned.
    79  // The complexity is O(1).
    80  // StackSafe must be not nil. Panic otherwise.
    81  func (s *StackSafe) Back() (any, bool) {
    82  	s.m.Lock()
    83  	elem, found := s.s.Back()
    84  	s.m.Unlock()
    85  	return elem, found
    86  }
    87  
    88  // Pop retrieves and removes the current element from the back
    89  // of the thread safe stack.
    90  // The second, bool result indicates whether a valid value was returned;
    91  // if the stack is empty, false will be returned.
    92  // The complexity is O(1).
    93  // StackSafe must be not nil. Panic otherwise.
    94  func (s *StackSafe) Pop() (any, bool) {
    95  	s.m.Lock()
    96  	elem, found := s.s.Pop()
    97  	s.m.Unlock()
    98  	return elem, found
    99  }
   100  
   101  // Push adds value v to the the back of the thread safe stack.
   102  // The complexity is O(1).
   103  // StackSafe must be not nil. Panic otherwise.
   104  func (s *StackSafe) Push(v any) {
   105  	s.m.Lock()
   106  	s.s.Push(v)
   107  	s.m.Unlock()
   108  }
   109  
   110  // NewStack returns a new initialized thread UNSAFE stack.
   111  func NewStack() *Stack {
   112  	return stack.New()
   113  }
   114  
   115  // NewStackSafe returns a new initialized thread safe stack.
   116  func NewStackSafe() *StackSafe {
   117  	return new(StackSafe)
   118  }