github.com/qioalice/ekago/v3@v3.3.2-0.20221202205325-5c262d586ee4/ekatyp/deque.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/deque
    27  Design draft about deque + 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/deque"
    37  )
    38  
    39  type (
    40  	// Deque is double-ended queue providing both of FIFO, LIFO design.
    41  	// Thanks to https://github.com/ef-ds/deque it's blazing fast.
    42  	// Deque must not be used by value, only by reference.
    43  	// Deque is thread UNSAFE. Use DequeSafe if you need thread safety version.
    44  	Deque = deque.Deque
    45  
    46  	// DequeSafe is the same as Deque but provides thread-safety operations
    47  	// protecting them by sync.Mutex.
    48  	// DequeSafe must not be used by value, only by reference.
    49  	DequeSafe struct {
    50  		q Deque
    51  		m sync.Mutex
    52  	}
    53  )
    54  
    55  // Init initializes or clears thread safe double ended queue.
    56  func (dq *DequeSafe) Init() *DequeSafe {
    57  	if dq == nil {
    58  		return NewDequeSafe()
    59  	}
    60  	dq.m.Lock()
    61  	dq.q.Init()
    62  	dq.m.Unlock()
    63  	return dq
    64  }
    65  
    66  // Len returns the number of elements of thread safe double ended queue.
    67  // The complexity is O(1).
    68  // DequeSafe must be not nil. Panic otherwise.
    69  func (dq *DequeSafe) Len() int {
    70  	dq.m.Lock()
    71  	ret := dq.q.Len()
    72  	dq.m.Unlock()
    73  	return ret
    74  }
    75  
    76  // Back returns the last element of thread safe double ended queue
    77  // or nil if the DequeSafe is empty.
    78  // The second, bool result indicates whether a valid value was returned;
    79  // if the deque is empty, false will be returned.
    80  // The complexity is O(1).
    81  // DequeSafe must be not nil. Panic otherwise.
    82  func (dq *DequeSafe) Back() (any, bool) {
    83  	dq.m.Lock()
    84  	elem, found := dq.q.Back()
    85  	dq.m.Unlock()
    86  	return elem, found
    87  }
    88  
    89  // Front returns the first element of thread safe double ended queue
    90  // or nil if the DequeSafe is empty.
    91  // The second, bool result indicates whether a valid value was returned;
    92  // if the deque is empty, false will be returned.
    93  // The complexity is O(1).
    94  // DequeSafe must be not nil. Panic otherwise.
    95  func (dq *DequeSafe) Front() (any, bool) {
    96  	dq.m.Lock()
    97  	elem, found := dq.q.Front()
    98  	dq.m.Unlock()
    99  	return elem, found
   100  }
   101  
   102  // PopBack retrieves and removes the current element from the back
   103  // of the thread safe double ended queue.
   104  // The second, bool result indicates whether a valid value was returned;
   105  // if the DequeSafe is empty, false will be returned.
   106  // The complexity is O(1).
   107  // DequeSafe must be not nil. Panic otherwise.
   108  func (dq *DequeSafe) PopBack() (any, bool) {
   109  	dq.m.Lock()
   110  	elem, found := dq.q.PopBack()
   111  	dq.m.Unlock()
   112  	return elem, found
   113  }
   114  
   115  // PopFront retrieves and removes the current element from the front
   116  // of the thread safe double ended queue.
   117  // The second, bool result indicates whether a valid value was returned;
   118  // if the DequeSafe is empty, false will be returned.
   119  // The complexity is O(1).
   120  // DequeSafe must be not nil. Panic otherwise.
   121  func (dq *DequeSafe) PopFront() (any, bool) {
   122  	dq.m.Lock()
   123  	elem, found := dq.q.PopFront()
   124  	dq.m.Unlock()
   125  	return elem, found
   126  }
   127  
   128  // PushBack adds value v to the the back of the thread safe double ended queue.
   129  // The complexity is O(1).
   130  // DequeSafe must be not nil. Panic otherwise.
   131  func (dq *DequeSafe) PushBack(v any) {
   132  	dq.m.Lock()
   133  	dq.q.PushBack(v)
   134  	dq.m.Unlock()
   135  }
   136  
   137  // PushFront adds value v to the the front of the thread safe double ended queue.
   138  // The complexity is O(1).
   139  // DequeSafe must be not nil. Panic otherwise.
   140  func (dq *DequeSafe) PushFront(v any) {
   141  	dq.m.Lock()
   142  	dq.q.PushFront(v)
   143  	dq.m.Unlock()
   144  }
   145  
   146  // NewDeque returns a new initialized thread UNSAFE double ended queue.
   147  func NewDeque() *Deque {
   148  	return deque.New()
   149  }
   150  
   151  // NewDequeSafe returns a new initialized thread safe double ended queue.
   152  func NewDequeSafe() *DequeSafe {
   153  	return new(DequeSafe)
   154  }