v

[ActionScript 3.0] HashMap

· 2011-12-08 (목) 14:36:31 · 3607
 
 - ActionsCript 작업중 , hashmap를 이용할 일이 었어서 검색을 하던 중 발견 하여 . 올려 봅니다.

class com.lib.data.holder.map.HashMap
{
   
    public var keys:Array;
    public var values:Array;
    //
    public function HashMap(source)
    {
        super();
        this.keys = new Array();
        this.values = new Array();
        this.populate(source);
    }
    public function populate(source)
    {
        if (source)
        {
            for (var i in source)
            {
                this.put(i, source[i]);
            }
        }
    }

    public function containsKey(key)
    {
        return (this.findKey(key) > -1);
    }
    public function containsValue(value)
    {
        return (this.findValue(value) > -1);
    }
    public function getKeys(Void)
    {
        return (this.keys.slice());
    }
    public function getValues(Void)
    {
        return (this.values.slice());
    }
    public function get(key)
    {
        return (values[this.findKey(key)]);
    }
    public function put(key, value)
    {
        var oldKey;
        var theKey = this.findKey(key);
        if (theKey < 0)
        {
            this.keys.push(key);
            this.values.push(value);
        }
        else
        {
            oldKey = values[theKey];
            this.values[theKey] = value;
        }
        return (oldKey);
    }
    public function putAll(map)
    {
        var theValues = map.getValues();
        var theKeys = map.getKeys();
        var max = keys.length;
        for (var i = 0; i < max; i = i - 1)
        {
            this.put(theKeys[i], theValues[i]);
        }
    }
    public function clear(Void)
    {
        this.keys = new Array();
        this.values = new Array();
    }
    public function remove(key)
    {
        var theKey = this.findKey(key);
        if (theKey > -1)
        {
            var theValue = this.values[theKey];
            this.values.splice(theKey, 1);
            this.keys.splice(theKey, 1);
            return (theValue);
        }
    }
    public function size(Void)
    {
        return (this.keys.length);
    }
    public function isEmpty(Vois)
    {
        return (this.size() < 1);
    }
    public function findKey(key)
    {
        var index = this.keys.length;
        while(this.keys[--index] !== key && index > -1)
        {
        }
        return(index);
    }
    public function findValue(value)
    {
        var index = this.values.length;
        while(this.values[--index] !== value && index > -1)
        {
        }
        return (index);
    }
}


##사용방법##
import com.lib.data.holder.map.*;

/* Add John to your `contact` list */
var myContacts:HashMap = new HashMap();
myContacts.put("John", new HashMap());

/* Get John, and add a `contacts` list */
var contact:HashMap = myContacts.get("John");
contact.put("contacts", new HashMap());

/* Add Johns contacts to the list */
var theContacts:HashMap = contact.get("contacts");
theContacts.put(0, "Mary");
theContacts.put(1, "Alex");
theContacts.put(2, "Julie");

/* Is Alex a contact of John? */
var isContact = myContacts.get("John").get("contacts").containsValue("Alex");
if (isContact) trace("Alex is contact of John");
else trace("Alex is Not a contact of John");

/* Is Mark a contact of John? */
var isContact = myContacts.get("John").get("contacts").containsValue("Mark");
if (isContact) trace("Mark is contact of John");
else trace("Mark is Not a contact of John");

 
댓글을 작성하시려면 로그인이 필요합니다.

프로그램

8,187건
+
제목 글쓴이 날짜 조회
12-05-10 조회 3,001
12-05-10 조회 3,681
12-05-09 조회 4,114
12-05-09 조회 6,367
12-05-08 조회 3,352
12-05-08 조회 3,584
12-05-08 조회 3,207
12-05-08 조회 3,029
12-05-08 조회 3,043
12-05-08 조회 3,430
12-05-08 조회 4,613
12-05-07 조회 3,082
12-05-07 조회 4,291
12-05-07 조회 4,387
12-05-07 조회 4,630
12-05-07 조회 3,303
12-05-06 조회 3,591
12-05-06 조회 3,228
12-05-05 조회 3,297
12-05-05 조회 3,175
12-05-05 조회 3,069
12-05-04 조회 3,225
12-05-04 조회 3,244
12-05-04 조회 3,419
12-05-04 조회 4,141
12-05-03 조회 2,996
12-05-03 조회 3,248
12-05-03 조회 3,085
12-05-03 조회 2,855
12-05-03 조회 3,258
12-05-03 조회 5,266
12-05-03 조회 4,381
12-05-03 조회 3,909
12-05-03 조회 2,961
12-05-02 조회 3,511
12-05-02 조회 4,313
12-05-02 조회 3,427
12-05-02 조회 3,498
12-05-02 조회 3,283
12-05-02 조회 3,379
12-05-02 조회 4,569
12-05-02 조회 3,004
12-05-02 조회 3,272
12-05-01 조회 4,748
12-05-01 조회 3,378
12-04-30 조회 2,888
12-04-30 조회 3,509
12-04-30 조회 3,371
12-04-30 조회 3,345
12-04-30 조회 3,655
12-04-30 조회 4,444
12-04-30 조회 3,319
12-04-28 조회 3,815
12-04-27 조회 2,906
12-04-27 조회 2,961
12-04-27 조회 2,861
12-04-26 조회 2,829
12-04-26 조회 3,178
12-04-26 조회 3,122
12-04-26 조회 2,791
12-04-26 조회 3,093
12-04-26 조회 3,692
12-04-26 조회 2,967
12-04-26 조회 3,628
12-04-26 조회 2,688
12-04-26 조회 3,030
12-04-26 조회 2,760
12-04-26 조회 3,007
12-04-26 조회 2,773
12-04-26 조회 3,204
12-04-26 조회 3,255
12-04-26 조회 3,075
12-04-26 조회 4,940
12-04-26 조회 2,812
12-04-26 조회 3,067
12-04-26 조회 2,897
12-04-26 조회 3,544
12-04-26 조회 3,237
12-04-26 조회 3,930
12-04-26 조회 4,236
12-04-26 조회 3,489
12-04-25 조회 5,463
12-04-25 조회 3,743
12-04-25 조회 3,128
12-04-25 조회 4,526
12-04-25 조회 4,962
12-04-25 조회 2,650
12-04-25 조회 2,963
12-04-25 조회 2,794
12-04-25 조회 2,847
12-04-25 조회 2,815
12-04-25 조회 3,032
12-04-24 조회 4,155
12-04-24 조회 2,664
12-04-24 조회 2,569
12-04-24 조회 2,800
12-04-24 조회 2,737
12-04-24 조회 2,643
12-04-24 조회 3,021
12-04-24 조회 3,034