v

[ActionScript 3.0] HashMap

· 2011-12-08 (목) 14:36:31 · 3589
 
 - 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건
+
제목 글쓴이 날짜 조회
11-11-16 조회 3,544
11-11-15 조회 2,660
11-11-15 조회 3,297
11-11-15 조회 2,747
11-11-14 조회 2,845
11-11-14 조회 3,791
11-11-14 조회 3,591
11-11-14 조회 4,280
11-11-14 조회 2,874
11-11-14 조회 2,777
11-11-13 조회 4,066
11-11-12 조회 2,702
11-11-12 조회 2,833
11-11-12 조회 4,059
11-11-11 조회 2,962
11-11-11 조회 2,909
11-11-11 조회 2,578
11-11-11 조회 2,902
11-11-11 조회 4,043
11-11-11 조회 2,509
11-11-11 조회 4,752
11-11-11 조회 2,779
11-11-11 조회 2,593
11-11-11 조회 2,461
11-11-11 조회 2,879
11-11-11 조회 2,654
11-11-11 조회 2,651
11-11-10 조회 3,105
11-11-10 조회 2,565
11-11-10 조회 3,195
11-11-10 조회 3,761
11-11-10 조회 2,455
11-11-10 조회 2,583
11-11-10 조회 2,416
11-11-09 조회 2,622
11-11-09 조회 3,344
11-11-09 조회 2,823
11-11-09 조회 2,961
11-11-08 조회 2,718
11-11-08 조회 3,049
11-11-08 조회 2,378
11-11-08 조회 3,335
11-11-08 조회 2,625
11-11-08 조회 3,402
11-11-06 조회 1만
11-11-06 조회 2,693
11-11-06 조회 3,694
11-11-06 조회 3,498
11-11-06 조회 3,803
11-11-06 조회 3,985
11-11-05 조회 2,528
11-11-05 조회 2,692
11-11-04 조회 8,591
11-11-04 조회 2,640
11-11-04 조회 2,696
11-11-04 조회 3,707
11-11-04 조회 2,594
11-11-04 조회 2,773
11-11-03 조회 4,038
11-11-03 조회 2,713
11-11-03 조회 5,678
11-11-02 조회 5,952
11-11-01 조회 3,237
11-11-01 조회 2,682
11-11-01 조회 3,304
11-11-01 조회 2,588
11-11-01 조회 2,928
11-11-01 조회 2,438
11-11-01 조회 9,460
11-11-01 조회 3,007
11-10-31 조회 2,390
11-10-27 조회 2,653
11-10-26 조회 3,236
11-10-26 조회 2,735
11-10-26 조회 2,586
11-10-26 조회 2,614
11-10-26 조회 2,497
11-10-25 조회 2,563
11-10-25 조회 2,670
11-10-25 조회 2,607
11-10-25 조회 2,581
11-10-24 조회 2,615
11-10-24 조회 2,900
11-10-24 조회 2,914
11-10-23 조회 3,286
11-10-23 조회 2,874
11-10-23 조회 2,769
11-10-21 조회 2,615
11-10-21 조회 2,541
11-10-21 조회 2,526
11-10-21 조회 2,925
11-10-20 조회 4,084
11-10-19 조회 2,584
11-10-19 조회 2,997
11-10-19 조회 4,111
11-10-19 조회 3,265
11-10-19 조회 2,974
11-10-19 조회 6,796
11-10-18 조회 4,130
11-10-18 조회 2,676