boaold /Backend/Runtime/Dict.cs

Language C# Lines 203
MD5 Hash 172a1acb698139547a170cedcdbd949f Estimated Cost $3,815 (why?)
Repository https://bitbucket.org/AdamMil/boaold View Raw File View Project SPDX
  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
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
Boa is the reference implementation for a language similar to Python,
also called Boa. This implementation is both interpreted and compiled,
targeting the Microsoft .NET Framework.

http://www.adammil.net/
Copyright (C) 2004-2005 Adam Milazzo

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

using System;
using System.Collections;
using System.Collections.Specialized;

// TODO: don't allow __repr__ to go into an infinite loop with circular references
// TODO: make IDictionary.GetEnumerator() (returns DictionaryEntry) different from
//       IEnumerator.GetEnumerator() (returns keys), if possible
// TODO: make objects that boa considers equal be the same key
namespace Boa.Runtime
{

[BoaType("dict")]
public class Dict : HybridDictionary, IComparable, IMapping, ICloneable, IRepresentable
{ public Dict() { }
  public Dict(IDictionary dict) { foreach(DictionaryEntry e in dict) Add(e.Key, e.Value); }
  public Dict(object o)
  { IEnumerator e = Ops.GetEnumerator(o);
    int i=0;
    while(e.MoveNext())
    { Tuple tup = e.Current as Tuple;
      if(tup==null || tup.Count!=2) throw Ops.TypeError("dict(): sequence element #{0} is not a 2-tuple", i);
      Add(tup.items[0], tup.items[1]);
    }
  }
  public Dict(int size) : base(size) { }

  #region DictEnumerator
  public class DictEnumerator : IEnumerator
  { public DictEnumerator(IDictionary dict) { e=dict.GetEnumerator(); }

    public object Current
    { get
      { if(current!=null) return current;
        DictionaryEntry e = (DictionaryEntry)this.e.Current;
        return current=new Tuple(e.Key, e.Value);
      }
    }

    public bool MoveNext() { current=null; return e.MoveNext(); }
    public void Reset() { current=null; e.Reset(); }

    IDictionaryEnumerator e;
    Tuple current;
  }
  #endregion

  public override bool Equals(object o)
  { if(o is Dict)
    { List l1 = items();
      l1.sort();
      List l2 = ((Dict)o).items();
      l2.sort();
      return l1.Equals(l2);
    }
    else return false;
  }
  
  public override int GetHashCode() { throw Ops.TypeError("dict objects are unhashable"); }
  public override string ToString() { return __repr__(); }

  #region IComparable Members
  public int CompareTo(object o)
  { if(o is Dict)
    { List l1 = items();
      l1.sort();
      List l2 = ((Dict)o).items();
      l2.sort();
      return l1.CompareTo(l2);
    }
    else return -1; // FIXME: compare by type names, like python does
  }
  #endregion

  #region IMapping Members
  public void clear() { Clear(); }
  public object copy() { return Clone(); }

  public object get(object key) { return this[key]; }
  public object get(object key, object defaultValue)
  { return Contains(key) ? this[key] : defaultValue;
  }

  public bool has_key(object key) { return Contains(key); }

  static Dict fromkeys(object seq) { return fromkeys(seq, null); }
  static Dict fromkeys(object seq, object value)
  { Dict d = new Dict();
    IEnumerator e = Ops.GetEnumerator(seq);
    while(e.MoveNext()) d[e.Current] = value;
    return d;
  }

  public object pop(object key)
  { object value = __getitem__(key);
    Remove(key);
    return value;
  }
  public object pop(object key, object defaultValue)
  { object value = Contains(key) ? this[key] : defaultValue;
    Remove(key);
    return value;
  }

  public Tuple popitem() // TODO: this is inefficient. any way to speed it up?
  { if(Count==0) throw Ops.KeyError("popitem(): dictionary is empty");
    IDictionaryEnumerator e = GetEnumerator();
    e.MoveNext();
    return new Tuple(e.Key, e.Value);
  }

  public object setdefault(object key) { return setdefault(key, null); }
  public object setdefault(object key, object defaultValue)
  { if(Contains(key)) return this[key];
    this[key] = defaultValue;
    return defaultValue;
  }
  
  public void update(object dict)
  { IDictionary d = dict as IDictionary;
    if(d!=null) foreach(DictionaryEntry e in d) this[e.Key] = e.Value;
    else
    { IMapping m = dict as IMapping;
      if(m!=null)
      { IEnumerator e = Ops.GetEnumerator(m.keys());
        while(e.MoveNext()) this[e.Current] = m.get(e.Current);
      }
      else
      { object getitem = Ops.GetAttr(dict, "__getitem__");
        IEnumerator e = Ops.GetEnumerator(Ops.Invoke(dict, "keys"));
        while(e.MoveNext()) this[e.Current] = Ops.Call(getitem, e.Current);
      }
    }
  }

  public List items()
  { List ret = new List(Count);
    foreach(DictionaryEntry e in this) ret.Add(new Tuple(e.Key, e.Value));
    return ret;
  }
  public List keys() { return new List(Keys); }
  public List values() { return new List(Values); }
  
  public IEnumerator iteritems() { return new DictEnumerator(this); }
  public IEnumerator iterkeys() { return Keys.GetEnumerator(); }
  public IEnumerator itervalues() { return Values.GetEnumerator(); }

  public void __delitem__(object key)
  { if(!Contains(key)) throw Ops.KeyError(Ops.Repr(key));
    Remove(key);
  }
  public object __getitem__(object key)
  { if(Contains(key)) return this[key];
    else throw Ops.KeyError(Ops.Repr(key));
  }
  public void __setitem__(object key, object value) { this[key]=value; }
  public int __len__() { return Count; }
  public bool __contains__(object key) { return Contains(key); }
  #endregion

  #region ICloneable Members
  public object Clone() { return new Dict(this); }
  #endregion

  #region IRepresentable Members
  public string __repr__()
  { System.Text.StringBuilder sb = new System.Text.StringBuilder();
    sb.Append('{');
    bool first=true;
    foreach(DictionaryEntry e in this)
    { if(first) first=false;
      else sb.Append(", ");
      sb.Append(Ops.Repr(e.Key));
      sb.Append(": ");
      sb.Append(Ops.Repr(e.Value));
    }
    sb.Append('}');
    return sb.ToString();
  }
  #endregion
}

} // namespace Boa.Runtime
Back to Top