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


Golang Reader.palletUtilization方法代码示例

本文整理汇总了Golang中io.Reader.palletUtilization方法的典型用法代码示例。如果您正苦于以下问题:Golang Reader.palletUtilization方法的具体用法?Golang Reader.palletUtilization怎么用?Golang Reader.palletUtilization使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在io.Reader的用法示例。


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

示例1: process

func process(doneTime time.Time, r io.Reader, a *accounting, resultChan chan result) {
	defer close(resultChan)

	tr := newTruckReader(r)
	in := make(chan *truck)
	out := make(chan *truck)

	// Construct the repacker.
	newRepacker(in, out)

	// A goroutine to read and send trucks
	go func() {
		defer close(in)

		for {
			done := time.Now().After(doneTime)

			t, err := tr.Next()
			if done || err != nil {
				if done {
					fmt.Println("timeout")
				}

				if err != nil && err != io.EOF {
					fmt.Println("truck reading error: ", err)
				}

				// Send one more empty truck as a signal that they now
				// need to send out any stored boxes.
				a.trucksMu.Lock()
				a.trucks[idLastTruck] = 0
				a.trucksMu.Unlock()
				in <- &truck{id: idLastTruck}

				return
			}

			a.boxesMu.Lock()
			for _, p := range t.pallets {
				for _, b := range p.boxes {
					a.boxes[b.canon()] = true
				}
			}
			a.boxesMu.Unlock()

			// Remember how many pallets were in the truck.
			a.trucksMu.Lock()
			a.trucks[t.id] = len(t.pallets)
			a.trucksMu.Unlock()

			in <- t
		}
	}()

	// Receive the trucks and check them.
	for t := range out {
		r := result{}
		totalUtilization := float64(0)
		palletCount := 0
		// Only correctly packed pallets count
		for pn, p := range t.pallets {
			palletCount++
			for _, b := range p.boxes {
				if !a.boxOk(b) {
					log.Printf("box %v in truck %d was not in the input", b.id, t.id)
					r.fail = true
				}
			}
			if err := p.IsValid(); err == nil {
				r.items += p.Items()
				totalUtilization += p.Utilization()
			} else {
				log.Printf("pallet %v in truck %d is not correctly packed: %v", pn, t.id, err)
				r.fail = true
			}
		}

		// Calculate the profit (or loss!) of pallets.
		a.trucksMu.Lock()
		if _, ok := a.trucks[t.id]; ok {
			r.profit = a.trucks[t.id] - len(t.pallets)
		} else {
			log.Printf("truck %v unknown", t.id)
			r.fail = true
		}
		a.trucksMu.Unlock()

		r.palletUtilization = totalUtilization / float64(palletCount)
		resultChan <- r
	}

	a.boxesMu.Lock()
	if len(a.boxes) != 0 {
		log.Printf("%v boxes not seen in the departing trucks", len(a.boxes))
		resultChan <- result{fail: true}
	}
	a.boxesMu.Unlock()
}
开发者ID:billyboar,项目名称:GCSolutions,代码行数:98,代码来源:main.go


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