v

[ActionScript 3.0] HashMap

· 2011-12-08 (목) 14:36:31 · 3503
 
 - 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-03-19 조회 3,599
12-03-16 조회 4,136
12-03-14 조회 4,018
12-03-13 조회 3,628
12-03-13 조회 4,481
12-03-12 조회 5,576
12-03-12 조회 1만
12-03-08 조회 6,124
12-03-06 조회 1.1만
12-03-06 조회 3,841
12-03-06 조회 6,346
12-03-02 조회 5,150
12-02-29 조회 3,682
12-02-27 조회 4,200
12-02-22 조회 3,733
12-02-20 조회 3,717
12-02-20 조회 6,795
12-02-01 조회 3,329
12-01-30 조회 8,863
12-01-29 조회 3,347
12-01-24 조회 3,683
12-01-21 조회 3,331
12-01-10 조회 3,794
11-12-26 조회 3,708
11-12-23 조회 5,131
11-12-23 조회 4,780
11-12-23 조회 4,382
11-12-23 조회 8,086
11-12-22 조회 3,826
11-12-17 조회 3,868
11-12-12 조회 3,281
11-12-08 조회 7,807
11-12-08 조회 3,632
11-12-07 조회 3,888
11-12-06 조회 5,651
11-12-02 조회 3,596
11-12-01 조회 3,219
11-11-30 조회 7,004
11-11-26 조회 3,970
11-11-24 조회 4,714
11-11-22 조회 3,097
11-11-22 조회 6,719
11-11-18 조회 5,152
11-11-16 조회 3,512
11-11-14 조회 3,545
11-11-12 조회 4,020
11-11-08 조회 3,346
11-11-06 조회 1만
11-11-06 조회 3,934
11-11-03 조회 5,623
11-11-02 조회 5,904
11-11-01 조회 9,410
11-10-19 조회 2,921
11-10-17 조회 2,700
11-10-10 조회 2,682
11-10-07 조회 3,438
11-10-06 조회 4,026
11-10-06 조회 2,584
11-10-02 조회 3,334
11-09-08 조회 4,346
11-09-08 조회 2,730
11-09-07 조회 2,696
11-09-02 조회 2,789
11-09-01 조회 3,741
11-08-28 조회 3,637
11-08-23 조회 3,451
11-08-22 조회 2,868
11-08-22 조회 2,469
11-08-13 조회 2,524
11-08-05 조회 2,967
11-08-03 조회 4,162
11-07-03 조회 2,648
11-06-15 조회 3,283
11-06-15 조회 3,119
11-05-15 조회 2,919
11-05-11 조회 5,652
11-04-12 조회 3,080
11-01-27 조회 3,897
11-01-26 조회 2,694
11-01-26 조회 4,524
11-01-18 조회 2,741
10-11-23 조회 2,968
10-10-24 조회 4,785
10-09-15 조회 4,479
10-09-07 조회 3,469
10-08-27 조회 4,146
10-08-20 조회 7,030
10-07-30 조회 5,716
10-06-30 조회 2,974
10-06-11 조회 4,732
10-06-11 조회 3,420
10-05-15 조회 4,265
10-05-10 조회 2,947
10-03-19 조회 5,493
10-03-05 조회 5,253
10-02-17 조회 5,695
09-11-21 조회 7,005
09-11-09 조회 4,065
09-11-04 조회 6,788
09-10-26 조회 3,922