1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using System.Collections.ObjectModel;
|
---|
6 | using System.Diagnostics;
|
---|
7 |
|
---|
8 | namespace AutoDiff
|
---|
9 | {
|
---|
10 | /// <summary>
|
---|
11 | /// Represents a sum of at least two terms.
|
---|
12 | /// </summary>
|
---|
13 | [Serializable]
|
---|
14 | [DebuggerDisplay("Sum: {Terms.Count}")]
|
---|
15 | public class Sum : Term
|
---|
16 | {
|
---|
17 | /// <summary>
|
---|
18 | /// Constructs an instance of the <see cref="Sum"/> class.
|
---|
19 | /// </summary>
|
---|
20 | /// <param name="first">The first term in the sum</param>
|
---|
21 | /// <param name="second">The second term in the sum</param>
|
---|
22 | /// <param name="rest">The rest of the terms in the sum.</param>
|
---|
23 | public Sum(Term first, Term second, params Term[] rest)
|
---|
24 | {
|
---|
25 | var allTerms =
|
---|
26 | (new Term[] { first, second}).Concat(rest);
|
---|
27 |
|
---|
28 | Terms = allTerms.ToList().AsReadOnly();
|
---|
29 | }
|
---|
30 |
|
---|
31 | internal Sum(IEnumerable<Term> terms)
|
---|
32 | {
|
---|
33 | Terms = Array.AsReadOnly(terms.ToArray());
|
---|
34 | }
|
---|
35 |
|
---|
36 | /// <summary>
|
---|
37 | /// Gets the terms of this sum.
|
---|
38 | /// </summary>
|
---|
39 | public ReadOnlyCollection<Term> Terms { get; private set; }
|
---|
40 |
|
---|
41 | /// <summary>
|
---|
42 | /// Accepts a term visitor
|
---|
43 | /// </summary>
|
---|
44 | /// <param name="visitor">The term visitor to accept.</param>
|
---|
45 | public override void Accept(ITermVisitor visitor)
|
---|
46 | {
|
---|
47 | visitor.Visit(this);
|
---|
48 | }
|
---|
49 |
|
---|
50 | /// <summary>
|
---|
51 | /// Accepts a term visitor with a generic result
|
---|
52 | /// </summary>
|
---|
53 | /// <typeparam name="TResult">The type of the result from the visitor's function</typeparam>
|
---|
54 | /// <param name="visitor">The visitor to accept</param>
|
---|
55 | /// <returns>
|
---|
56 | /// The result from the visitor's visit function.
|
---|
57 | /// </returns>
|
---|
58 | public override TResult Accept<TResult>(ITermVisitor<TResult> visitor)
|
---|
59 | {
|
---|
60 | return visitor.Visit(this);
|
---|
61 | }
|
---|
62 | }
|
---|
63 | }
|
---|