package bbolt

import (
	
	
	
	
)

const pageHeaderSize = unsafe.Sizeof(page{})

const minKeysPerPage = 2

const branchPageElementSize = unsafe.Sizeof(branchPageElement{})
const leafPageElementSize = unsafe.Sizeof(leafPageElement{})

const (
	branchPageFlag   = 0x01
	leafPageFlag     = 0x02
	metaPageFlag     = 0x04
	freelistPageFlag = 0x10
)

const (
	bucketLeafFlag = 0x01
)

type pgid uint64

type page struct {
	id       pgid
	flags    uint16
	count    uint16
	overflow uint32
}

// typ returns a human readable page type string used for debugging.
func ( *page) () string {
	if (.flags & branchPageFlag) != 0 {
		return "branch"
	} else if (.flags & leafPageFlag) != 0 {
		return "leaf"
	} else if (.flags & metaPageFlag) != 0 {
		return "meta"
	} else if (.flags & freelistPageFlag) != 0 {
		return "freelist"
	}
	return fmt.Sprintf("unknown<%02x>", .flags)
}

// meta returns a pointer to the metadata section of the page.
func ( *page) () *meta {
	return (*meta)(unsafeAdd(unsafe.Pointer(), unsafe.Sizeof(*)))
}

// leafPageElement retrieves the leaf node by index
func ( *page) ( uint16) *leafPageElement {
	return (*leafPageElement)(unsafeIndex(unsafe.Pointer(), unsafe.Sizeof(*),
		leafPageElementSize, int()))
}

// leafPageElements retrieves a list of leaf nodes.
func ( *page) () []leafPageElement {
	if .count == 0 {
		return nil
	}
	var  []leafPageElement
	 := unsafeAdd(unsafe.Pointer(), unsafe.Sizeof(*))
	unsafeSlice(unsafe.Pointer(&), , int(.count))
	return 
}

// branchPageElement retrieves the branch node by index
func ( *page) ( uint16) *branchPageElement {
	return (*branchPageElement)(unsafeIndex(unsafe.Pointer(), unsafe.Sizeof(*),
		unsafe.Sizeof(branchPageElement{}), int()))
}

// branchPageElements retrieves a list of branch nodes.
func ( *page) () []branchPageElement {
	if .count == 0 {
		return nil
	}
	var  []branchPageElement
	 := unsafeAdd(unsafe.Pointer(), unsafe.Sizeof(*))
	unsafeSlice(unsafe.Pointer(&), , int(.count))
	return 
}

// dump writes n bytes of the page to STDERR as hex output.
func ( *page) ( int) {
	 := unsafeByteSlice(unsafe.Pointer(), 0, 0, )
	fmt.Fprintf(os.Stderr, "%x\n", )
}

type pages []*page

func ( pages) () int           { return len() }
func ( pages) (,  int)      { [], [] = [], [] }
func ( pages) (,  int) bool { return [].id < [].id }

// branchPageElement represents a node on a branch page.
type branchPageElement struct {
	pos   uint32
	ksize uint32
	pgid  pgid
}

// key returns a byte slice of the node key.
func ( *branchPageElement) () []byte {
	return unsafeByteSlice(unsafe.Pointer(), 0, int(.pos), int(.pos)+int(.ksize))
}

// leafPageElement represents a node on a leaf page.
type leafPageElement struct {
	flags uint32
	pos   uint32
	ksize uint32
	vsize uint32
}

// key returns a byte slice of the node key.
func ( *leafPageElement) () []byte {
	 := int(.pos)
	 :=  + int(.ksize)
	return unsafeByteSlice(unsafe.Pointer(), 0, , )
}

// value returns a byte slice of the node value.
func ( *leafPageElement) () []byte {
	 := int(.pos) + int(.ksize)
	 :=  + int(.vsize)
	return unsafeByteSlice(unsafe.Pointer(), 0, , )
}

// PageInfo represents human readable information about a page.
type PageInfo struct {
	ID            int
	Type          string
	Count         int
	OverflowCount int
}

type pgids []pgid

func ( pgids) () int           { return len() }
func ( pgids) (,  int)      { [], [] = [], [] }
func ( pgids) (,  int) bool { return [] < [] }

// merge returns the sorted union of a and b.
func ( pgids) ( pgids) pgids {
	// Return the opposite slice if one is nil.
	if len() == 0 {
		return 
	}
	if len() == 0 {
		return 
	}
	 := make(pgids, len()+len())
	mergepgids(, , )
	return 
}

// mergepgids copies the sorted union of a and b into dst.
// If dst is too small, it panics.
func (, ,  pgids) {
	if len() < len()+len() {
		panic(fmt.Errorf("mergepgids bad len %d < %d + %d", len(), len(), len()))
	}
	// Copy in the opposite slice if one is nil.
	if len() == 0 {
		copy(, )
		return
	}
	if len() == 0 {
		copy(, )
		return
	}

	// Merged will hold all elements from both lists.
	 := [:0]

	// Assign lead to the slice with a lower starting value, follow to the higher value.
	,  := , 
	if [0] < [0] {
		,  = , 
	}

	// Continue while there are elements in the lead.
	for len() > 0 {
		// Merge largest prefix of lead that is ahead of follow[0].
		 := sort.Search(len(), func( int) bool { return [] > [0] })
		 = append(, [:]...)
		if  >= len() {
			break
		}

		// Swap lead and follow.
		,  = , [:]
	}

	// Append what's left in follow.
	_ = append(, ...)
}