v

[ActionScript 3.0] HashMap

· 2011-12-08 (목) 14:36:31 · 3392
 
 - 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,188건
+
제목 글쓴이 날짜 조회
12-08-29 조회 4,067
12-08-07 조회 3,749
12-08-03 조회 3,563
12-07-31 조회 3,666
12-07-30 조회 3,615
12-07-16 조회 3,839
12-07-15 조회 3,623
12-07-07 조회 3,522
12-07-06 조회 3,717
12-07-03 조회 3,688
12-06-29 조회 3,273
12-06-22 조회 3,651
12-06-17 조회 3,893
12-05-21 조회 3,824
12-05-02 조회 3,393
12-05-02 조회 4,187
12-05-02 조회 3,302
12-04-30 조회 3,485
12-04-30 조회 3,178
12-03-17 조회 4,027
12-03-10 조회 3,371
12-03-08 조회 3,901
12-03-03 조회 3,583
12-02-27 조회 4,999
12-02-17 조회 4,324
12-02-16 조회 4,575
12-02-03 조회 3,290
12-01-31 조회 3,185
12-01-24 조회 3,152
12-01-17 조회 3,874
12-01-05 조회 3,430
11-12-30 조회 3,154
11-12-23 조회 4,712
11-12-08 조회 3,437
11-12-07 조회 4,420
11-11-27 조회 3,826
11-11-25 조회 4,175
11-11-23 조회 3,638
11-11-22 조회 3,490
11-11-09 조회 3,208
11-10-23 조회 3,121
11-10-19 조회 3,092
11-10-08 조회 2,789
11-10-04 조회 2,796
11-10-04 조회 5,241
11-09-01 조회 4,318
11-08-13 조회 2,486
11-08-06 조회 3,167
11-07-11 조회 3,573
11-06-27 조회 2,943
11-06-21 조회 2,621
11-06-13 조회 2,407
11-06-07 조회 2,406
11-05-26 조회 2,401
11-04-20 조회 2,710
11-03-21 조회 2,805
11-03-18 조회 2,504
11-03-11 조회 2,531
11-02-07 조회 2,348
11-01-18 조회 2,672
11-01-12 조회 2,400
11-01-06 조회 2,495
10-12-10 조회 2,760
10-12-09 조회 2,591
10-12-08 조회 5,023
10-11-29 조회 2,691
10-11-28 조회 2,782
10-11-28 조회 2,620
10-11-19 조회 2,804
10-11-04 조회 2,692
10-10-21 조회 2,408
10-10-19 조회 3,355
10-10-12 조회 2,679
10-10-07 조회 2,512
10-09-30 조회 3,473
10-09-17 조회 3,869
10-09-17 조회 3,834
10-05-31 조회 3,516
09-12-31 조회 3,652
09-07-23 조회 4,977
08-09-16 조회 3,198
08-09-06 조회 2,933
08-09-04 조회 3,354
08-08-16 조회 4,404
08-08-15 조회 3,272
08-08-14 조회 4,482
08-08-12 조회 3,958
08-07-23 조회 2,781
08-07-10 조회 3,721
08-07-04 조회 2,945
08-06-27 조회 7,593
08-06-19 조회 4,884
08-05-27 조회 5,499
08-05-25 조회 5,325
08-05-23 조회 3,396
08-05-23 조회 4,540
08-05-21 조회 4,916
08-05-20 조회 4,038
08-05-20 조회 3,276
08-04-18 조회 4,319