oras.land/oras-go/v2@v2.5.1-0.20240520045656-aef90e4d04c4/internal/copyutil/stack.go (about)

     1  /*
     2  Copyright The ORAS Authors.
     3  Licensed under the Apache License, Version 2.0 (the "License");
     4  you may not use this file except in compliance with the License.
     5  You may obtain a copy of the License at
     6  
     7  http://www.apache.org/licenses/LICENSE-2.0
     8  
     9  Unless required by applicable law or agreed to in writing, software
    10  distributed under the License is distributed on an "AS IS" BASIS,
    11  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  See the License for the specific language governing permissions and
    13  limitations under the License.
    14  */
    15  
    16  package copyutil
    17  
    18  import (
    19  	ocispec "github.com/opencontainers/image-spec/specs-go/v1"
    20  )
    21  
    22  // NodeInfo represents information of a node that is being visited in
    23  // ExtendedCopy.
    24  type NodeInfo struct {
    25  	// Node represents a node in the graph.
    26  	Node ocispec.Descriptor
    27  	// Depth represents the depth of the node in the graph.
    28  	Depth int
    29  }
    30  
    31  // Stack represents a stack data structure that is used in ExtendedCopy for
    32  // storing node information.
    33  type Stack []NodeInfo
    34  
    35  // IsEmpty returns true if the stack is empty, otherwise returns false.
    36  func (s *Stack) IsEmpty() bool {
    37  	return len(*s) == 0
    38  }
    39  
    40  // Push pushes an item to the stack.
    41  func (s *Stack) Push(i NodeInfo) {
    42  	*s = append(*s, i)
    43  }
    44  
    45  // Pop pops the top item out of the stack.
    46  func (s *Stack) Pop() (NodeInfo, bool) {
    47  	if s.IsEmpty() {
    48  		return NodeInfo{}, false
    49  	}
    50  
    51  	last := len(*s) - 1
    52  	top := (*s)[last]
    53  	*s = (*s)[:last]
    54  	return top, true
    55  }