本文整理汇总了Golang中sort.IntSlice.Len方法的典型用法代码示例。如果您正苦于以下问题:Golang IntSlice.Len方法的具体用法?Golang IntSlice.Len怎么用?Golang IntSlice.Len使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sort.IntSlice
的用法示例。
在下文中一共展示了IntSlice.Len方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Test_InitSorted
func Test_InitSorted(t *testing.T) {
data := sort.IntSlice{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}
tree := Tree{}
tree.InitSorted(data.Len())
check(t, data, &tree, tree.root)
check_iter(t, data, &tree)
data = sort.IntSlice{}
f := map[int]struct{}{}
tree = Tree{}
for i := 100; i > 0; i-- {
v := rand.Intn(1000)
if _, ok := f[v]; ok {
continue
}
data = append(data, v)
}
sort.Sort(data)
tree.InitSorted(data.Len())
check(t, data, &tree, tree.root)
check_iter(t, data, &tree)
}
示例2: Quicksort
//Be careful to use same partioning as in lecture
//and return total number of comparisons
func Quicksort(input sort.IntSlice, ps pivotSelector) int {
if input.Len() < 2 {
return 0
}
input.Swap(0, ps(input))
i := 1
for j := 1; j < input.Len(); j++ {
if input.Less(j, 0) {
input.Swap(i, j)
i++
}
}
input.Swap(0, i-1)
comparisons := input.Len() - 1
comparisons += Quicksort(input[:i-1], ps)
comparisons += Quicksort(input[i:], ps)
return comparisons
}
示例3: saveUsingXrefStream
func (f *File) saveUsingXrefStream() error {
info, err := os.Stat(f.filename)
if err != nil {
return err
}
file, err := os.OpenFile(f.filename, os.O_RDWR|os.O_APPEND, 0666)
if err != nil {
return err
}
defer func() {
err := file.Close()
if err != nil {
panic(err)
}
}()
offset := info.Size() + 1
n, err := writeLineBreakTo(file)
if err != nil {
return err
}
offset += n
xrefs := map[Integer]crossReference{}
xrefs[0] = crossReference{0, 0, 65535}
free := sort.IntSlice{0}
for i := range f.objects {
switch typed := f.objects[i].(type) {
case crossReference:
// no-op, don't need to write unchanged objects to file
// however, we do need to handle the free list
// xrefs[Integer(i)] = typed
if typed[0] == 0 {
free = append(free, int(i))
}
case IndirectObject:
xrefs[Integer(i)] = crossReference{1, uint(offset - 1), typed.GenerationNumber}
n, err = typed.writeTo(file)
if err != nil {
return err
}
offset += n
n, err = writeLineBreakTo(file)
if err != nil {
return err
}
offset += n
case freeObject:
xrefs[Integer(i)] = crossReference{0, 0, uint(typed)}
free = append(free, int(i))
default:
panic(fmt.Sprintf("unhandled type: %T", typed))
}
}
// Figure out the highest object number to set Size properly
var maxObjNum uint
for objNum := range f.objects {
if objNum > maxObjNum {
maxObjNum = objNum
}
}
// add an xref for the xrefstream
xrefstreamObjectNumber := uint(maxObjNum + 1)
maxObjNum++
xref := crossReference{1, uint(offset - 1), 0}
xrefs[Integer(xrefstreamObjectNumber)] = xref
f.objects[xrefstreamObjectNumber] = xref
// fill in the free linked list
free.Sort()
for i := 0; i < free.Len()-1; i++ {
xref := xrefs[Integer(free[i])]
xref[1] = uint(free[i+1])
xrefs[Integer(free[i])] = xref
}
objects := make(sort.IntSlice, 0, len(xrefs))
for objectNumber := range xrefs {
objects = append(objects, int(objectNumber))
}
objects.Sort()
// group into consecutive sets
groups := []sort.IntSlice{}
groupStart := 0
for i := range objects {
if i == 0 {
continue
}
if objects[i] != objects[i-1]+1 {
groups = append(groups, objects[groupStart:i])
groupStart = i
//.........这里部分代码省略.........
示例4: saveUsingXrefTable
func (f *File) saveUsingXrefTable() error {
info, err := os.Stat(f.filename)
if err != nil {
return err
}
file, err := os.OpenFile(f.filename, os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
return err
}
defer func() {
err := file.Close()
if err != nil {
panic(err)
}
}()
offset := info.Size() + 1
n, err := writeLineBreakTo(file)
if err != nil {
return err
}
offset += n
xrefs := map[Integer]crossReference{}
xrefs[0] = crossReference{0, 0, 65535}
free := sort.IntSlice{}
for i := range f.objects {
switch typed := f.objects[i].(type) {
case crossReference:
// no-op, don't need to write unchanged objects to file
// however, we do need to handle the free list
// xrefs[Integer(i)] = typed
if typed[0] == 0 {
free = append(free, int(i))
}
case IndirectObject:
xrefs[Integer(i)] = crossReference{1, uint(offset - 1), typed.GenerationNumber}
n, err = typed.writeTo(file)
if err != nil {
return err
}
offset += n
n, err = writeLineBreakTo(file)
if err != nil {
return err
}
offset += n
case freeObject:
xrefs[Integer(i)] = crossReference{0, 0, uint(typed)}
free = append(free, int(i))
default:
panic(fmt.Sprintf("unhandled type: %T", typed))
}
}
// fill in the free linked list
free.Sort()
for i := 0; i < free.Len()-1; i++ {
xref := xrefs[Integer(free[i])]
xref[1] = uint(free[i+1])
xrefs[Integer(free[i])] = xref
}
objects := make(sort.IntSlice, 0, len(xrefs))
for objectNumber := range xrefs {
objects = append(objects, int(objectNumber))
}
objects.Sort()
// group into consecutive sets
groups := []sort.IntSlice{}
groupStart := 0
for i := range objects {
if i == 0 {
continue
}
if objects[i] != objects[i-1]+1 {
groups = append(groups, objects[groupStart:i])
groupStart = i
}
}
// add remaining group
groups = append(groups, objects[groupStart:])
// write as an xref table to file
fmt.Fprintf(file, "xref\n")
for _, group := range groups {
fmt.Fprintf(file, "%d %d\n", group[0], len(group))
for _, objectNumber := range group {
xref := xrefs[Integer(objectNumber)]
fmt.Fprintf(file, "%010d %05d ", xref[1], xref[2])
switch xref[0] {
case 0:
// f entries
//.........这里部分代码省略.........