// dotNode generates a graph node in DOT format.
510808
func dotNode(rpt *Report, maxFlat float64, rIndex int, n *node) string {
	flat, cum := n.flat, n.cum

	labels := strings.Split(n.info.prettyName(), "::")
	label := strings.Join(labels, `\n`) + `\n`

	flatValue := rpt.formatValue(flat)
	if flat > 0 {
		label = label + fmt.Sprintf(`%s(%s)`,
			flatValue,
			strings.TrimSpace(percentage(flat, rpt.total)))
	} else {
		label = label + "0"
	}
	cumValue := flatValue
	if cum != flat {
		if flat > 0 {
			label = label + `\n`
		} else {
			label = label + " "
		}
		cumValue = rpt.formatValue(cum)
		label = label + fmt.Sprintf(`of %s(%s)`,
			cumValue,
			strings.TrimSpace(percentage(cum, rpt.total)))
	}

	// Scale font sizes from 8 to 24 based on percentage of flat frequency.
	// Use non linear growth to emphasize the size difference.
	baseFontSize, maxFontGrowth := 8, 16.0
	fontSize := baseFontSize
	if maxFlat > 0 && flat > 0 && float64(flat) <= maxFlat {
		fontSize += int(math.Ceil(maxFontGrowth * math.Sqrt(float64(flat)/maxFlat)))
	}
	return fmt.Sprintf(`N%d [label="%s" fontsize=%d shape=box tooltip="%s (%s)"]`,
		rIndex,
		label,
		fontSize, n.info.prettyName(), cumValue)
}
933755
func (t *FST) addNode(nodeIn *UnCompiledNode) (int64, error) {
	// fmt.Printf("FST.addNode pos=%v numArcs=%v\n", t.bytes.position(), nodeIn.NumArcs)
	if nodeIn.NumArcs == 0 {
		if nodeIn.IsFinal {
			return FST_FINAL_END_NODE, nil
		}
		return FST_NON_FINAL_END_NODE, nil
	}

	startAddress := t.bytes.position()
	// fmt.Printf("  startAddr=%v\n", startAddress)

	doFixedArray := t.shouldExpand(nodeIn)
	if doFixedArray {
		// fmt.Println("  fixedArray")
		if len(t.bytesPerArc) < nodeIn.NumArcs {
			t.bytesPerArc = make([]int, util.Oversize(nodeIn.NumArcs, 1))
		}
	}

	t.arcCount += int64(nodeIn.NumArcs)

	lastArc := nodeIn.NumArcs - 1

	lastArcStart := t.bytes.position()
	maxBytesPerArc := 0
	for arcIdx := 0; arcIdx < nodeIn.NumArcs; arcIdx++ {
		arc := nodeIn.Arcs[arcIdx]
		target := arc.Target.(*CompiledNode)
		flags := byte(0)
		// fmt.Printf("  arc %v label=%v -> target=%v\n", arcIdx, arc.label, target.node)

		if arcIdx == lastArc {
			flags += FST_BIT_LAST_ARC
		}

		if t.lastFrozenNode == target.node && !doFixedArray {
			flags += FST_BIT_TARGET_NEXT
		}

		if arc.isFinal {
			flags += FST_BIT_FINAL_ARC
			if arc.nextFinalOutput != NO_OUTPUT {
				flags += FST_BIT_ARC_HAS_FINAL_OUTPUT
			}
		} else {
			assert(arc.nextFinalOutput == NO_OUTPUT)
		}

		targetHasArcs := target.node > 0

		if !targetHasArcs {
			flags += FST_BIT_STOP_NODE
		} else if t.inCounts != nil {
			panic("not implemented yet")
		}

		if arc.output != NO_OUTPUT {
			flags += FST_BIT_ARC_HAS_OUTPUT
		}

		t.bytes.WriteByte(flags)
		var err error
		if err = t.writeLabel(t.bytes, arc.label); err != nil {
			return 0, err
		}

		// fmt.Printf("  write arc: label=%c flags=%v target=%v pos=%v output=%v\n",
		// 	rune(arc.label), flags, target.node, t.bytes.position(),
		// 	t.outputs.outputToString(arc.output))

		if arc.output != NO_OUTPUT {
			if err = t.outputs.Write(arc.output, t.bytes); err != nil {
				return 0, err
			}
			// fmt.Println("    write output")
			t.arcWithOutputCount++
		}

		if arc.nextFinalOutput != NO_OUTPUT {
			// fmt.Println("    write final output")
			if err = t.outputs.writeFinalOutput(arc.nextFinalOutput, t.bytes); err != nil {
				return 0, err
			}
		}

		if targetHasArcs && (flags&FST_BIT_TARGET_NEXT) == 0 {
			assert(target.node > 0)
			// fmt.Println("    write target")
			if err = t.bytes.WriteVLong(target.node); err != nil {
				return 0, err
			}
		}

		// just write the arcs "like normal" on first pass, but record
		// how many bytes each one took, and max byte size:
		if doFixedArray {
			t.bytesPerArc[arcIdx] = int(t.bytes.position() - lastArcStart)
			lastArcStart = t.bytes.position()
			if t.bytesPerArc[arcIdx] > maxBytesPerArc {
				maxBytesPerArc = t.bytesPerArc[arcIdx]
			}
		}
	}

	if doFixedArray {
		MAX_HEADER_SIZE := 11 // header(byte) + numArcs(vint) + numBytes(vint)
		assert(maxBytesPerArc > 0)
		// 2nd pass just "expands" all arcs to take up a fixed byte size
		// create the header
		header := make([]byte, MAX_HEADER_SIZE)
		bad := store.NewByteArrayDataOutput(header)
		// write a "false" first arc:
		bad.WriteByte(FST_ARCS_AS_FIXED_ARRAY)
		bad.WriteVInt(int32(nodeIn.NumArcs))
		bad.WriteVInt(int32(maxBytesPerArc))
		headerLen := bad.Position()

		fixedArrayStart := startAddress + int64(headerLen)

		// expand the arcs in place, backwards
		srcPos := t.bytes.position()
		destPos := fixedArrayStart + int64(nodeIn.NumArcs)*int64(maxBytesPerArc)
		assert(destPos >= srcPos)
		if destPos > srcPos {
			t.bytes.skipBytes(int(destPos - srcPos))
			for arcIdx := nodeIn.NumArcs - 1; arcIdx >= 0; arcIdx-- {
				destPos -= int64(maxBytesPerArc)
				srcPos -= int64(t.bytesPerArc[arcIdx])
				if srcPos != destPos {
					assert2(destPos > srcPos,
						"destPos=%v srcPos=%v arcIdx=%v maxBytesPerArc=%v bytesPerArc[arcIdx]=%v nodeIn.numArcs=%v",
						destPos, srcPos, arcIdx, maxBytesPerArc, t.bytesPerArc[arcIdx], nodeIn.NumArcs)
					t.bytes.copyBytesInside(srcPos, destPos, t.bytesPerArc[arcIdx])
				}
			}
		}

		// now write the header
		t.bytes.writeBytesAt(startAddress, header[:headerLen])
	}

	thisNodeAddress := t.bytes.position() - 1

	t.bytes.reverse(startAddress, thisNodeAddress)

	// PackedInts uses int as the index, so we cannot handle > 2.1B
	// nodes when packing:
	assert2(t.nodeAddress == nil || t.nodeCount < math.MaxInt32,
		"cannot create a packed FST with more than 2.1 billion nodes")

	t.nodeCount++
	var node int64
	if t.nodeAddress != nil {
		panic("not implemented yet")
	} else {
		node = thisNodeAddress
	}
	t.lastFrozenNode = node

	// fmt.Printf("  ret node=%v address=%v nodeAddress=%v",
	// 	node, thisNodeAddress, t.nodeAddress)
	return node, nil
}
684838
func GraphDot(g *Graph, opts *dag.DotOpts) (string, error) {
	return string(g.Dot(opts)), nil
}
823082
func (remote *RemoteDebugger) ResolveNode(nodeID int) (map[string]interface{}, error) {
	return remote.SendRequest("DOM.resolveNode", Params{
		"nodeId": nodeID,
	})
}
13630
func (g *metadataGraph) node(path string) *metadataNode {
	cur := g.root
	if path != "" {
		for _, elem := range strings.Split(path, "/") {
			cur = cur.child(elem)
		}
	}
	return cur
}
809169
func (db *nodeDB) updateNode(node *Node) error {
	return db.storeRLP(makeKey(node.ID, nodeDBDiscoverRoot), node)
}
444794
func (g *MutableDirectedGraph) RemoveNode(n *Node) {
	delete(g.nodesByName, n.UniqueName)
	g.DirectedGraph.RemoveNode(n)
}
1298820
func removeNode(n *node) {
	var i int
	for {
		i = -1
		if n.Children[0] != nil {
			i = 0
		} else if n.Children[1] != nil {
			i = 1
		} else if n.Children[2] != nil {
			i = 2
		} else if n.Children[3] != nil {
			i = 3
		}

		if i == -1 {
			n.Value = nil
			return
		}

		if n.Children[i].Value == nil {
			n.Children[i] = nil
			continue
		}

		break
	}

	n.Value = n.Children[i].Value
	removeNode(n.Children[i])
}
625633
func (c *Context) GetNode() (*core.IpfsNode, error) {
	var err error
	if c.node == nil {
		if c.ConstructNode == nil {
			return nil, errors.New("nil ConstructNode function")
		}
		c.node, err = c.ConstructNode()
	}
	return c.node, err
}
613333
func (ns *nodeSet) updateNode(n NodeInfo) {
	_, ok := ns.nodes[n.ID]
	if ok {
		ns.nodes[n.ID] = n
	}
}
1207904
func (g *UndirectedGraph) AddNode(n graph.Node) {
	if _, exists := g.nodes[n.ID()]; exists {
		panic(fmt.Sprintf("simple: node ID collision: %d", n.ID()))
	}
	g.nodes[n.ID()] = n
	g.edges[n.ID()] = &sliceEdgeHolder{self: n.ID()}

	g.freeIDs.Remove(n.ID())
	g.usedIDs.Insert(n.ID())
}
796846
func (m *rewrite) walkNode(cur expr.Node) (expr.Node, error) {
	//u.Debugf("WalkNode: %#v", cur)
	switch curNode := cur.(type) {
	case *expr.NumberNode, *expr.StringNode:
		return curNode, nil
	case *expr.BinaryNode:
		return m.walkFilterBinary(curNode)
	case *expr.TriNode: // Between
		return m.walkFilterTri(curNode)
	case *expr.UnaryNode:
		//return m.walkUnary(curNode)
		u.Warnf("not implemented: %#v", curNode)
		return nil, fmt.Errorf("Not implemented urnary function: %v", curNode.String())
	case *expr.FuncNode:
		return m.walkFilterFunc(curNode)
	case *expr.IdentityNode:
		u.Warnf("we are trying to project?   %v", curNode.String())
		return curNode, nil
	case *expr.ArrayNode:
		return m.walkArrayNode(curNode)
	default:
		u.Debugf("unrecognized T:%T  %v", cur, cur)
	}

	return cur, nil
}
342793
func simplifyNode(n node) node {
	switch n := n.(type) {
	case *shortNode:
		// Short nodes discard the flags and cascade
		return &rawShortNode{Key: n.Key, Val: simplifyNode(n.Val)}

	case *fullNode:
		// Full nodes discard the flags and cascade
		node := rawFullNode(n.Children)
		for i := 0; i < len(node); i++ {
			if node[i] != nil {
				node[i] = simplifyNode(node[i])
			}
		}
		return node

	case valueNode, hashNode, rawNode:
		return n

	default:
		panic(fmt.Sprintf("unknown node type: %T", n))
	}
}
537604
func NewNode(t *Type, id *ID) *Node {
	return &Node{
		t:  t,
		id: id,
	}
}
903698
func (g *marshalGraph) Dot(opts *DotOpts) []byte {
	if opts == nil {
		opts = &DotOpts{
			DrawCycles: true,
			MaxDepth:   -1,
			Verbose:    true,
		}
	}

	var w indentWriter
	w.WriteString("digraph {\n")
	w.Indent()

	// some dot defaults
	w.WriteString(`compound = "true"` + "\n")
	w.WriteString(`newrank = "true"` + "\n")

	// the top level graph is written as the first subgraph
	w.WriteString(`subgraph "root" {` + "\n")
	g.writeBody(opts, &w)

	// cluster isn't really used other than for naming purposes in some graphs
	opts.cluster = opts.MaxDepth != 0
	maxDepth := opts.MaxDepth
	if maxDepth == 0 {
		maxDepth = -1
	}

	for _, s := range g.Subgraphs {
		g.writeSubgraph(s, opts, maxDepth, &w)
	}

	w.Unindent()
	w.WriteString("}\n")
	return w.Bytes()
}
346265
func NewNode(node int64) (*Node, error) {

	// re-calc in case custom NodeBits or StepBits were set
	// DEPRECATED: the below block will be removed in a future release.
	mu.Lock()
	nodeMax = -1 ^ (-1 << NodeBits)
	nodeMask = nodeMax << StepBits
	stepMask = -1 ^ (-1 << StepBits)
	timeShift = NodeBits + StepBits
	nodeShift = StepBits
	mu.Unlock()

	n := Node{}
	n.node = node
	n.nodeMax = -1 ^ (-1 << NodeBits)
	n.nodeMask = n.nodeMax << StepBits
	n.stepMask = -1 ^ (-1 << StepBits)
	n.timeShift = NodeBits + StepBits
	n.nodeShift = StepBits

	if n.node < 0 || n.node > n.nodeMax {
		return nil, errors.New("Node number must be between 0 and " + strconv.FormatInt(n.nodeMax, 10))
	}

	var curTime = time.Now()
	// add time.Duration to curTime to make sure we use the monotonic clock if available
	n.epoch = curTime.Add(time.Unix(Epoch/1000, (Epoch%1000)*1000000).Sub(curTime))

	return &n, nil
}
89230
func (s *Server) GetNode(w http.ResponseWriter, req *http.Request) {
	node := req.Context().Value("node").(*Node)

	s.JSON(w, http.StatusOK, node.NodeInfo())
}