Example #1
0
// InsertAt attempts to insert the value v at the given logical index i. It
// returns false if i is out of bounds. Note that a value of i equal to the
// length of this GapSlice is not considered out of bounds; it is handled as
// a special case of appending to this GapSlice.
func (s *GapSlice) InsertAt(i int, v interface{}) (success bool) {
	if i > s.size {
		return false
	}

	list := s.list

	// Special case: inserting in the very end of this gap slice.
	if i == s.size {
		list.PushBack(v)
		s.size += 1
		return true
	}

	e, offset := s.locate(i)
	if e == nil {
		return false
	}

	if slice, isSlice := e.Value.(gapSliceChunk); isSlice {
		if offset == 0 {
			list.InsertBefore(v, e)
		} else {
			a, b := slice[:offset], slice[offset:]
			e.Value = a
			e = list.InsertAfter(v, e)
			list.InsertAfter(b, e)
		}
		s.size += 1
		return true
	}

	list.InsertBefore(v, e)
	s.size += 1
	return true
}
Example #2
0
func (node *Node) CreateNodeBefor(nodename string, nodevalue string, args ...string) (*Node, error) {
	if len(args)%2 != 0 {
		return &Node{}, DyXmlErr{msg: "args not pairs"}
	}
	ele := node.NodeAdd
	list := node.NodeFather.ChildEle
	newnode := &Node{Name: nodename, Value: nodevalue, NodeFather: node.NodeFather, Attr: make(map[string]string), attr_order: make([]string, 0, len(args)/2)}
	for i := 0; i < len(args); i += 2 {
		newnode.Attr[args[i]] = args[i+1]
		newnode.attr_order = append(newnode.attr_order, args[i])
	}
	newele := list.InsertBefore(newnode, ele)
	newnode.NodeAdd = newele
	return newnode, nil
}
Example #3
0
// Add a new service definition to the list. If the definition is added or
// updated, return true.
func (l *serviceList) Add(service *ServiceDef) bool {
	list := (*list.List)(l)
	for iter := list.Front(); iter != nil; iter = iter.Next() {
		e := iter.Value.(*ServiceDef)
		res := service.compare(e)
		if res > 0 {
			continue
		} else if res < 0 {
			list.InsertBefore(service, iter)
			return true
		} else if e.connId == service.connId {
			// Replace the definition if it is from the same connection.
			iter.Value = service
			return true
		}
		// Equal entries but from a different connection.
		return false
	}
	list.PushBack(service)
	return true
}