Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/09/09 12:42:49 (16 years ago)
Author:
epitzer
Message:

Serialization of multi dimensional arrays. (#506)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/New Persistence Exploration/Persistence/Persistence/CompoundSerializers.cs

    r1280 r1313  
    3838  public class ArraySerializer : ICustomSerializer {
    3939
    40     public bool CanSerialize(Type type) {
     40    public bool CanSerialize(Type type) {     
    4141      return type.IsArray;
    4242    }
    4343
    4444    public IEnumerable Serialize(object array) {
     45      Array a = (Array)array;
     46      yield return a.Rank;
     47      for (int i = 0; i < a.Rank; i++) {
     48        yield return a.GetLength(i);
     49      }
    4550      foreach (object o in (Array)array) {
    4651        yield return o;
     
    4954
    5055    public object DeSerialize(IEnumerable elements, Type t) {
    51       List<object> allElements = new List<object>();
    52       foreach (object obj in elements) {
    53         allElements.Add(obj);
     56      IEnumerator e = elements.GetEnumerator();
     57      e.MoveNext();
     58      int rank = (int)e.Current;
     59      int[] lengths = new int[rank];
     60      for (int i = 0; i < rank; i++) {
     61        e.MoveNext();
     62        lengths[i] = (int)e.Current;
    5463      }
    55       Array array =
    56         Array.CreateInstance(t.GetElementType(), allElements.Count);
    57       for (int i = 0; i < array.Length; i++) {
    58         array.SetValue(allElements[i], i);
     64      Array a = Array.CreateInstance(t.GetElementType(), lengths);     
     65      int[] positions = new int[rank];
     66      while (e.MoveNext()) {
     67        a.SetValue(e.Current, positions);
     68        positions[0] += 1;
     69        for (int i = 0; i < rank-1; i++) {
     70          if (positions[i] >= lengths[i]) {
     71            positions[i] = 0;
     72            positions[i + 1] += 1;
     73          } else {
     74            break;
     75          }
     76        }
    5977      }
    60       return array;
     78      return a;
    6179    }
    6280
Note: See TracChangeset for help on using the changeset viewer.