Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/28/09 15:25:09 (15 years ago)
Author:
epitzer
Message:

Migrate HL.Random to new persistence library. (#603)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Random/3.3/MersenneTwister.cs

    r1530 r1688  
    3737using System.Xml;
    3838using HeuristicLab.Core;
     39using HeuristicLab.Persistence.Default.Decomposers.Storable;
    3940
    4041namespace HeuristicLab.Random {
     
    4647
    4748    private object locker = new object();
     49    [Storable]
    4850    private uint[] state = new uint[n];
     51    [Storable]
    4952    private int p = 0;
     53    [Storable]
    5054    private bool init = false;
    5155
     
    154158      }
    155159    }
    156 
    157     #region Persistence Methods
    158     /// <summary>
    159     /// Saves the current instance as <see cref="XmlNode"/> in the specified <paramref name="document"/>.
    160     /// </summary>
    161     /// <remarks>The state(s) are saved as child node with the tag <c>State</c>, each state separaated with
    162     /// a semicolon. Also the elements <c>p</c> and the <c>init</c> flag are saved as child nodes with
    163     /// tag names <c>P</c> and <c>Init</c> respectively.</remarks>
    164     /// <param name="name">The (tag)name of the <see cref="XmlNode"/>.</param>
    165     /// <param name="document">The <see cref="XmlDocument"/> where the data is saved.</param>
    166     /// <param name="persistedObjects">A dictionary of all already persisted objects. (Needed to avoid cycles.)</param>
    167     /// <returns>The saved <see cref="XmlNode"/>.</returns>
    168     public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
    169       XmlNode node = base.GetXmlNode(name, document, persistedObjects);
    170 
    171       StringBuilder builder = new StringBuilder();
    172       builder.Append(state[0]);
    173       for (int i = 1; i < state.Length; i++) {
    174         builder.Append(';');
    175         builder.Append(state[i]);
    176       }
    177       XmlNode stateNode = document.CreateNode(XmlNodeType.Element, "State", null);
    178       stateNode.InnerText = builder.ToString();
    179       node.AppendChild(stateNode);
    180 
    181       XmlNode pNode = document.CreateNode(XmlNodeType.Element, "P", null);
    182       pNode.InnerText = p.ToString();
    183       node.AppendChild(pNode);
    184 
    185       XmlNode initNode = document.CreateNode(XmlNodeType.Element, "Init", null);
    186       initNode.InnerText = init.ToString();
    187       node.AppendChild(initNode);
    188 
    189       return node;
    190     }
    191     /// <summary>
    192     /// Loads the persisted random number generator from the specified <paramref name="node"/>.
    193     /// </summary>
    194     /// <remarks>The elements of the current instance must be saved in a special way, see
    195     /// <see cref="GetXmlNode"/>.</remarks>
    196     /// <param name="node">The <see cref="XmlNode"/> where the instance is saved.</param>
    197     /// <param name="restoredObjects">The dictionary of all already restored objects. (Needed to avoid cycles.)</param>
    198     public override void Populate(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) {
    199       base.Populate(node, restoredObjects);
    200 
    201       string stateString = node.SelectSingleNode("State").InnerText;
    202       string[] tokens = stateString.Split(';');
    203       for (int i = 0; i < tokens.Length; i++)
    204         state[i] = uint.Parse(tokens[i]);
    205       p = int.Parse(node.SelectSingleNode("P").InnerText);
    206       init = bool.Parse(node.SelectSingleNode("Init").InnerText);
    207     }
    208     #endregion
    209160
    210161    #region Seed Methods
Note: See TracChangeset for help on using the changeset viewer.