v

[ActionScript 3.0] HashMap

· 2011-12-08 (목) 14:36:31 · 3472
 
 - 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-04-26 조회 3,830
12-04-24 조회 3,226
12-04-23 조회 4,558
12-04-22 조회 3,250
12-04-19 조회 3,334
12-04-18 조회 4,387
12-04-18 조회 3,098
12-04-17 조회 3,370
12-04-17 조회 5,130
12-04-16 조회 3,547
12-04-13 조회 3,089
12-04-09 조회 3,161
12-04-04 조회 3,932
12-04-01 조회 3,211
12-03-31 조회 3,602
12-03-30 조회 3,211
12-03-30 조회 3,873
12-03-26 조회 3,482
12-03-26 조회 3,064
12-03-22 조회 3,216
12-03-21 조회 3,072
12-03-19 조회 3,479
12-03-17 조회 3,510
12-03-14 조회 3,239
12-03-10 조회 3,922
12-03-09 조회 3,500
12-03-09 조회 3,327
12-03-08 조회 3,800
12-03-08 조회 3,971
12-03-08 조회 4,065
12-03-03 조회 3,313
12-03-02 조회 4,638
12-02-28 조회 3,174
12-02-27 조회 2,987
12-02-25 조회 3,130
12-02-24 조회 3,840
12-02-23 조회 3,107
12-02-22 조회 3,628
12-02-22 조회 5,397
12-02-22 조회 3,970
12-02-22 조회 4,290
12-02-18 조회 3,781
12-02-17 조회 3,001
12-02-17 조회 3,409
12-02-14 조회 3,484
12-02-13 조회 3,364
12-02-10 조회 1만
12-02-08 조회 3,023
12-02-07 조회 3,363
12-02-06 조회 3,088
12-02-05 조회 3,553
12-01-31 조회 3,481
12-01-30 조회 9,664
12-01-29 조회 3,821
12-01-26 조회 3,420
12-01-25 조회 4,033
12-01-24 조회 3,426
12-01-10 조회 3,738
12-01-10 조회 4,496
12-01-09 조회 3,272
12-01-08 조회 7,135
12-01-08 조회 3,657
12-01-07 조회 5,190
12-01-04 조회 3,888
12-01-02 조회 3,114
12-01-02 조회 3,168
12-01-02 조회 3,474
12-01-01 조회 3,106
11-12-31 조회 3,416
11-12-31 조회 3,094
11-12-30 조회 3,259
11-12-30 조회 3,412
11-12-29 조회 3,626
11-12-28 조회 3,718
11-12-26 조회 3,291
11-12-23 조회 3,507
11-12-22 조회 3,051
11-12-15 조회 4,141
11-12-08 조회 3,959
11-12-05 조회 3,298
11-12-04 조회 5,714
11-12-03 조회 3,574
11-12-02 조회 3,355
11-12-01 조회 5,430
11-11-30 조회 2,914
11-11-30 조회 2,814
11-11-28 조회 3,159
11-11-26 조회 3,338
11-11-25 조회 3,359
11-11-24 조회 2,955
11-11-22 조회 3,046
11-11-22 조회 2,887
11-11-17 조회 2,823
11-11-11 조회 2,821
11-11-09 조회 2,877
11-11-08 조회 3,254
11-11-08 조회 2,546
11-11-04 조회 3,627
11-11-01 조회 2,934
11-10-26 조회 2,520