当前位置: 首页>>代码示例>>Golang>>正文


Golang IntSlice.Len方法代码示例

本文整理汇总了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)
}
开发者ID:funny-falcon,项目名称:go-tree,代码行数:22,代码来源:tree_test.go

示例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
}
开发者ID:titorenko,项目名称:stanford-algo-course,代码行数:20,代码来源:quicksort.go

示例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
//.........这里部分代码省略.........
开发者ID:nathankerr,项目名称:pdf,代码行数:101,代码来源:file.go

示例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
//.........这里部分代码省略.........
开发者ID:nathankerr,项目名称:pdf,代码行数:101,代码来源:file.go


注:本文中的sort.IntSlice.Len方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。