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 InsertAfterList(rl *list.List, ol *list.List, mark *list.Element) *list.Element { rcur := mark for cur := ol.Front(); cur != nil; cur = cur.Next() { rcur = rl.InsertAfter(cur.Value, rcur) } return rcur }
// The limit is depth, not total number of returned commits. func (repo *Repository) commitsBefore(l *list.List, parent *list.Element, id sha1, current, limit int) error { // Reach the limit if limit > 0 && current > limit { return nil } commit, err := repo.getCommit(id) if err != nil { return fmt.Errorf("getCommit: %v", err) } var e *list.Element if parent == nil { e = l.PushBack(commit) } else { var in = parent for { if in == nil { break } else if in.Value.(*Commit).ID.Equal(commit.ID) { return nil } else if in.Next() == nil { break } if in.Value.(*Commit).Committer.When.Equal(commit.Committer.When) { break } if in.Value.(*Commit).Committer.When.After(commit.Committer.When) && in.Next().Value.(*Commit).Committer.When.Before(commit.Committer.When) { break } in = in.Next() } e = l.InsertAfter(commit, in) } pr := parent if commit.ParentCount() > 1 { pr = e } for i := 0; i < commit.ParentCount(); i++ { id, err := commit.ParentID(i) if err != nil { return err } err = repo.commitsBefore(l, pr, id, current+1, limit) if err != nil { return err } } return nil }
func (repo *Repository) commitsBefore(lock *sync.Mutex, l *list.List, parent *list.Element, id sha1, limit int) error { commit, err := repo.getCommit(id) if err != nil { return err } var e *list.Element if parent == nil { e = l.PushBack(commit) } else { var in = parent //lock.Lock() for { if in == nil { break } else if in.Value.(*Commit).Id.Equal(commit.Id) { //lock.Unlock() return nil } else { if in.Next() == nil { break } if in.Value.(*Commit).Committer.When.Equal(commit.Committer.When) { break } if in.Value.(*Commit).Committer.When.After(commit.Committer.When) && in.Next().Value.(*Commit).Committer.When.Before(commit.Committer.When) { break } } in = in.Next() } e = l.InsertAfter(commit, in) //lock.Unlock() } var pr = parent if commit.ParentCount() > 1 { pr = e } for i := 0; i < commit.ParentCount(); i++ { id, err := commit.ParentId(i) if err != nil { return err } err = repo.commitsBefore(lock, l, pr, id, 0) if err != nil { return err } } return nil }
func (s *Segment) processAfterSegment(text string, result *list.List) { // 匹配同义词 if s.options.SynonymOutput { node := result.Front() for node != nil { pW := node.Value.(*dict.WordInfo) synonyms := s.synonym.GetSynonyms(pW.Word) if synonyms != nil { for _, word := range synonyms { node = result.InsertAfter(dict.NewWordInfo(word, pW.Position, pW.Pos, pW.Frequency, s.params.SymbolRank, dict.TSynonym, pW.WordType), node) } } node = node.Next() } } // 通配符匹配 if s.options.WildcardOutput { // todo: >>>>>>> } }
// 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()) } }
func add(l *list.List, args []string) { switch len(args) { case 0: fmt.Println("What do you want to add?") os.Exit(0) case 1: item := ListItem{ DESC: args[0], DATE: time.Now().Format(DATE_FORMAT), } l.PushFront(item) fmt.Println("adding new item to list: " + args[0]) case 2: // Check if first arg is int n, err := strconv.Atoi(args[0]) if err == nil { // Make item (args[1]) item := ListItem{ DESC: args[1], DATE: time.Now().Format(DATE_FORMAT), } // Check bounds if n > l.Len()+1 { fmt.Printf("item [%d] is out of bounds.\n", n) os.Exit(1) } else if n < 1 { fmt.Printf("item [%d] is out of bounds.\n", n) os.Exit(1) } else if n == 1 { // regular add l.PushFront(item) fmt.Println("adding new item to list: " + args[1]) break } else { // add in place // Insert item at location (n) // get nth element itemNumber, element := 1, l.Front() for itemNumber < n-1 { element = element.Next() itemNumber++ } // add args[1] after element n-1 l.InsertAfter(item, element) fmt.Printf("adding new item[%d]: %s\n", itemNumber+1, args[1]) break } } fallthrough default: // check for first arg numeric in bounds n, err := strconv.Atoi(args[0]) if err == nil { // numeric args = args[1:] // Check bounds if n > l.Len()+1 { fmt.Printf("item [%d] is out of bounds.\n", n) os.Exit(1) } else if n < 1 { fmt.Printf("item [%d] is out of bounds.\n", n) os.Exit(1) } } // Make item item := ListItem{ DATE: time.Now().Format(DATE_FORMAT), DESC: args[0], } // check if args[1:] have other fields for _, a := range args[1:] { fields := strings.Split(a, ":") if len(fields) < 2 { // not extra field fmt.Printf("Missing field name: %s\n", a) os.Exit(1) } else if len(fields) > 2 { // too many extra field fmt.Printf("Too many field names: %s\n", a) os.Exit(1) } else { // add new field to item item[strings.Title(fields[0])] = fields[1] } } str, _ := getJSONString(item) switch n { case 0, 1: // not numeric, regular add l.PushFront(item) fmt.Println("adding new item to list: " + str) default: // add in place // Insert item at location (n) // get n-1th element itemNumber, element := 1, l.Front() for itemNumber < n-1 { element = element.Next() itemNumber++ } // add args[1] after element n-1 l.InsertAfter(item, element) fmt.Printf("adding new item[%d]: %s\n", itemNumber+1, str) } } }