v

[ActionScript 3.0] HashMap

· 2011-12-08 (목) 14:36:31 · 3457
 
 - 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-10 조회 3,972
12-04-06 조회 5,395
12-04-06 조회 3,173
12-04-04 조회 4,275
12-04-02 조회 3,567
12-03-30 조회 3,520
12-03-29 조회 6,065
12-03-28 조회 3,392
12-03-28 조회 4,315
12-03-27 조회 4,607
12-03-23 조회 3,796
12-03-21 조회 3,516
12-03-20 조회 3,319
12-03-18 조회 3,175
12-03-15 조회 3,353
12-03-12 조회 3,421
12-03-12 조회 3,347
12-03-08 조회 3,682
12-03-03 조회 3,770
12-03-03 조회 3,311
12-02-29 조회 3,605
12-02-28 조회 3,592
12-02-28 조회 3,407
12-02-28 조회 3,540
12-02-27 조회 3,980
12-02-27 조회 3,427
12-02-24 조회 3,519
12-02-24 조회 3,427
12-02-23 조회 3,726
12-02-20 조회 3,736
12-02-20 조회 4,121
12-02-17 조회 4,091
12-02-16 조회 3,276
12-02-15 조회 3,565
12-02-15 조회 3,624
12-02-14 조회 3,592
12-02-08 조회 3,557
12-02-06 조회 3,521
12-02-06 조회 3,932
12-01-25 조회 4,176
12-01-21 조회 4,083
12-01-20 조회 6,192
12-01-15 조회 3,671
12-01-15 조회 3,627
12-01-14 조회 3,521
12-01-14 조회 3,671
12-01-14 조회 3,393
12-01-12 조회 4,178
12-01-12 조회 4,306
12-01-10 조회 3,603
12-01-10 조회 4,445
12-01-09 조회 3,778
12-01-08 조회 7,028
12-01-08 조회 3,248
12-01-07 조회 3,599
12-01-06 조회 3,613
12-01-04 조회 3,763
12-01-03 조회 3,602
12-01-02 조회 4,005
12-01-02 조회 3,188
12-01-01 조회 4,130
12-01-01 조회 3,472
11-12-31 조회 3,735
11-12-31 조회 3,848
11-12-31 조회 3,666
11-12-30 조회 3,910
11-12-30 조회 5,088
11-12-30 조회 4,194
11-12-30 조회 4,352
11-12-30 조회 3,813
11-12-30 조회 3,542
11-12-30 조회 3,548
11-12-29 조회 3,282
11-12-22 조회 3,525
11-12-21 조회 3,467
11-12-20 조회 3,713
11-12-15 조회 5,494
11-12-15 조회 4,492
11-12-14 조회 5,546
11-12-12 조회 3,706
11-12-03 조회 3,483
11-11-26 조회 3,657
11-11-19 조회 3,787
11-11-01 조회 3,144
11-10-23 조회 2,765
11-10-21 조회 2,814
11-10-15 조회 3,199
11-10-15 조회 2,935
11-10-15 조회 3,617
11-10-15 조회 2,849
11-10-14 조회 2,879
11-10-13 조회 2,649
11-10-11 조회 2,888
11-10-10 조회 2,743
11-09-30 조회 3,578
11-09-30 조회 3,022
11-09-29 조회 4,854
11-09-29 조회 2,677
11-09-23 조회 2,751
11-09-16 조회 3,088