v

[ActionScript 3.0] HashMap

· 2011-12-08 (목) 14:36:31 · 3571
 
 - 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건
+
제목 글쓴이 날짜 조회
11-10-18 조회 2,653
11-10-18 조회 2,573
11-10-18 조회 2,491
11-10-17 조회 3,859
11-10-17 조회 5,139
11-10-17 조회 2,486
11-10-17 조회 2,756
11-10-17 조회 3,849
11-10-16 조회 2,523
11-10-16 조회 3,167
11-10-16 조회 3,610
11-10-15 조회 1.2만
11-10-15 조회 3,291
11-10-15 조회 3,027
11-10-15 조회 3,710
11-10-15 조회 3,692
11-10-15 조회 2,938
11-10-14 조회 2,961
11-10-13 조회 2,722
11-10-13 조회 4,504
11-10-12 조회 2,530
11-10-12 조회 2,691
11-10-12 조회 3,800
11-10-12 조회 4,097
11-10-12 조회 2,442
11-10-11 조회 2,963
11-10-11 조회 2,609
11-10-11 조회 2,787
11-10-11 조회 3,096
11-10-10 조회 2,760
11-10-10 조회 2,742
11-10-10 조회 2,449
11-10-10 조회 2,440
11-10-10 조회 2,718
11-10-10 조회 2,836
11-10-09 조회 2,644
11-10-08 조회 2,636
11-10-08 조회 4,570
11-10-08 조회 2,922
11-10-07 조회 3,134
11-10-07 조회 2,971
11-10-07 조회 3,468
11-10-06 조회 4,059
11-10-06 조회 2,618
11-10-06 조회 2,710
11-10-06 조회 3,919
11-10-06 조회 2,766
11-10-06 조회 4,305
11-10-06 조회 2,533
11-10-05 조회 2,818
11-10-05 조회 2,954
11-10-05 조회 4,048
11-10-04 조회 2,935
11-10-04 조회 5,379
11-10-04 조회 2,840
11-10-04 조회 2,391
11-10-03 조회 2,772
11-10-02 조회 2,656
11-10-02 조회 3,383
11-10-01 조회 2,413
11-10-01 조회 2,727
11-09-30 조회 2,613
11-09-30 조회 2,713
11-09-30 조회 2,895
11-09-30 조회 2,903
11-09-30 조회 3,668
11-09-30 조회 2,674
11-09-30 조회 3,102
11-09-29 조회 4,927
11-09-29 조회 2,755
11-09-29 조회 2,655
11-09-29 조회 3,063
11-09-28 조회 3,164
11-09-28 조회 3,940
11-09-28 조회 3,140
11-09-28 조회 2,517
11-09-28 조회 2,459
11-09-28 조회 2,334
11-09-28 조회 2,627
11-09-28 조회 4,130
11-09-26 조회 2,544
11-09-26 조회 2,613
11-09-26 조회 2,734
11-09-25 조회 3,075
11-09-24 조회 2,554
11-09-23 조회 2,834
11-09-23 조회 3,425
11-09-23 조회 2,552
11-09-23 조회 2,952
11-09-23 조회 2,296
11-09-22 조회 2,468
11-09-22 조회 2,879
11-09-21 조회 3,248
11-09-21 조회 3,276
11-09-20 조회 2,838
11-09-20 조회 2,670
11-09-20 조회 3,192
11-09-19 조회 2,712
11-09-19 조회 6,003
11-09-19 조회 3,802