Exemple #1
0
/* synchronize subtitles by knowing the time of the first and the last subtitle.
 * to archive this we must use the linear equation: y = mx + b */
func sync_subs(subs *list.List, synced_first_ms uint, synced_last_ms uint) {
	var slope, yint float64

	desynced_first_ms := subs.Front().Value.(*subtitle).start
	desynced_last_ms := subs.Back().Value.(*subtitle).start

	/* m = (y2 - y1) / (x2 - x1)
	 * m: slope
	 * y2: synced_last_ms
	 * y1: synced_first_ms
	 * x2: desynced_last_ms
	 * x1: desynced_first_ms */
	slope = float64(synced_last_ms-synced_first_ms) / float64(desynced_last_ms-desynced_first_ms)
	/* b = y - mx
	 * b: yint
	 * y: synced_last_ms
	 * m: slope
	 * x: desynced_last_ms */
	yint = float64(synced_last_ms) - slope*float64(desynced_last_ms)

	for e := subs.Front(); e != nil; e = e.Next() {
		sub := e.Value.(*subtitle)
		/* y = mx + b
		 * y: sub.start and sub.end
		 * m: slope
		 * x: sub.start and sub.end
		 * b: yint */
		sub.start = uint(roundFloat64(slope*float64(sub.start) + yint))
		sub.end = uint(roundFloat64(slope*float64(sub.end) + yint))
	}
}
Exemple #2
0
// Adds the given story id and bayes factor to the given list if it
// is higher than at least one of the ones already in the list
func addIfHigh(scores *list.List, length int, storyid int64, k float64) {

	s := score{storyid: storyid, score: k}

	// Add the score if the list is empty
	last := scores.Back()
	if last == nil {
		scores.PushBack(s)
		return
	}

	if scores.Len() < length {
		insertScore(scores, length, s)
		return
	}

	// Add the score to the list if it is high enough
	lowest, ok := last.Value.(score)
	if !ok {
		log.Fatal("Could not extract score from sorted list")
	}

	if k < lowest.score {
		return
	}

	// If this point is reached, we insert the score
	insertScore(scores, length, s)
}
//add Nodes we here about in the reply to the shortList, only if that node is not in the sentList
func addResponseNodesToSL(fnodes []FoundNode, shortList *list.List, sentMap map[ID]bool, targetID ID) {
	for i := 0; i < len(fnodes); i++ {
		foundNode := &fnodes[i]
		_, inSentList := sentMap[foundNode.NodeID]
		//if the foundNode is already in sentList, dont add it to shortList
		if inSentList {
			continue
		}
		for e := shortList.Front(); e != nil; e = e.Next() {
			if e.Value.(*FoundNode).NodeID.Equals(foundNode.NodeID) {
				break
			}
			dist := e.Value.(*FoundNode).NodeID.Distance(targetID)
			foundNodeDist := foundNode.NodeID.Distance(targetID)
			//if responseNode is closer than node in ShortList, add it
			if foundNodeDist < dist {
				shortList.InsertBefore(foundNode, e)
				//keep the shortList length < Kconst
				if shortList.Len() > KConst {
					shortList.Remove(shortList.Back())
				}
				//node inserted! getout
				break
			}
		}
	}
}
Exemple #4
0
func Delete(e Elem, L *list.List) bool {
	ret := false

	if L.Len() == 0 {
		return ret
	}
	back := L.Back()
	if e.GetTime() > back.Value.(Elem).GetTime() {
		return ret
	}

	el := L.Front()
Loop:
	for i := 0; el != nil; i++ {
		elt := el.Value.(Elem).GetTime()
		if elt > e.GetTime() {
			break Loop
		} else if e.IsEqual(el.Value.(Elem)) {
			L.Remove(el)
			ret = true
			break Loop
		}
		el = el.Next()
	}
	return ret
}
Exemple #5
0
func Insert(e Elem, L *list.List) int {
	if L.Len() == 0 {
		L.PushFront(e)
		return L.Len()
	}

	front := L.Front()
	if e.GetTime() < front.Value.(Elem).GetTime() {
		L.InsertBefore(e, front)
		return L.Len()
	}

	el := L.Back()
Loop:
	for {
		if el.Value.(Elem).GetTime() > e.GetTime() {
			el = el.Prev()
		} else {
			break Loop
		}
	}
	L.InsertAfter(e, el)

	return L.Len()
}
func registerNewVideos(videos *list.List) {
	insertCount := 0
	startVideoId := ""
	endVideoId := ""

	for video := videos.Back(); video != nil; video = video.Prev() {
		videoObj := video.Value.(map[string]string)

		if isExistsVideo(videoObj["id"]) {
			continue
		}

		stmtIns, stmtInsErr := db.Prepare("INSERT INTO new_videos (id, title, post_datetime, status) VALUES( ?, ?, ?, ?)")
		if stmtInsErr != nil {
			panic(stmtInsErr.Error())
		}
		defer stmtIns.Close()

		// fmt.Println(videoObj["id"], " ", videoObj["datetime"], " ", videoObj["title"])
		insertCount++
		if startVideoId == "" {
			startVideoId = videoObj["id"]
		}
		endVideoId = videoObj["id"]

		_, insErr := stmtIns.Exec(videoObj["id"], videoObj["title"], videoObj["datetime"], 0)
		if insErr != nil {
			panic(insErr.Error())
		}
	}

	fmt.Println("datetime=[" + time.Now().String() + "] insertCount=[" + fmt.Sprint(insertCount) + "] startVideoId=[" + startVideoId + "] endVideoId=[" + endVideoId + "]")
}
Exemple #7
0
// findSource tries to find corresponding Send event to ev.
func findSource(sends *list.List, ev *trace.Event) *trace.Event {
	for e := sends.Back(); e != nil; e = e.Prev() {
		send := e.Value.(*trace.Event)
		if send.Args[1] == ev.Args[1] && send.Args[0] == ev.Args[0] {
			sends.Remove(e)
			return send
		}
	}
	return nil
}
Exemple #8
0
func (this *Splitter) endOfSentence(w *list.Element, v *list.List) bool {
	if w == v.Back() {
		return true
	} else {
		r := w
		r = r.Next()
		f := r.Value.(*Word).getForm()

		return strings.Title(f) == f || this.starters.Has(f)
	}
}
Exemple #9
0
// find is used to locate an element in a list by value. It will
// return true and a pointer to the element if the element was found
// and false and a pointer to the last element of the list (or nil)
// otherwise.
func find(lst *list.List, value Vertex) (bool, *list.Element) {
	elem := lst.Front()
	if elem != nil {
		for elem != lst.Back() && elem.Value != value {
			elem = elem.Next()
		}
		if elem.Value == value {
			return true, elem
		}
	}
	return false, elem
}
Exemple #10
0
func searchStateOnAction(queue *list.List, current bucketState, from, to int) {
	canDump := current.canDump(from, to)
	if canDump {
		next := bucketState{buckets: []int{current.buckets[0], current.buckets[1], current.buckets[2]}}
		current.dumpWater(from, to, &next)
		if !isProcessedState(queue, next) {
			queue.PushBack(next)
			searchStates(queue)
			queue.Remove(queue.Back())
		}
	}
}
Exemple #11
0
// removes up to n elements from the list starting backwards and putting their
// values in the removed slice (which should be atleast remove big). Also returns how
// many were removed
func removeFromList(l *list.List, remove int, removed []types.ObjectIndex) int {
	var e = l.Back()
	var prev *list.Element
	var i = 0
	for ; remove > i && e != nil; i++ {
		prev = e.Prev()
		removed[i] = l.Remove(e).(types.ObjectIndex)
		e = prev
	}

	return i
}
Exemple #12
0
func searchStates(queue *list.List) {
	current := queue.Back().Value.(bucketState)
	if current.isFinal() {
		// 已经是最后状态了
		printResult(queue)
	} else {
		for i := 0; i < 3; i++ {
			for j := 0; j < 3; j++ {
				searchStateOnAction(queue, current, j, i)
			}
		}
	}
}
Exemple #13
0
// maxLength should be >= length of original inputList
func insertUnseenSorted(inputList *list.List, items [](Contact), compare func(Contact, Contact) int, alreadySeen map[ID]bool, maxLength int) {
	for i, _ := range items {
		c := items[i]
		if !alreadySeen[c.NodeID] {

			insertSorted(inputList, c, compare)

			if inputList.Len() == maxLength {
				inputList.Remove(inputList.Back())
			}
		}
	}
}
Exemple #14
0
func rm(l *list.List, args []string) {
	switch len(args) {
	case 0:
		fmt.Println("What element do you want to remove?")
		os.Exit(1)
	default:
		// check if remaining args are integers
		nums := make([]int, len(args))
		for i, a := range args {
			n, err := strconv.Atoi(a)
			if err != nil {
				fmt.Printf("%q is not an item number.\n", a)
				os.Exit(1)
			}
			nums[i] = n
		}

		// Sort nums largest to smallest
		sort.Ints(nums)
		reverse(nums)

		// make sure integers is not out of bounds
		if max(nums...) > l.Len() {
			fmt.Printf("item [%d] is out of bounds.\n", max(nums...))
			os.Exit(1)
		} else if min(nums...) < 1 {
			fmt.Printf("item [%d] is out of bounds.\n", min(nums...))
			os.Exit(1)
		}

		// Collect elements
		index := 0
		elements := make([]*list.Element, len(nums))
		itemNumber, element := l.Len(), l.Back()
		for _, n := range nums {
			for n < itemNumber {
				element = element.Prev()
				itemNumber--
			}
			elements[index] = element
			index++
		}

		// Remove elements
		for i, e := range elements {
			fmt.Printf("Removing item[%d]:", nums[i])
			printJSON(l.Remove(e))
		}
		fmt.Println()
	}
}
Exemple #15
0
//notifyExpired func
func (self *TimeWheel) notifyExpired(idx int) {
	var remove *list.List
	self.lock.RLock()
	slots := self.wheel[idx]
	for e := slots.hooks.Back(); nil != e; e = e.Prev() {
		sj := e.Value.(*slotJob)
		sj.ttl--
		//ttl expired
		if sj.ttl <= 0 {
			if nil == remove {
				remove = list.New()
			}
			//记录删除
			remove.PushFront(e)

			self.slotJobWorkers <- true
			//async
			go func() {
				defer func() {
					if err := recover(); nil != err {
						//ignored
						log.Error("TimeWheel|notifyExpired|Do|ERROR|%s\n", err)
					}
					<-self.slotJobWorkers

				}()

				sj.do()

				sj.ch <- true
				close(sj.ch)
				// log.Debug("TimeWheel|notifyExpired|%d\n", sj.ttl)

			}()
		}
	}

	self.lock.RUnlock()

	if nil != remove {
		//remove
		for e := remove.Back(); nil != e; e = e.Prev() {
			re := e.Value.(*list.Element)
			self.lock.Lock()
			slots.hooks.Remove(e.Value.(*list.Element))
			delete(self.hashWheel, re.Value.(*slotJob).id)
			self.lock.Unlock()
		}
	}

}
Exemple #16
0
func setNavRels(basetm *list.List, dttmp *time.Time, sess *Session) (navonly bool, closest string) {
	start := time.Now()
	defer benchmarker("AGGREGATOR", "setnav", "Navigational relations annotated", start, sess)
	itm := basetm.Front().Value.(Link)
	itm.NavRels = append(itm.NavRels, "first")
	basetm.Front().Value = itm
	itm = basetm.Back().Value.(Link)
	itm.NavRels = append(itm.NavRels, "last")
	basetm.Back().Value = itm
	closelm := basetm.Front()
	if dttmp != nil {
		navonly = true
		dttm := *dttmp
		etm := closelm.Value.(Link).Timeobj
		dur := etm.Sub(dttm)
		if dttm.After(etm) {
			dur = dttm.Sub(etm)
		}
		mindur := dur
		for e := closelm.Next(); e != nil; e = e.Next() {
			etm = e.Value.(Link).Timeobj
			dur = etm.Sub(dttm)
			if dttm.After(etm) {
				dur = dttm.Sub(etm)
			}
			if dur > mindur {
				break
			}
			mindur = dur
			closelm = e
		}
		itm = closelm.Value.(Link)
		closest = itm.Href
		itm.NavRels = append(itm.NavRels, "closest")
		closelm.Value = itm
		if closelm.Next() != nil {
			itm = closelm.Next().Value.(Link)
			itm.NavRels = append(itm.NavRels, "next")
			closelm.Next().Value = itm
		}
		if closelm.Prev() != nil {
			itm = closelm.Prev().Value.(Link)
			itm.NavRels = append(itm.NavRels, "prev")
			closelm.Prev().Value = itm
		}
	}
	return
}
Exemple #17
0
func NewMultiword(f string, a *list.List) *Word {
	w := Word{
		form:        f,
		phForm:      "",
		lcForm:      strings.ToLower(f),
		multiword:   a,
		start:       a.Front().Value.(*Word).getSpanStart(),
		finish:      a.Back().Value.(*Word).getSpanFinish(),
		inDict:      true,
		locked:      false,
		ambiguousMw: false,
		position:    -1,
		expired:     false,
	}
	w.List = list.New()
	return &w
}
func checkHanging(list *list.List, character string, out chan<- error, path string) {
	for e := list.Back(); e != nil; e = e.Prev() {
		syntaxError := e.Value.(syntaxError)
		basicError := fmt.Errorf(`Bad Markdown URL in %s:
	extra opening %s at line %d, column %d`, path, character, syntaxError.line, syntaxError.column)
		out <- usefulError(path, syntaxError.position, basicError)
	}
}
Exemple #19
0
func (res *HTTPResponseEvent) ToResponse() *http.Response {
	raw := new(http.Response)
	raw.Proto = "HTTP"
	raw.ProtoMajor = 1
	raw.ProtoMinor = 1
	//raw.Close = true
	raw.ContentLength = int64(res.Content.Len())
	raw.Header = make(http.Header)
	raw.StatusCode = int(res.Status)
	res.SetHeader("Content-Length", strconv.Itoa(res.Content.Len()))
	for i := 0; i < len(res.Headers); i++ {
		header := res.Headers[i]
		if strings.EqualFold(header.Name, "Set-Cookie") || strings.EqualFold(header.Name, "Set-Cookie2") {
			tmp := strings.Split(header.Value, ",")
			if len(tmp) > 1 {
				var vlist list.List
				for _, v := range tmp {
					if (!strings.Contains(v, "=") || strings.Index(v, "=") > strings.Index(v, ";")) && vlist.Len() > 0 {
						v = vlist.Back().Value.(string) + "," + v
						vlist.Remove(vlist.Back())
						vlist.PushBack(v)
						//headerValues.add(headerValues.removeLast() + "," + v);
					} else {
						vlist.PushBack(v)
					}
				}
				e := vlist.Front()
				for {
					if e == nil {
						break
					}
					raw.Header.Add(header.Name, e.Value.(string))
					e = e.Next()
				}
			} else {
				raw.Header.Add(header.Name, header.Value)
			}
		} else {
			raw.Header.Add(header.Name, header.Value)
		}
	}
	if raw.ContentLength > 0 {
		raw.Body = ioutil.NopCloser(&res.Content)
	}
	return raw
}
// uses a linked list to find the max value in a list
// window shifting not yet implemented
func find_max_sliding_window(haystack []int, window_size int) int {
	if window_size > len(haystack) {
		return -1
	}

	// instantiate a linked list
	var window list.List

	// populate first window
	for i := 0; i < window_size; i++ {
		tail := window.Back()

		// remove any tail elements that are less than the next value
		if tail != nil && haystack[i] >= tail.Value.(int) {
			window.Remove(tail)
		}
		window.PushBack(haystack[i])
	}

	for i := window_size; i < len(haystack); i++ {
		// push tail element off the heap if it's less than the current haystack value
		tail := window.Back()
		if haystack[i] > tail.Value.(int) {
			window.Remove(tail)
		}

		// remove head if it is no longer in the window
		head := window.Front()

		if head != nil {
			for index, needle := range haystack {
				if needle == head.Value.(int) {
					if index < i {
						window.Remove(head)
					}
				}
			}
		}

		// add the current element to the tail
		window.PushBack(haystack[i])
	}

	return window.Front().Value.(int)
}
Exemple #21
0
func isPop(list *list.List, s string) (op []string, ok bool) {
	switch string(s) {
	case "(":
		ok = false
		return
	case ")":
		ok = true
		cur := list.Back()
		for {
			prev := cur.Prev()
			if curValue, ok2 := cur.Value.(string); ok2 {
				if string(curValue) == "(" {
					list.Remove(cur)
					return
				}
				op = append(op, curValue)
				list.Remove(cur)
				cur = prev
			}
		}
	default:
		for cur := list.Back(); cur != nil; {
			prev := cur.Prev()
			if curValue, ok2 := cur.Value.(string); ok2 {
				if level, ok3 := po[curValue]; ok3 && level >= po[s] {
					ok = true
					op = append(op, curValue)
					// fmt.Println(op)
					list.Remove(cur)
				} else if curValue == "(" {
					// fmt.Println(curValue, op)
					if len(op) != 0 {
						ok = true
					} else {
						ok = false
					}
					return
				}
			}
			cur = prev
		}
	}
	return
}
Exemple #22
0
/* delete all the elements with time <= t */
func DeleteBefore(t Time, L *list.List) {
	if L.Len() == 0 {
		return
	}
	back := L.Back()
	if back.Value.(Elem).GetTime() <= t {
		L = L.Init()
		return
	}
Loop:
	for {
		el := L.Front()
		if el.Value.(Elem).GetTime() <= t {
			L.Remove(el)
		} else {
			break Loop
		}
	}
}
Exemple #23
0
/* delete all the elements with time >= t */
func DeleteAfter(t Time, L *list.List) {
	if L.Len() == 0 {
		return
	}
	front := L.Front()
	if front.Value.(Elem).GetTime() >= t {
		L = L.Init()
		return
	}
Loop:
	for {
		el := L.Back()
		if el.Value.(Elem).GetTime() >= t {
			L.Remove(el)
		} else {
			break Loop
		}
	}
}
Exemple #24
0
func main() {
	var l *list.List
	l = list.New()

	a := &A{123}

	m := uint64(uint32(2<<16) * uint32(2<<13))
	e := uint64(2 << 29)

	fmt.Printf("m = e || %v = %v\n", m, e)

	fmt.Printf("\nBack: %v\n", l.Back())

	fmt.Println("\ntry move to front with nil...")
	l.MoveToFront((*list.Element)(a))

	fmt.Println("\ntry removing nil from list...")
	l.Remove(nil)
}
Exemple #25
0
func ScanAbnormal(queue *list.List) {
	c := time.Tick(time.Second)
	action := "info"
	if mcfg, err := utils.GetMainConfig(); err == nil {
		action = mcfg.Action
	}
	for _ = range c {
		erase_list := []string{}
		for {
			if ele := queue.Back(); ele != nil {
				value := queue.Remove(ele)
				msg := value.(router.Message)
				if fromWhiteList(msg) {
					logging.Infof("%v is on  white list,pass.", msg.FileName)
				} else if fromBigFile(msg) {
					if canEraseInstant(msg.FileName) {
						erase_list = append(erase_list, msg.FileName)
					} else {
						logging.Warningf("Big file found and I dare not del.(%v:%v)", msg.FileName, msg.Size)
					}
				} else if can_del, ok := fromBigDirectory(msg); ok {
					if yes, _ := canErase(can_del...); len(yes) > 0 {
						erase_list = append(erase_list, yes...)
					}
				} else {
					logging.Debugf("You're good %s(%s), let you go.", msg.FileName, watchman.HumanReadable(msg.Event))
				}
			} else {
				break
			}
		}
		if len(erase_list) > 0 {
			logging.Infof("[%s] Remove %v", action, erase_list)
			switch action {
			case "info":
				printState(erase_list...)
			case "remove":
				erase(erase_list...)
			}
		}
	}
}
func searchRiverStates(queue *list.List) {
	current := queue.Back().Value.(itemState)

	if current.isFinal() {
		// 已经是最后状态了
		printRiver(queue)
	} else {
		if current.currentAction.direct == 0 {
			for i := 0; i < 5; i++ {
				next := current.move(backAction[i])
				//next.printState()
				if next.validate() && !isProcessedRiverState(queue, next) {
					queue.PushBack(next)
					searchRiverStates(queue)
					queue.Remove(queue.Back())
				}
			}
		} else {
			for i := 0; i < 5; i++ {
				next := current.move(goAction[i])
				//next.printState()
				if next.validate() && !isProcessedRiverState(queue, next) {
					queue.PushBack(next)
					searchRiverStates(queue)
					queue.Remove(queue.Back())
				}
			}
		}
	}
}
Exemple #27
0
func simplifyTimeFunctionTokens(funcs *list.List) *TimeFunction {
	var lb, rb *list.Element
	for {
		lb, rb = nil,nil
		for e := funcs.Front(); e != nil; e = e.Next() {
			if e.Value.(*TimeFunction).op == opLB {lb = e}
			if e.Value.(*TimeFunction).op == opRB {
				rb = e
				simplifyBracelessTimeFunctionTokens(funcs, lb,rb)
				funcs.Remove(lb)
				funcs.Remove(rb)
				break
			}
		}
		if rb == nil && lb == nil {
			if funcs.Len() <= 0 {panic("Too few funcs")}
			simplifyBracelessTimeFunctionTokens(funcs, funcs.Front(), funcs.Back())
			return funcs.Front().Next().Value.(*TimeFunction)
		}
	}
	panic("unexpected situation")
}
Exemple #28
0
func BenchmarkRandomList(b *testing.B) {
	var q list.List
	rand.Seed(64738)
	for i := 0; i < b.N; i++ {
		if rand.Float32() < 0.8 {
			q.PushBack(i)
		}
		if rand.Float32() < 0.8 {
			q.PushFront(i)
		}
		if rand.Float32() < 0.5 {
			if e := q.Front(); e != nil {
				q.Remove(e)
			}
		}
		if rand.Float32() < 0.5 {
			if e := q.Back(); e != nil {
				q.Remove(e)
			}
		}
	}
}
Exemple #29
0
// loop churns messages from the main loop onto the internal buffer of this subscription,
// and from there out to the consumer, as requested by calls to Consume.
func (q *queue) loop(ch1 <-chan interface{}, ch2 chan<- interface{}) {
	var l list.List
__preclose:
	for {
		if w := l.Back(); w != nil {
			select {
			case v, ok := <-ch1: // distribute
				if !ok {
					q.setClosed(true)
					break __preclose
				}
				l.PushFront(v)
				q.addPend(1)
			case ch2 <- w.Value: // consume
				l.Remove(w)
				q.addPend(-1)
			}
		} else {
			v, ok := <-ch1
			if !ok {
				q.setClosed(true)
				break __preclose
			}
			l.PushFront(v)
			q.addPend(1)
		}
	}
	// After ch1 has been closed
	for {
		w := l.Back()
		if w == nil {
			close(ch2)
			return
		}
		ch2 <- w.Value
		l.Remove(w)
		q.addPend(-1)
	}
}
Exemple #30
0
// Insert the given score into the given list
func insertScore(scores *list.List, length int, s score) {
	// Loop through the scores
	for e := scores.Front(); e != nil; e = e.Next() {
		if e == scores.Back() {
			scores.InsertAfter(s, e)
			break
		}
		v, ok := e.Value.(score)
		if !ok {
			log.Fatal("Could not extract score from sorted list")
		}
		if s.score > v.score {
			scores.InsertBefore(s, e)
			break
		}
	}

	// Remove the last entry if the list is too long
	if scores.Len() > length {
		scores.Remove(scores.Back())
	}
}