code.vegaprotocol.io/vega@v0.79.0/core/matching/pegged_orders.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package matching
    17  
    18  import "github.com/pkg/errors"
    19  
    20  var ErrUnknownPeggedOrderID = errors.New("unknow pegged order")
    21  
    22  type peggedOrders struct {
    23  	ids []string
    24  }
    25  
    26  func newPeggedOrders() *peggedOrders {
    27  	return &peggedOrders{
    28  		ids: []string{},
    29  	}
    30  }
    31  
    32  func (p *peggedOrders) Clear() {
    33  	p.ids = []string{}
    34  }
    35  
    36  func (p *peggedOrders) Add(id string) {
    37  	p.ids = append(p.ids, id)
    38  }
    39  
    40  func (p *peggedOrders) Delete(id string) error {
    41  	idx := -1
    42  	for i, v := range p.ids {
    43  		if v == id {
    44  			idx = i
    45  			break
    46  		}
    47  	}
    48  
    49  	if idx == -1 {
    50  		return ErrUnknownPeggedOrderID
    51  	}
    52  
    53  	if idx < len(p.ids)-1 {
    54  		copy(p.ids[idx:], p.ids[idx+1:])
    55  	}
    56  	p.ids[len(p.ids)-1] = ""
    57  	p.ids = p.ids[:len(p.ids)-1]
    58  
    59  	return nil
    60  }
    61  
    62  func (p *peggedOrders) Exists(id string) bool {
    63  	for _, v := range p.ids {
    64  		if v == id {
    65  			return true
    66  		}
    67  	}
    68  
    69  	return false
    70  }
    71  
    72  func (p *peggedOrders) Iter() []string {
    73  	return p.ids
    74  }
    75  
    76  func (p *peggedOrders) Len() int {
    77  	return len(p.ids)
    78  }