Skip to content

Commit

Permalink
Merge pull request accord-net#1 from hanswolff/master
Browse files Browse the repository at this point in the history
retain ReadOnlyDictionary reference instead of recreating instance on each property get
  • Loading branch information
cesarsouza committed Dec 6, 2013
2 parents 42d1b80 + 3363b99 commit ccc84d8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
10 changes: 8 additions & 2 deletions Sources/Accord.MachineLearning/BagOfWords.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ public class BagOfWords : IBagOfWords<string[]>
{

private Dictionary<string, int> stringToCode;
private ReadOnlyDictionary<string, int> readOnlyStringToCode;

private Dictionary<int, string> codeToString;
private ReadOnlyDictionary<int, string> readOnlyCodeToString;


/// <summary>
Expand All @@ -83,7 +86,7 @@ public class BagOfWords : IBagOfWords<string[]>
///
public ReadOnlyDictionary<string, int> StringToCode
{
get { return new ReadOnlyDictionary<string, int>(stringToCode); }
get { return readOnlyStringToCode; }
}

/// <summary>
Expand All @@ -93,7 +96,7 @@ public ReadOnlyDictionary<string, int> StringToCode
///
public ReadOnlyDictionary<int, string> CodeToString
{
get { return new ReadOnlyDictionary<int, string>(codeToString); }
get { return readOnlyCodeToString; }
}

/// <summary>
Expand Down Expand Up @@ -144,7 +147,10 @@ public BagOfWords()
private void initialize(string[][] texts)
{
stringToCode = new Dictionary<string, int>();
readOnlyStringToCode = new ReadOnlyDictionary<string, int>(stringToCode);

codeToString = new Dictionary<int, string>();
readOnlyCodeToString = new ReadOnlyDictionary<int, string>(codeToString);

MaximumOccurance = 1;

Expand Down
16 changes: 12 additions & 4 deletions Sources/Accord.Math/Optimization/NonlinearObjectiveFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ namespace Accord.Math.Optimization
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text.RegularExpressions;
using System.Text;

/// <summary>
/// Quadratic objective function.
Expand All @@ -36,15 +34,18 @@ public class NonlinearObjectiveFunction : IObjectiveFunction
{

private Dictionary<string, int> variables;
private readonly ReadOnlyDictionary<string, int> readOnlyVariables;

private Dictionary<int, string> indices;
private ReadOnlyDictionary<int, string> readOnlyIndices;

/// <summary>
/// Gets input variable's labels for the function.
/// </summary>
///
public IDictionary<string, int> Variables
{
get { return new ReadOnlyDictionary<string, int>(variables); }
get { return readOnlyVariables; }
}

/// <summary>
Expand All @@ -53,7 +54,7 @@ public IDictionary<string, int> Variables
///
public IDictionary<int, string> Indices
{
get { return new ReadOnlyDictionary<int, string>(indices); }
get { return readOnlyIndices; }
}

/// <summary>
Expand Down Expand Up @@ -94,7 +95,11 @@ public NonlinearObjectiveFunction(int numberOfVariables,
this.Gradient = gradient;

variables = new Dictionary<string, int>();
readOnlyVariables = new ReadOnlyDictionary<string, int>(variables);

indices = new Dictionary<int, string>();
readOnlyIndices = new ReadOnlyDictionary<int, string>(indices);

for (int i = 0; i < numberOfVariables; i++)
{
string name = "x" + i;
Expand All @@ -118,7 +123,10 @@ public NonlinearObjectiveFunction(
Expression<Func<double[]>> gradient = null)
{
variables = new Dictionary<string, int>();
readOnlyVariables = new ReadOnlyDictionary<string, int>(variables);

indices = new Dictionary<int, string>();
readOnlyIndices = new ReadOnlyDictionary<int, string>(indices);

SortedSet<string> list = new SortedSet<string>();
ExpressionParser.Parse(list, function.Body);
Expand Down

0 comments on commit ccc84d8

Please sign in to comment.