Difference and explanation

06/01/2012 12:09 marlyandedsel#1
code 1

PHP Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 
System
{
    public class 
SafeDictionary<T1T2>
    {
        private 
Dictionary<T1T2DictBase;

        public 
SafeDictionary(int capacity)
        {
            
DictBase = new Dictionary<T1T2>(capacity);
        }
        public 
SafeDictionary()
        {
            
DictBase = new Dictionary<T1T2>();
        }
        public 
Dictionary<T1T2Base
        
{
            
get
            
{
                return 
DictBase;
            }
        }
        public 
int Count
        
{
            
get
            
{
                return 
DictBase.Count;
            }
        }

        public 
bool Add(T1 keyT2 value)
        {
            if (!
DictBase.ContainsKey(key))
            {
                
DictBase.Add(keyvalue);
                return 
true;
            }
            return 
false;
        }

        public 
void Remove(T1 key)
        {
            
DictBase.Remove(key);
        }

        public 
bool ContainsKey(T1 key)
        {
            return 
DictBase.ContainsKey(key);
        }

        public 
bool ContainsValue(T2 value)
        {
            return 
DictBase.ContainsValue(value);
        }

        public 
void Clear()
        {
            
DictBase.Clear();
        }

        public 
T2 this[T1 key]
        {
            
get
            
{
                if (
ContainsKey(key))
                    return 
DictBase[key];
                else return default(
T2);
            }
        }

        public 
Dictionary<T1T2>.ValueCollection Values
        
{
            
get
            
{
                if (
DictBase == null)
                    
DictBase = new Dictionary<T1T2>();
                return 
DictBase.Values;
            }
        }

        public 
bool TryGetValue(T1 keyout T2 value)
        {
            return 
DictBase.TryGetValue(keyout value);
        }

        
/*internal void Add(ushort p, Conquer_Online_Server.Interfaces.IProf Prof)
        {
           throw new NotImplementedException();
        } */
    
}

I have Argument Null Exception at that code in this portion
PHP Code:
   public bool ContainsKey(T1 key)
        {
            return 
DictBase.ContainsKey(key);
        } 
Code 2
Are they just same function of code 1?
PHP Code:
using System;
using System.Linq;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace 
System.Collections.Generic
{
    public class 
ThreadSafeDictionary<T1T2>
    {
        private 
Dictionary<T1T2SafeDictionaryBase;
        public 
T2[] Values = new T2[0];

        public 
ThreadSafeDictionary(int capacity)
        {
            
SafeDictionaryBase = new Dictionary<T1T2>(capacity 10);
        }
        public 
int Count
        
{
            
get
            
{
                return 
SafeDictionaryBase.Count;
            }
        }
        public 
Dictionary<T1T2Base
        
{
            
get
            
{
                return 
SafeDictionaryBase;
            }
        }
        public 
void Add(T1 keyT2 value)
        {
            if (
SafeDictionaryBase.ContainsKey(key) == false)
            {
                
lock (SafeDictionaryBase)
                    
SafeDictionaryBase.Add(keyvalue);
                
safeUpdate();
            }
        }
        public 
void Remove(T1 key)
        {
            
lock (SafeDictionaryBase)
                
SafeDictionaryBase.Remove(key);
            
safeUpdate();
        }

        public 
T2 this[T1 key]
        {
            
get
            
{
                if (
ContainsKey(key))
                    return 
SafeDictionaryBase[key];
                else return default(
T2);
            }
        }

        public 
bool TryGetValue(T1 keyout T2 value)
        {
            return 
SafeDictionaryBase.TryGetValue(keyout value);
        }

        public 
bool ContainsKey(T1 key)
        {
            return 
SafeDictionaryBase.ContainsKey(key);
        }

        public 
bool ContainsValue(T2 value)
        {
            return 
SafeDictionaryBase.ContainsValue(value);
        }

        private 
void safeUpdate()
        {
            if (
Values == null)
            {
                
Values SafeDictionaryBase.Values.ToArray();
            }
            else
            {
                if (
System.Threading.Monitor.TryEnter(Values1))
                {
                    
Values SafeDictionaryBase.Values.ToArray();
                }
            }
        }
    }

these are all from public source but I just need to know if this two is good or not..
06/01/2012 12:22 I don't have a username#2
They key you specified is null or the dict is null.
06/01/2012 12:52 Korvacs#3
ConcurrentDictionary

That is all.
06/01/2012 19:36 Zeroxelli#4
Quote:
Originally Posted by Korvacs View Post
ConcurrentDictionary

That is all.
^ Best advice to follow is right there.
06/01/2012 22:20 I don't have a username#5
Code:
    public interface ThreadedObject
    {
        string Name { get; set; }
        uint UID { get; set; }
        ThreadedType EntityType { get; set; }
        ushort MapID { get; set; }
    }
    public enum ThreadedType
    {
        Pet,
        Player,
        Mob,
        Guard,
        ReviverGuard,
        Boss,
        SOB
    }
 public class ThreadedCollection : ConcurrentDictionary<uint, ThreadedObject>
    {
        public bool Add(uint Key, ThreadedObject Value)
        {
            return base.TryAdd(Key, Value);
        }
        public bool Remove(uint Key)
        {
            ThreadedObject Value;
            return base.TryRemove(Key, out Value);
        }
        public new ThreadedObject this[uint Key]
        {
            get
            {
                ThreadedObject Value = null;
                base.TryGetValue(Key, out Value);
                return Value;
            }
        }

        public ThreadSafeList<ThreadedObject> Search(ThreadedType ObjectType)
        {
            ThreadSafeList<ThreadedObject> ObjectList = new ThreadSafeList<ThreadedObject>();
            foreach (ThreadedObject Object in base.Values)
                if (Object.EntityType == ObjectType)
                    ObjectList.Add(Object);

            return ObjectList;
        }
        public ThreadSafeList<ThreadedObject> Search(uint UIDStart, uint UIDEnd)
        {
            ThreadSafeList<ThreadedObject> ObjectList = new ThreadSafeList<ThreadedObject>();
            foreach (ThreadedObject Object in base.Values)
                if (Object.UID > UIDStart && Object.UID < UIDEnd)
                    ObjectList.Add(Object);

            return ObjectList;
        }
        public ThreadSafeList<ThreadedObject> Search(ushort MapID)
        {
            ThreadSafeList<ThreadedObject> ObjectList = new ThreadSafeList<ThreadedObject>();
            foreach (ThreadedObject Object in base.Values)
                if (Object.MapID == MapID)
                    ObjectList.Add(Object);

            return ObjectList;
        }
        public ThreadSafeList<ThreadedObject> Search(ThreadedType ObjectType, ushort MapID)
        {
            ThreadSafeList<ThreadedObject> ObjectList = new ThreadSafeList<ThreadedObject>();
            foreach (ThreadedObject Object in base.Values)
                if (Object.EntityType == ObjectType && Object.MapID == MapID)
                    ObjectList.Add(Object);

            return ObjectList;
        }
        public ThreadedObject Search(string Name)
        {
            uint UID = 0;
            foreach (ThreadedObject Object in base.Values)
                if (Object.Name == Name)
                    UID = Object.UID;
            return this[UID];
        }
        public ThreadedObject Search(ThreadedType ObjectType, string Name)
        {
            uint UID = 0;
            foreach (ThreadedObject Object in base.Values)
                if (Object.Name == Name && Object.EntityType == ObjectType)
                    UID = Object.UID;
            return this[UID];
        }
    }
:D
06/01/2012 23:09 Zeroxelli#6
That also works.
06/01/2012 23:15 I don't have a username#7
Quote:
Originally Posted by Zeroxelli View Post
That also works.
It's a concurrentdictionary o.o
06/01/2012 23:20 Zeroxelli#8
Quote:
Originally Posted by I don't have a username View Post
It's a concurrentdictionary o.o
At the root, yes. But with methods so they can still use the method [names] "Remove" and "Add" instead of the native TryRemove and TryAdd. Less confusing to newbies.
06/01/2012 23:32 I don't have a username#9
Quote:
Originally Posted by Zeroxelli View Post
At the root, yes. But with methods so they can still use the method [names] "Remove" and "Add" instead of the native TryRemove and TryAdd. Less confusing to newbies.
Yup, but does not make a difference really, I just did it, because of less typing lool.
06/01/2012 23:35 Zeroxelli#10
Quote:
Originally Posted by I don't have a username View Post
Yup, but does not make a difference really, I just did it, because of less typing lool.
You'd be surprised at the amount of people who post on forums asking how to add and remove things from a ConcurrentDictionary. As if IntelliSense didn't tell them as soon as they type the period...
06/02/2012 00:03 nTL3fTy#11
People still use ushort as a map id? If you went and looked at the latest GameMap.dat (or a patch since they added the arena), you'd quickly notice that its an int.
06/02/2012 00:10 Zeroxelli#12
Oh, wow. Why would they need map IDs larger than 0xFFFF (65535)? They don't have anywhere near that many maps.
06/02/2012 00:17 I don't have a username#13
Quote:
Originally Posted by nTL3fTy View Post
People still use ushort as a map id? If you went and looked at the latest GameMap.dat (or a patch since they added the arena), you'd quickly notice that its an int.
I'm running patch 4267.
06/02/2012 00:18 Zeroxelli#14
Touche.

No point using the extra two bytes of memory space if you're never going to use it. Two bytes may seem small, but getting into the habit of things like that are how servers end up being inefficient.
06/02/2012 00:24 _DreadNought_#15
Quote:
Originally Posted by nTL3fTy View Post
People still use ushort as a map id? If you went and looked at the latest GameMap.dat (or a patch since they added the arena), you'd quickly notice that its an int.
I noticed that a long time ago;