v

[ActionScript 3.0] HashMap

· 2011-12-08 (목) 14:36:31 · 3605
 
 - 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,187건
+
제목 글쓴이 날짜 조회
12-03-13 조회 3,051
12-03-12 조회 5,644
12-03-12 조회 3,128
12-03-12 조회 3,777
12-03-12 조회 3,524
12-03-12 조회 3,086
12-03-12 조회 1만
12-03-12 조회 3,446
12-03-12 조회 4,085
12-03-12 조회 3,075
12-03-12 조회 3,442
12-03-12 조회 3,332
12-03-12 조회 4,281
12-03-12 조회 5,890
12-03-12 조회 3,860
12-03-12 조회 5,673
12-03-12 조회 3,887
12-03-12 조회 6,294
12-03-11 조회 3,785
12-03-11 조회 3,227
12-03-11 조회 4,160
12-03-11 조회 4,973
12-03-11 조회 3,873
12-03-11 조회 3,844
12-03-11 조회 5,506
12-03-10 조회 3,518
12-03-10 조회 3,104
12-03-10 조회 4,008
12-03-10 조회 3,137
12-03-09 조회 7,478
12-03-09 조회 3,229
12-03-09 조회 3,630
12-03-09 조회 3,217
12-03-09 조회 3,233
12-03-09 조회 3,430
12-03-09 조회 4,213
12-03-08 조회 3,906
12-03-08 조회 3,785
12-03-08 조회 4,071
12-03-08 조회 6,190
12-03-08 조회 3,097
12-03-08 조회 4,189
12-03-08 조회 4,083
12-03-07 조회 3,004
12-03-07 조회 3,160
12-03-07 조회 2,849
12-03-06 조회 1.1만
12-03-06 조회 3,301
12-03-06 조회 4,363
12-03-06 조회 3,907
12-03-06 조회 3,246
12-03-06 조회 2,986
12-03-06 조회 6,405
12-03-06 조회 3,076
12-03-05 조회 3,161
12-03-04 조회 4,393
12-03-03 조회 3,769
12-03-03 조회 3,894
12-03-03 조회 3,444
12-03-03 조회 3,424
12-03-02 조회 3,249
12-03-02 조회 3,531
12-03-02 조회 3,368
12-03-02 조회 4,324
12-03-02 조회 3,484
12-03-02 조회 5,209
12-03-02 조회 3,092
12-03-02 조회 4,740
12-03-01 조회 5,244
12-03-01 조회 6,592
12-03-01 조회 3,397
12-02-29 조회 3,615
12-02-29 조회 4,097
12-02-29 조회 3,720
12-02-29 조회 3,753
12-02-29 조회 5,535
12-02-28 조회 3,689
12-02-28 조회 3,515
12-02-28 조회 3,684
12-02-28 조회 3,664
12-02-28 조회 3,288
12-02-27 조회 4,534
12-02-27 조회 4,382
12-02-27 조회 4,136
12-02-27 조회 3,576
12-02-27 조회 3,391
12-02-27 조회 3,112
12-02-27 조회 4,284
12-02-27 조회 5,191
12-02-27 조회 3,939
12-02-26 조회 4,190
12-02-26 조회 3,626
12-02-26 조회 3,662
12-02-26 조회 3,810
12-02-26 조회 3,517
12-02-26 조회 3,431
12-02-26 조회 3,375
12-02-26 조회 3,728
12-02-26 조회 3,844
12-02-26 조회 3,471