public WeakMapEntry(K key, V value, int hash, IMapEntry <K, V> nextEntry) : base(key) { this.hash = hash; this.nextEntry = nextEntry; this.value = value; }
public void EntryAtOutOfRangeHighReturnsNull() { MapEntry me = new MapEntry(1, "abc"); IMapEntry me1 = me.entryAt(4); Expect(me1).To.Be.Null(); }
public static PersistentStructMap create(Def def, ISeq keyvals) { object[] vals = new object[def.Keyslots.count()]; IPersistentMap ext = PersistentHashMap.EMPTY; for (; keyvals != null; keyvals = keyvals.next().next()) { if (keyvals.next() == null) { throw new ArgumentException(String.Format("No value supplied for key: {0}", keyvals.first())); } object k = keyvals.first(); object v = RT.second(keyvals); IMapEntry me = def.Keyslots.entryAt(k); if (me != null) { vals[Util.ConvertToInt(me.val())] = v; } else { ext = ext.assoc(k, v); } } return(new PersistentStructMap(null, def, vals, ext)); }
public void EntryAtOutOfRangeLowReturnsNull() { MapEntry me = new MapEntry(1, "abc"); IMapEntry me1 = me.entryAt(-4); Expect(me1, Null); }
protected override void Transfer(IMapEntry <K, V>[] newTable) { int newCapacityMinus1 = newTable.Length - 1; IMapEntry <K, V>[] table = this.table; for (int a = table.Length; a-- > 0;) { IMapEntry <K, V> entry = table[a], next; while (entry != null) { next = GetNextEntry(entry); // only handle this entry if it has still a valid key if (entry.Key != null) { int i = entry.Hash & newCapacityMinus1; SetNextEntry(entry, newTable[i]); newTable[i] = entry; } entry = next; } } base.Transfer(newTable); }
/// <summary> /// Returns the key/value pair for this key. /// </summary> /// <param name="key">The key to retrieve</param> /// <returns>The key/value pair for the key, or null if the key is not in the map.</returns> public override IMapEntry entryAt(object key) { IMapEntry me = _def.Keyslots.entryAt(key); return(me == null ? _ext.entryAt(key) : new MapEntry(me.key(), _vals[Util.ConvertToInt(me.val())])); }
/// <summary> /// Gets the value associated with a key. /// </summary> /// <param name="key">The key to look up.</param> /// <param name="notFound">The value to return if the key is not present.</param> /// <returns>The associated value (or <c>notFound</c> if the key is not present.</returns> public override object valAt(object key, object notFound) { IMapEntry me = _def.Keyslots.entryAt(key); return(me == null ? _ext.valAt(key, notFound) : _vals[Util.ConvertToInt(me.val())]); }
/// <summary> /// Gets the value associated with a key. /// </summary> /// <param name="key">The key to look up.</param> /// <param name="notFound">The value to return if the key is not present.</param> /// <returns>The associated value (or <c>notFound</c> if the key is not present.</returns> public override object valAt(object key, object notFound) { IMapEntry e = entryAt(key); return(e != null ? e.val() : notFound); }
public void EntryAtOnIndexOutOfRangeReturnsNull() { CPV v = new CPV(new object[] { 4, 5, 6 }); IMapEntry me = v.entryAt(5); Expect(me, Null); }
public void EntryAtOnNonNumericReturnsNull() { CPV v = new CPV(new object[] { 4, 5, 6 }); IMapEntry me = v.entryAt("a"); Expect(me, Null); }
public void EntryAtOnIndexInRangeReturnsEntry() { CPV v = new CPV(new object[] { 4, 5, 6 }); IMapEntry me = v.entryAt(1); Expect(me.key(), EqualTo(1)); Expect(me.val(), EqualTo(5)); }
public override void Remove() { if (!removeAllowed) { throw new NotSupportedException(); } hashMap.Remove(currEntry.Key); currEntry = null; }
private static CustomAttributeBuilder CreateCustomAttributeBuilder(IMapEntry me) { Type t = (Type)me.key(); IPersistentMap args = (IPersistentMap)me.val(); object[] ctorArgs = new object[0]; Type[] ctorTypes = Type.EmptyTypes; List <PropertyInfo> pInfos = new List <PropertyInfo>(); List <Object> pVals = new List <object>(); List <FieldInfo> fInfos = new List <FieldInfo>(); List <Object> fVals = new List <object>(); for (ISeq s = RT.seq(args); s != null; s = s.next()) { IMapEntry m2 = (IMapEntry)s.first(); Keyword k = (Keyword)m2.key(); object v = m2.val(); if (k == ARGS_KEY) { ctorArgs = GetCtorArgs((IPersistentVector)v); ctorTypes = GetCtorTypes(ctorArgs); } else { string name = k.Name; PropertyInfo pInfo = t.GetProperty(name); if (pInfo != null) { pInfos.Add(pInfo); pVals.Add(v); continue; } FieldInfo fInfo = t.GetField(name); if (fInfo != null) { fInfos.Add(fInfo); fVals.Add(v); continue; } throw new ArgumentException(String.Format("Unknown field/property: {0} for attribute: {1}", k.Name, t.FullName)); } } ConstructorInfo ctor = t.GetConstructor(ctorTypes); if (ctor == null) { throw new ArgumentException(String.Format("Unable to find constructor for attribute: {0}", t.FullName)); } CustomAttributeBuilder cb = new CustomAttributeBuilder(ctor, ctorArgs, pInfos.ToArray(), pVals.ToArray(), fInfos.ToArray(), fVals.ToArray()); return(cb); }
protected override bool RemoveEldestEntry(IMapEntry eldest) { if (this.Size() > maxSize) { Cleanup(eldest); return(true); } return(false); }
public void EntryAtOnExistingKeyWorks() { MapEntry me = new MapEntry(1, "abc"); IMapEntry me1 = me.entryAt(0); IMapEntry me2 = me.entryAt(1); Expect(me1.key()).To.Equal(0); Expect(me1.val()).To.Equal(1); Expect(me2.key()).To.Equal(1); Expect(me2.val()).To.Equal("abc"); }
/// <summary> /// Get the box of the current binding on the stack for this var, or null if no binding. /// </summary> /// <returns>The box of the current binding on the stack (or null if no binding).</returns> Box GetThreadBinding() { if (_count.get() > 0) { IMapEntry e = CurrentFrame.Bindings.entryAt(this); if (e != null) { return((Box)e.val()); } } return(null); }
public void ValAtOnIndexOutOfRangeReturnsDefault() { CPV v = new CPV(new object[] { 4, 5, 6 }); IMapEntry me = v.entryAt(5); object val1 = v.valAt(4); object val2 = v.valAt(4, "abc"); Expect(val1, Null); Expect(val2, EqualTo("abc")); }
public TBox getThreadBinding() { if (_threadBound.get()) { IMapEntry e = CurrentFrame.Bindings.entryAt(this); if (e != null) { return((TBox)e.val()); } } return(null); }
private static IPersistentVector flattenMap(object form) { IPersistentVector keyvals = PersistentVector.EMPTY; for (ISeq s = RT.seq(form); s != null; s = s.next()) { IMapEntry e = (IMapEntry)s.first(); keyvals = (IPersistentVector)keyvals.cons(e.key()); keyvals = (IPersistentVector)keyvals.cons(e.val()); } return(keyvals); }
public static IFn getAccessor(Def def, object key) { IMapEntry e = def.Keyslots.entryAt(key); if (e == null) { throw new ArgumentException("Not a key of struct"); } int i = (int)e.val(); return(new AccessorFn(def, i)); }
public void EntryAtReturnsEntryforKey() { Dictionary <int, string> d = new Dictionary <int, string>(); d[1] = "a"; d[2] = "b"; IPersistentMap m = PersistentArrayMap.create(d); IMapEntry me = m.entryAt(1); Expect(me.key(), EqualTo(1)); Expect(me.val(), EqualTo("a")); }
/// <summary> /// Add a new key/value pair. /// </summary> /// <param name="key">The key</param> /// <param name="val">The value</param> /// <returns>A new map with key+value added.</returns> /// <remarks>Overwrites an exising value for the <paramref name="key"/>, if present.</remarks> public override IPersistentMap assoc(object key, object val) { IMapEntry me = _def.Keyslots.entryAt(key); if (me != null) { int i = Util.ConvertToInt(me.val()); object[] newVals = (object[])_vals.Clone(); newVals[i] = val; return(makeNew(_meta, _def, newVals, _ext)); } return(makeNew(_meta, _def, _vals, _ext.assoc(key, val))); }
public static Expr Parse(IPersistentMap form) { IPersistentVector keyvals = PersistentVector.EMPTY; for (ISeq s = RT.seq(form); s != null; s = s.next()) { IMapEntry e = (IMapEntry)s.first(); keyvals = (IPersistentVector)keyvals.cons(Compiler.GenerateAST(e.key())); keyvals = (IPersistentVector)keyvals.cons(Compiler.GenerateAST(e.val())); } Expr ret = new MapExpr(keyvals); return(Compiler.OptionallyGenerateMetaInit(form, ret)); }
public override bool Equals(Object obj) { if (obj == this) { return(true); } if (!(obj is IMapEntry <K, V>)) { return(false); } IMapEntry <K, V> other = (IMapEntry <K, V>)obj; return(Object.Equals(Key, other.Key) && Object.Equals(Value, other.Value)); }
public static Associative getThreadBindings() { Frame f = CurrentFrame; IPersistentMap ret = PersistentHashMap.EMPTY; for (ISeq bs = f.Bindings.seq(); bs != null; bs = bs.next()) { IMapEntry e = (IMapEntry)bs.first(); Var v = (Var)e.key(); TBox b = (TBox)e.val(); ret = ret.assoc(v, b.Val); } return(ret); }
/// <summary> /// Remove a key entry. /// </summary> /// <param name="key">The key to remove</param> /// <returns>A new map with the key removed (or the same map if the key is not contained).</returns> public override IPersistentMap without(object key) { IMapEntry me = _def.Keyslots.entryAt(key); if (me != null) { throw new InvalidOperationException("Can't remove struct key"); } IPersistentMap newExt = _ext.without(key); return(newExt == _ext ? this : makeNew(_meta, _def, _vals, newExt)); }
public override object first() { object entry = _seq.first(); IMapEntry me = entry as IMapEntry; if (me != null) { return(me.key()); } else if (entry is DictionaryEntry) { return(((DictionaryEntry)entry).Key); } throw new InvalidCastException("Cannot convert hashtable entry to IMapEntry or DictionaryEntry"); }
/// <summary> /// Push a new frame of bindings onto the binding stack. /// </summary> /// <param name="bindings">The new bindings.</param> /// <remarks>Lowercase name for core.clj compatability.</remarks> public static void pushThreadBindings(Associative bindings) { Frame f = CurrentFrame; Associative bmap = f.Bindings; for (ISeq bs = bindings.seq(); bs != null; bs = bs.next()) { IMapEntry e = (IMapEntry)bs.first(); Var v = (Var)e.key(); v.Validate(e.val()); v._count.incrementAndGet(); bmap = bmap.assoc(v, new Box(e.val())); } CurrentFrame = new Frame(bindings, bmap, f); }
private static List <CustomAttributeBuilder> CreateCustomAttributeBuilders(IMapEntry me) { Type t = (Type)me.key(); IPersistentSet inits = (IPersistentSet)me.val(); List <CustomAttributeBuilder> builders = new List <CustomAttributeBuilder>(inits.count()); for (ISeq s = RT.seq(inits); s != null; s = s.next()) { IPersistentMap init = (IPersistentMap)s.first(); builders.Add(CreateCustomAttributeBuilder(t, init)); } return(builders); }
protected TempHashMap <IMapEntry <K, V>, K, V> CreateCopy() { // Copy existing data in FULLY NEW STRUCTURE IMapEntry <K, V>[] table = this.table; TempHashMap <IMapEntry <K, V>, K, V> backupMap = CreateEmptyInstance(); if (AutoCleanupNullValue) { for (int a = table.Length; a-- > 0;) { IMapEntry <K, V> entry = table[a]; while (entry != null) { K key = entry.Key; if (key != null) { V value = entry.Value; WeakReference valueAsRef = value as WeakReference; if (valueAsRef.Target != null) { // Only copy the entry if the value content is still valid backupMap.Put(CloneKey(key), CloneValue(value)); } } entry = entry.NextEntry; } } } else { for (int a = table.Length; a-- > 0;) { IMapEntry <K, V> entry = table[a]; while (entry != null) { K key = entry.Key; if (key != null) { V value = entry.Value; backupMap.Put(CloneKey(key), CloneValue(value)); } entry = entry.NextEntry; } } } return(backupMap); }
private static List<CustomAttributeBuilder> CreateCustomAttributeBuilders(IMapEntry me) { Type t = (Type)me.key(); IPersistentSet inits = (IPersistentSet)me.val(); List<CustomAttributeBuilder> builders = new List<CustomAttributeBuilder>(inits.count()); for (ISeq s = RT.seq(inits); s != null; s = s.next()) { IPersistentMap init = (IPersistentMap)s.first(); builders.Add(CreateCustomAttributeBuilder(t, init)); } return builders; }
private static CustomAttributeBuilder CreateCustomAttributeBuilder(IMapEntry me) { Type t = (Type) me.key(); IPersistentMap args = (IPersistentMap)me.val(); object[] ctorArgs = new object[0]; Type[] ctorTypes = Type.EmptyTypes; List<PropertyInfo> pInfos = new List<PropertyInfo>(); List<Object> pVals = new List<object>(); List<FieldInfo> fInfos = new List<FieldInfo>(); List<Object> fVals = new List<object>(); for (ISeq s = RT.seq(args); s != null; s = s.next()) { IMapEntry m2 = (IMapEntry)s.first(); Keyword k = (Keyword) m2.key(); object v = m2.val(); if (k == ARGS_KEY) { ctorArgs = GetCtorArgs((IPersistentVector)v); ctorTypes = GetCtorTypes(ctorArgs); } else { string name = k.Name; PropertyInfo pInfo = t.GetProperty(name); if (pInfo != null) { pInfos.Add(pInfo); pVals.Add(v); continue; } FieldInfo fInfo = t.GetField(name); if (fInfo != null) { fInfos.Add(fInfo); fVals.Add(v); continue; } throw new ArgumentException(String.Format("Unknown field/property: {0} for attribute: {1}", k.Name, t.FullName)); } } ConstructorInfo ctor = t.GetConstructor(ctorTypes); if (ctor == null) throw new ArgumentException(String.Format("Unable to find constructor for attribute: {0}", t.FullName)); CustomAttributeBuilder cb = new CustomAttributeBuilder(ctor,ctorArgs,pInfos.ToArray(),pVals.ToArray(),fInfos.ToArray(),fVals.ToArray()); return cb; }
public void ICollection_CopyTo_Copies() { Dictionary<int, string> d = new Dictionary<int, string>(); d[1] = "a"; d[2] = "b"; ICollection c = (ICollection)PersistentArrayMap.create(d); IMapEntry[] a = new IMapEntry[c.Count]; c.CopyTo(a, 0); int key0 = (int)a[0].key(); int key1 = (int)a[1].key(); string val0 = (string)a[0].val(); string val1 = (string)a[1].val(); Expect(key0, EqualTo(1) | EqualTo(2)); Expect(key1, EqualTo(key0 == 1 ? 2 : 1)); Expect(val0, EqualTo(key0 == 1 ? "a" : "b")); Expect(val1, EqualTo(key1 == 1 ? "a" : "b")); }