v

[ActionScript 3.0] HashMap

· 2011-12-08 (목) 14:36:31 · 3533
 
 - 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건
+
제목 글쓴이 날짜 조회
11-09-19 조회 2,579
11-09-19 조회 2,499
11-09-19 조회 5,516
11-09-19 조회 6,159
11-09-16 조회 2,873
11-09-16 조회 3,135
11-09-16 조회 6,335
11-09-15 조회 3,099
11-09-15 조회 2,861
11-09-15 조회 2,529
11-09-15 조회 2,531
11-09-15 조회 2,724
11-09-15 조회 2,695
11-09-15 조회 2,811
11-09-14 조회 2,719
11-09-14 조회 3,280
11-09-14 조회 2,389
11-09-14 조회 2,320
11-09-14 조회 2,304
11-09-13 조회 2,484
11-09-10 조회 2,958
11-09-10 조회 2,579
11-09-09 조회 3,159
11-09-09 조회 3,020
11-09-09 조회 3,099
11-09-09 조회 3,143
11-09-08 조회 3,828
11-09-08 조회 5,028
11-09-08 조회 3,415
11-09-08 조회 4,350
11-09-08 조회 3,861
11-09-08 조회 3,655
11-09-08 조회 2,757
11-09-08 조회 2,326
11-09-08 조회 2,736
11-09-08 조회 2,847
11-09-07 조회 3,515
11-09-07 조회 3,580
11-09-07 조회 3,000
11-09-07 조회 2,696
11-09-07 조회 2,801
11-09-07 조회 2,821
11-09-07 조회 3,047
11-09-07 조회 2,321
11-09-07 조회 2,418
11-09-07 조회 2,574
11-09-07 조회 3,517
11-09-07 조회 2,789
11-09-06 조회 2,623
11-09-06 조회 2,903
11-09-06 조회 2,469
11-09-05 조회 2,682
11-09-05 조회 2,625
11-09-05 조회 2,905
11-09-05 조회 2,419
11-09-04 조회 2,394
11-09-04 조회 2,346
11-09-03 조회 2,528
11-09-03 조회 2,683
11-09-02 조회 3,415
11-09-02 조회 2,790
11-09-02 조회 2,652
11-09-02 조회 3,569
11-09-02 조회 2,601
11-09-02 조회 2,482
11-09-02 조회 2,484
11-09-01 조회 2,682
11-09-01 조회 2,828
11-09-01 조회 2,706
11-09-01 조회 2,714
11-09-01 조회 2,855
11-09-01 조회 2,704
11-09-01 조회 4,147
11-09-01 조회 3,753
11-09-01 조회 4,416
11-08-31 조회 2,298
11-08-31 조회 2,599
11-08-31 조회 2,893
11-08-31 조회 2,612
11-08-31 조회 2,457
11-08-31 조회 3,260
11-08-30 조회 3,003
11-08-30 조회 4,232
11-08-29 조회 2,997
11-08-29 조회 2,577
11-08-29 조회 5,195
11-08-29 조회 3,337
11-08-29 조회 2,744
11-08-29 조회 3,094
11-08-28 조회 2,494
11-08-28 조회 3,641
11-08-28 조회 2,594
11-08-28 조회 2,958
11-08-26 조회 2,496
11-08-26 조회 3,007
11-08-26 조회 3,240
11-08-26 조회 2,734
11-08-25 조회 2,564
11-08-25 조회 2,885
11-08-25 조회 2,610