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


Golang mathutil.Min函数代码示例

本文整理汇总了Golang中github.com/cznic/mathutil.Min函数的典型用法代码示例。如果您正苦于以下问题:Golang Min函数的具体用法?Golang Min怎么用?Golang Min使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了Min函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: Seek

// Seek implements http.File.
func (f *HTTPFile) Seek(offset int64, whence int) (int64, error) {
	if f.closed {
		return 0, os.ErrInvalid
	}

	if offset < 0 {
		return int64(f.off), fmt.Errorf("cannot seek before start of file")
	}

	switch whence {
	case 0:
		noff := int64(f.off) + offset
		if noff > mathutil.MaxInt {
			return int64(f.off), fmt.Errorf("seek target overflows int: %d", noff)
		}

		f.off = mathutil.Min(int(offset), len(f.content))
		if f.off == int(offset) {
			return offset, nil
		}

		return int64(f.off), io.EOF
	case 1:
		noff := int64(f.off) + offset
		if noff > mathutil.MaxInt {
			return int64(f.off), fmt.Errorf("seek target overflows int: %d", noff)
		}

		off := mathutil.Min(f.off+int(offset), len(f.content))
		if off == f.off+int(offset) {
			f.off = off
			return int64(off), nil
		}

		f.off = off
		return int64(off), io.EOF
	case 2:
		noff := int64(f.off) - offset
		if noff < 0 {
			return int64(f.off), fmt.Errorf("cannot seek before start of file")
		}

		f.off = len(f.content) - int(offset)
		return int64(f.off), nil
	default:
		return int64(f.off), fmt.Errorf("seek: invalid whence %d", whence)
	}
}
开发者ID:sinfomicien,项目名称:rkt,代码行数:49,代码来源:httpfs.go

示例2: pickCellsToLearnOn

//Pick cells to form distal connections to.
func (tm *TemporalMemory) pickCellsToLearnOn(n int, segment int,
	winnerCells []int, connections *TemporalMemoryConnections) []int {

	candidates := make([]int, len(winnerCells))
	copy(candidates, winnerCells)

	for _, val := range connections.SynapsesForSegment(segment) {
		syn := connections.DataForSynapse(val)
		for idx, val := range candidates {
			if val == syn.SourceCell {
				candidates = append(candidates[:idx], candidates[idx+1:]...)
				break
			}
		}
	}

	//Shuffle candidates
	for i := range candidates {
		j := rand.Intn(i + 1)
		candidates[i], candidates[j] = candidates[j], candidates[i]
	}

	n = mathutil.Min(n, len(candidates))
	return candidates[:n]
}
开发者ID:Fardinak,项目名称:htm,代码行数:26,代码来源:temporalMemory.go

示例3: ReadAt

func (f *mem) ReadAt(b []byte, off int64) (n int, err error) {
	avail := f.size - off
	pi := off >> f.pgBits
	po := int(off) & f.pgMask
	rem := len(b)
	if int64(rem) >= avail {
		rem = int(avail)
		err = io.EOF
	}
	var zeroPage *[]byte
	for rem != 0 && avail > 0 {
		pg := f.m[pi]
		if pg == nil {
			if zeroPage == nil {
				zeroPage = buffer.CGet(f.pgSize)
				defer buffer.Put(zeroPage)
			}
			pg = zeroPage
		}
		nc := copy(b[:mathutil.Min(rem, f.pgSize)], (*pg)[po:])
		pi++
		po = 0
		rem -= nc
		n += nc
		b = b[nc:]
	}
	return n, err
}
开发者ID:brgmnn,项目名称:syncthing,代码行数:28,代码来源:file.go

示例4: ReadAt

func (f *bitFiler) ReadAt(b []byte, off int64) (n int, err error) {
	avail := f.size - off
	pgI := off >> bfBits
	pgO := int(off & bfMask)
	rem := len(b)
	if int64(rem) >= avail {
		rem = int(avail)
		err = io.EOF
	}
	for rem != 0 && avail > 0 {
		pg := f.m[pgI]
		if pg == nil {
			pg = &bitPage{}
			pg.pdata = buffer.CGet(bfSize)
			pg.data = *pg.pdata
			if f.parent != nil {
				_, err = f.parent.ReadAt(pg.data, off&^bfMask)
				if err != nil && !fileutil.IsEOF(err) {
					return
				}

				err = nil
			}
			f.m[pgI] = pg
		}
		nc := copy(b[:mathutil.Min(rem, bfSize)], pg.data[pgO:])
		pgI++
		pgO = 0
		rem -= nc
		n += nc
		b = b[nc:]
		off += int64(nc)
	}
	return
}
开发者ID:brgmnn,项目名称:syncthing,代码行数:35,代码来源:xact.go

示例5: getTree

func (t *treeCache) getTree(db *DB, prefix int, name string, canCreate bool, cacheSize int) (r *lldb.BTree, err error) {
	m := t.get()
	r, ok := m[name]
	if ok {
		return
	}

	root, err := db.root()
	if err != nil {
		return
	}

	val, err := root.get(prefix, name)
	if err != nil {
		return
	}

	switch x := val.(type) {
	case nil:
		if !canCreate {
			return
		}

		var h int64
		r, h, err = lldb.CreateBTree(db.alloc, collate)
		if err != nil {
			return nil, err
		}

		if err = root.set(h, prefix, name); err != nil {
			return nil, err
		}
	case int64:
		if r, err = lldb.OpenBTree(db.alloc, collate, x); err != nil {
			return nil, err
		}
	default:
		return nil, &lldb.ErrINVAL{Src: "corrupted root directory value for", Val: fmt.Sprintf("%q, %q", prefix, name)}
	}

	if len(m) > cacheSize {
		i, j, n := 0, cacheSize/2, mathutil.Min(cacheSize/20, 10)
	loop:
		for k := range m {
			if i++; i >= j {
				delete(m, k)
				if n == 0 {
					break loop
				}

				n--
			}
		}
	}

	m[name] = r
	return
}
开发者ID:pmezard,项目名称:exp,代码行数:58,代码来源:etc.go

示例6: get2

func (c *cache) get2(n int) (r *node, isZeroed bool) {
	s := *c
	lens := len(s)
	if lens == 0 {
		return &node{b: make([]byte, n, mathutil.Min(2*n, maxBuf))}, true
	}

	i := sort.Search(lens, func(x int) bool { return len(s[x].b) >= n })
	if i == lens {
		i--
		s[i].b, isZeroed = make([]byte, n, mathutil.Min(2*n, maxBuf)), true
	}

	r = s[i]
	r.b = r.b[:n]
	copy(s[i:], s[i+1:])
	s = s[:lens-1]
	*c = s
	return
}
开发者ID:klizhentas,项目名称:acbuild,代码行数:20,代码来源:falloc.go

示例7: Execute

// Execute implements Instruction.
//
// Stack frame before Execute:
//
//	        +----------------+
//	        | Return address |
//	        +----------------+
//	  SP -> |                |
//
//
// Stack frame after Execute:
//
//	        +----------------+
//	        | Return address |
//	        +----------------+
//	        | Previous FP    |
//	        +----------------+
//	        | [LNL]int       | Parent Frame Pointers
//	        +----------------+
//	        | LNL            | Lexical Nesting Level
//	        +----------------+
//	  FP -> | [NVars]int     | Local variables
//	        +----------------+
//	  SP -> |                ! Evaluation stack
func (n *Enter) Execute(m *VM) {
	m.push(m.FP)
	cur := 0
	if n.LNL != 0 {
		cur = m.read(m.FP - 1)
		fp0x := m.FP - 1 - cur
		for i := 0; i < mathutil.Min(cur, n.LNL); i++ {
			m.push(m.read(fp0x))
			fp0x++
		}
	}
	if n.LNL > cur {
		m.push(m.FP)
	}
	m.push(n.LNL)
	m.FP = m.SP()
	m.Stack = append(m.Stack, make([]int, n.NVars)...)
}
开发者ID:JamesLinus,项目名称:pl0,代码行数:42,代码来源:vm.go

示例8: PutBytes

// PutBytes stores b in the DB and returns its id.  Zero length byte slices are
// guaranteed to return zero ID. PutBytes Locks the DB before updating it.
func (d *MemDB) PutBytes(b []byte) int {
	if len(b) == 0 {
		return 0
	}

	if len(b) == 1 {
		return int(b[0]) + 1
	}

	d.mu.Lock() // W+

	id := d.nextID
	pi := id >> dbPageShift
	if pi < len(d.pages) {
		p := d.pages[pi]
		off := id & dbPageMask
		if n := cap(p) - off - maxUvarint; n >= len(b) {
			p = p[:cap(p)]
			l := binary.PutUvarint(p[off:], uint64(len(b)))
			copy(p[off+l:], b)
			n = l + len(b)
			d.pages[pi] = p[:off+n]
			d.nextID += n

			d.mu.Unlock() // W-

			return id + 257
		}

		pi++
	}

	p := make([]byte, mathutil.Max(dbPageSize, maxUvarint+len(b)))
	p = p[:binary.PutUvarint(p, uint64(len(b)))]
	p = append(p, b...)
	d.pages = append(d.pages, p)
	id = pi << dbPageShift
	d.nextID = id + mathutil.Min(dbPageSize, len(p))

	d.mu.Unlock() // W-

	return id + 257
}
开发者ID:cznic,项目名称:xc,代码行数:45,代码来源:db.go

示例9: Readdir

// Readdir implements http.File.
func (f *HTTPFile) Readdir(count int) ([]os.FileInfo, error) {
	if f.isFile {
		return nil, fmt.Errorf("not a directory: %s", f.name)
	}

	if count <= 0 {
		r := f.dirEntries
		f.dirEntries = f.dirEntries[:0]
		return r, nil
	}

	rq := mathutil.Min(count, len(f.dirEntries))
	r := f.dirEntries[:rq]
	f.dirEntries = f.dirEntries[rq:]
	if len(r) != 0 {
		return r, nil
	}

	return nil, io.EOF
}
开发者ID:sinfomicien,项目名称:rkt,代码行数:21,代码来源:httpfs.go

示例10: ReadAt

// ReadAt implements Filer.
func (f *MemFiler) ReadAt(b []byte, off int64) (n int, err error) {
	avail := f.size - off
	pgI := off >> pgBits
	pgO := int(off & pgMask)
	rem := len(b)
	if int64(rem) >= avail {
		rem = int(avail)
		err = io.EOF
	}
	for rem != 0 && avail > 0 {
		pg := f.m[pgI]
		if pg == nil {
			pg = &zeroPage
		}
		nc := copy(b[:mathutil.Min(rem, pgSize)], pg[pgO:])
		pgI++
		pgO = 0
		rem -= nc
		n += nc
		b = b[nc:]
	}
	return
}
开发者ID:sinfomicien,项目名称:rkt,代码行数:24,代码来源:memfiler.go

示例11: readAt

func (f *File) readAt(b []byte, off int64, bits bool) (n int, err error) {
	var fsize int64
	if !bits {
		fsize, err = f.Size()
		if err != nil {
			return
		}
	}

	avail := fsize - off
	pgI := off >> pgBits
	pgO := int(off & pgMask)
	rem := len(b)
	if !bits && int64(rem) >= avail {
		rem = int(avail)
		err = io.EOF
	}
	for rem != 0 && (bits || avail > 0) {
		v, err := (*Array)(f).Get(pgI)
		if err != nil {
			return n, err
		}

		pg, _ := v.([]byte)
		if len(pg) == 0 {
			pg = zeroPage[:]
		}

		nc := copy(b[:mathutil.Min(rem, pgSize)], pg[pgO:])
		pgI++
		pgO = 0
		rem -= nc
		n += nc
		b = b[nc:]
	}
	return
}
开发者ID:pmezard,项目名称:exp,代码行数:37,代码来源:file.go

示例12: BenchmarkRollbackFiler

func BenchmarkRollbackFiler(b *testing.B) {
	rng := rand.New(rand.NewSource(42))
	type t struct {
		off int64
		b   []byte
	}
	a := []t{}
	for rem := b.N; rem > 0; {
		off := rng.Int63()
		n := mathutil.Min(rng.Intn(1e3)+1, rem)
		a = append(a, t{off, rndBytes(rng, n)})
		rem -= n
	}

	var r *RollbackFiler
	f := NewMemFiler()

	checkpoint := func(sz int64) (err error) {
		return f.Truncate(sz)
	}

	r, err := NewRollbackFiler(f, checkpoint, f)
	if err != nil {
		b.Fatal(err)
	}

	if err := r.BeginUpdate(); err != nil {
		b.Fatal(err)
	}

	b.ResetTimer()
	for _, v := range a {
		if _, err := r.WriteAt(v.b, v.off); err != nil {
			b.Fatal(err)
		}
	}
}
开发者ID:klizhentas,项目名称:acbuild,代码行数:37,代码来源:xact_test.go

示例13: main1


//.........这里部分代码省略.........
		if err := p.SkeletonXErrors(b); err != nil {
			return err
		}

		if err := b.Flush(); err != nil {
			return err
		}

		if err := f.Close(); err != nil {
			return err
		}
	}

	msu := make(map[*y.Symbol]int, len(p.Syms)) // sym -> usage
	for nm, sym := range p.Syms {
		if nm == "" || nm == "ε" || nm == "$accept" || nm == "#" {
			continue
		}

		msu[sym] = 0
	}
	var minArg, maxArg int
	for _, state := range p.Table {
		for _, act := range state {
			msu[act.Sym]++
			k, arg := act.Kind()
			if k == 'a' {
				continue
			}

			if k == 'r' {
				arg = -arg
			}
			minArg, maxArg = mathutil.Min(minArg, arg), mathutil.Max(maxArg, arg)
		}
	}
	su := make(symsUsed, 0, len(msu))
	for sym, used := range msu {
		su = append(su, symUsed{sym, used})
	}
	sort.Sort(su)

	// ----------------------------------------------------------- Prologue
	f := strutil.IndentFormatter(out, "\t")
	f.Format("// CAUTION: Generated file - DO NOT EDIT.\n\n")
	f.Format("%s", injectImport(p.Prologue))
	f.Format(`
type %[1]sSymType %i%s%u

type %[1]sXError struct {
	state, xsym int
}
`, *oPref, p.UnionSrc)

	// ---------------------------------------------------------- Constants
	nsyms := map[string]*y.Symbol{}
	a := make([]string, 0, len(msu))
	maxTokName := 0
	for sym := range msu {
		nm := sym.Name
		if nm == "$default" || nm == "$end" || sym.IsTerminal && nm[0] != '\'' && sym.Value > 0 {
			maxTokName = mathutil.Max(maxTokName, len(nm))
			a = append(a, nm)
		}
		nsyms[nm] = sym
	}
开发者ID:XuHuaiyu,项目名称:tidb,代码行数:67,代码来源:main.go

示例14: flatten

// []interface{}{qltype, ...}->[]interface{}{lldb scalar type, ...}
// + long blobs are (pre)written to a chain of chunks.
func (s *file) flatten(data []interface{}) (err error) {
	for i, v := range data {
		tag := 0
		var b []byte
		switch x := v.(type) {
		case []byte:
			tag = qBlob
			b = x
		case *big.Int:
			tag = qBigInt
			b, err = s.codec.encode(x)
		case *big.Rat:
			tag = qBigRat
			b, err = s.codec.encode(x)
		case time.Time:
			tag = qTime
			b, err = s.codec.encode(x)
		case time.Duration:
			tag = qDuration
			b, err = s.codec.encode(x)
		default:
			continue
		}
		if err != nil {
			return
		}

		const chunk = 1 << 16
		chunks := 0
		var next int64
		var buf []byte
		for rem := len(b); rem > shortBlob; {
			n := mathutil.Min(rem, chunk)
			part := b[rem-n:]
			b = b[:rem-n]
			rem -= n
			switch next {
			case 0: // last chunk
				buf, err = lldb.EncodeScalars([]interface{}{part}...)
			default: // middle chunk
				buf, err = lldb.EncodeScalars([]interface{}{next, part}...)
			}
			if err != nil {
				return
			}

			h, err := s.a.Alloc(buf)
			if err != nil {
				return err
			}

			next = h
			chunks++
		}

		switch next {
		case 0: // single chunk
			buf, err = lldb.EncodeScalars([]interface{}{tag, b}...)
		default: // multi chunks
			buf, err = lldb.EncodeScalars([]interface{}{tag, next, b}...)
		}
		if err != nil {
			return
		}

		data[i] = buf
	}
	return
}
开发者ID:uabassguy,项目名称:ql,代码行数:71,代码来源:file.go

示例15: freeNSynapses

/*
Free up some synapses in this segment. We always free up inactive
synapses (lowest permanence freed up first) before we start to free up
active ones.

param numToFree number of synapses to free up
param inactiveSynapseIndices list of the inactive synapse indices.
*/
func (s *Segment) freeNSynapses(numToFree int, inactiveSynapseIndices []int) {
	//Make sure numToFree isn't larger than the total number of syns we have
	if numToFree > len(s.syns) {
		panic("Number to free cannot be larger than existing synapses.")
	}

	if s.tp.params.Verbosity >= 5 {
		fmt.Println("freeNSynapses with numToFree=", numToFree)
		fmt.Println("inactiveSynapseIndices= ", inactiveSynapseIndices)
	}

	var candidates []int
	// Remove the lowest perm inactive synapses first
	if len(inactiveSynapseIndices) > 0 {
		perms := make([]float64, len(inactiveSynapseIndices))
		for idx, _ := range perms {
			perms[idx] = s.syns[idx].Permanence
		}
		var indexes []int
		floats.Argsort(perms, indexes)
		//sort perms
		cSize := mathutil.Min(numToFree, len(perms))
		candidates = make([]int, cSize)
		//indexes[0:cSize]
		for i := 0; i < cSize; i++ {
			candidates[i] = inactiveSynapseIndices[indexes[i]]
		}
	}

	// Do we need more? if so, remove the lowest perm active synapses too
	var activeSynIndices []int
	if len(candidates) < numToFree {
		for i := 0; i < len(s.syns); i++ {
			if !utils.ContainsInt(i, inactiveSynapseIndices) {
				activeSynIndices = append(activeSynIndices, i)
			}
		}

		perms := make([]float64, len(activeSynIndices))
		for i := range perms {
			perms[i] = s.syns[i].Permanence
		}
		var indexes []int
		floats.Argsort(perms, indexes)

		moreToFree := numToFree - len(candidates)
		//moreCandidates := make([]int, moreToFree)
		for i := 0; i < moreToFree; i++ {
			candidates = append(candidates, activeSynIndices[indexes[i]])
		}
	}

	if s.tp.params.Verbosity >= 4 {
		fmt.Printf("Deleting %v synapses from segment to make room for new ones: %v \n",
			len(candidates), candidates)
		fmt.Println("Before:", s.ToString())
	}

	// Delete candidate syns by copying undeleted to new slice
	var newSyns []Synapse
	for idx, val := range s.syns {
		if !utils.ContainsInt(idx, candidates) {
			newSyns = append(newSyns, val)
		}
	}
	s.syns = newSyns

	if s.tp.params.Verbosity >= 4 {
		fmt.Println("After:", s.ToString())
	}

}
开发者ID:Fardinak,项目名称:htm,代码行数:80,代码来源:segment.go


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