v

[ActionScript 3.0] HashMap

· 2011-12-08 (목) 14:36:31 · 3497
 
 - 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건
+
제목 글쓴이 날짜 조회
11-10-25 조회 2,508
11-10-18 조회 4,043
11-10-18 조회 2,429
11-10-15 조회 3,649
11-10-10 조회 2,707
11-10-02 조회 2,609
11-09-28 조회 2,426
11-09-24 조회 2,518
11-09-22 조회 2,411
11-09-05 조회 2,895
11-08-31 조회 2,275
11-08-31 조회 2,588
11-08-31 조회 2,444
11-08-29 조회 2,982
11-08-28 조회 2,482
11-08-25 조회 2,533
11-08-17 조회 2,248
11-08-16 조회 2,483
11-08-11 조회 2,942
11-08-07 조회 2,650
11-08-03 조회 2,358
11-07-22 조회 2,105
11-07-12 조회 3,321
11-06-27 조회 2,100
11-06-01 조회 1,768
11-05-17 조회 2,561
11-05-01 조회 2,623
11-04-28 조회 2,912
11-04-28 조회 2,970
11-04-13 조회 2,624
11-03-27 조회 2,752
11-03-27 조회 2,622
11-03-14 조회 2,039
11-03-02 조회 2,356
11-03-02 조회 2,591
11-02-27 조회 3,016
11-02-23 조회 2,396
11-02-19 조회 2,536
11-02-14 조회 4,152
11-02-03 조회 2,937
11-01-24 조회 3,889
11-01-04 조회 2,421
10-12-08 조회 5,432
10-11-15 조회 2,441
10-11-14 조회 2,624
10-11-14 조회 3,646
10-10-29 조회 3,712
10-10-19 조회 2,722
10-10-07 조회 2,549
10-10-05 조회 4,458
10-09-18 조회 2,756
10-09-18 조회 2,500
10-09-17 조회 4,908
10-09-15 조회 2,845
10-09-15 조회 2,380
10-09-15 조회 2,862
10-09-15 조회 2,697
10-09-11 조회 4,028
10-09-11 조회 2,524
10-09-11 조회 2,573
10-09-07 조회 3,843
10-09-07 조회 3,385
10-08-29 조회 2,848
10-07-01 조회 3,093
10-06-30 조회 2,496
10-06-30 조회 2,673
10-06-30 조회 3,028
10-06-13 조회 2,064
10-04-15 조회 3,233
10-04-04 조회 3,467
10-02-23 조회 3,574
10-02-09 조회 3,166
10-01-25 조회 2,676
10-01-25 조회 2,777
09-12-27 조회 4,414
09-11-23 조회 3,639
09-11-20 조회 4,880
09-08-30 조회 3,071
09-08-30 조회 3,067
09-08-12 조회 3,038
09-07-22 조회 4,093
09-07-22 조회 4,752
09-07-15 조회 5,910
09-07-11 조회 2,991
09-06-17 조회 2,725
09-06-15 조회 3,017
09-05-29 조회 3,299
09-05-11 조회 5,936
09-05-08 조회 2,693
09-04-30 조회 3,806
09-04-17 조회 3,130
09-04-04 조회 3,311
09-03-05 조회 3,693
09-02-09 조회 3,668
09-02-03 조회 6,167
08-12-27 조회 3,226
08-12-20 조회 4,311
08-11-21 조회 5,273
08-11-14 조회 2,848
08-11-03 조회 3,304