v

[ActionScript 3.0] HashMap

· 2011-12-08 (목) 14:36:31 · 3206
 
 - 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-01-22 조회 3,493
12-01-21 조회 2,794
12-01-19 조회 4,811
12-01-18 조회 2,784
12-01-18 조회 2,787
12-01-18 조회 2,994
12-01-17 조회 2,762
12-01-17 조회 2,995
12-01-14 조회 1.1만
12-01-14 조회 3,551
12-01-14 조회 3,867
12-01-11 조회 3,213
12-01-11 조회 3,072
12-01-11 조회 3,036
12-01-11 조회 3,066
12-01-11 조회 3,407
12-01-11 조회 2,624
12-01-10 조회 3,018
12-01-10 조회 2,804
12-01-10 조회 2,805
12-01-09 조회 2,763
12-01-08 조회 2,807
12-01-07 조회 2,779
12-01-06 조회 2,639
12-01-06 조회 2,761
12-01-05 조회 2,762
12-01-02 조회 2,911
11-12-29 조회 2,784
11-12-28 조회 2,789
11-12-28 조회 2,767
11-12-27 조회 2,991
11-12-23 조회 2,711
11-12-23 조회 2,747
11-12-21 조회 3,864
11-12-21 조회 2,870
11-12-19 조회 2,801
11-12-14 조회 4,143
11-12-08 조회 3,207
11-12-08 조회 4,034
11-12-07 조회 4,322
11-12-06 조회 4,224
11-12-06 조회 3,548
11-12-05 조회 3,186
11-12-05 조회 4,813
11-12-04 조회 3,136
11-12-03 조회 3,513
11-12-02 조회 3,361
11-12-02 조회 3,448
11-12-02 조회 2,787
11-12-01 조회 2,749
11-12-01 조회 3,654
11-12-01 조회 2,739
11-11-29 조회 5,626
11-11-23 조회 3,562
11-11-23 조회 3,883
11-11-22 조회 3,880
11-11-22 조회 2,941
11-11-21 조회 3,487
11-11-21 조회 2,769
11-11-18 조회 3,866
11-11-18 조회 3,042
11-11-17 조회 2,934
11-11-17 조회 2,934
11-11-17 조회 4,502
11-11-11 조회 2,798
11-11-11 조회 2,799
11-11-11 조회 3,934
11-11-11 조회 4,636
11-11-08 조회 2,906
11-11-06 조회 3,351
11-11-06 조회 3,643
11-10-21 조회 2,452
11-10-21 조회 2,353
11-10-19 조회 2,405
11-10-19 조회 3,902
11-10-08 조회 2,459
11-10-06 조회 4,138
11-10-01 조회 2,454
11-09-30 조회 2,540
11-09-29 조회 2,541
11-09-26 조회 2,398
11-09-23 조회 2,396
11-09-23 조회 2,171
11-09-22 조회 2,758
11-09-09 조회 3,039
11-08-30 조회 3,926
11-08-23 조회 2,183
11-08-12 조회 2,357
11-08-04 조회 3,945
11-07-01 조회 2,916
11-06-09 조회 2,557
11-05-17 조회 2,631
11-05-07 조회 2,938
11-04-21 조회 2,605
11-04-12 조회 2,453
11-04-11 조회 3,566
11-02-11 조회 2,697
11-02-08 조회 2,123
11-01-11 조회 2,522
10-11-26 조회 3,882