blob: 5261c4858a8453d3bf975cd4f3aa9f779263497e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
//---------------------------------------------------------------------
// <copyright file="NodeCounter.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//
// @owner Microsoft
// @backupOwner Microsoft
//---------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Diagnostics;
using System.Data.Common;
using md=System.Data.Metadata.Edm;
namespace System.Data.Query.InternalTrees
{
/// <summary>
/// Counts the number of nodes in a tree
/// </summary>
internal class NodeCounter : BasicOpVisitorOfT<int>
{
/// <summary>
/// Public entry point - Calculates the nubmer of nodes in the given subTree
/// </summary>
/// <param name="subTree"></param>
/// <returns></returns>
internal static int Count(Node subTree)
{
NodeCounter counter = new NodeCounter();
return counter.VisitNode(subTree);
}
/// <summary>
/// Common processing for all node types
/// Count = 1 (self) + count of children
/// </summary>
/// <param name="n"></param>
/// <returns></returns>
protected override int VisitDefault(Node n)
{
int count = 1;
foreach (Node child in n.Children)
{
count += VisitNode(child);
}
return count;
}
}
}
|