v

[ActionScript 3.0] HashMap

· 2011-12-08 (목) 14:36:31 · 3576
 
 - 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-08-25 조회 3,696
11-08-25 조회 2,895
11-08-25 조회 2,585
11-08-24 조회 2,485
11-08-24 조회 3,116
11-08-24 조회 4,437
11-08-23 조회 2,506
11-08-23 조회 4,346
11-08-23 조회 3,489
11-08-23 조회 2,567
11-08-23 조회 2,801
11-08-23 조회 2,717
11-08-23 조회 2,848
11-08-23 조회 2,304
11-08-23 조회 2,444
11-08-23 조회 2,614
11-08-23 조회 3,014
11-08-23 조회 2,605
11-08-22 조회 3,046
11-08-22 조회 2,758
11-08-22 조회 2,902
11-08-22 조회 2,497
11-08-22 조회 2,766
11-08-21 조회 2,706
11-08-21 조회 2,638
11-08-20 조회 2,534
11-08-20 조회 2,901
11-08-20 조회 3,328
11-08-19 조회 2,739
11-08-19 조회 2,522
11-08-19 조회 5,055
11-08-19 조회 2,671
11-08-19 조회 2,523
11-08-19 조회 3,793
11-08-19 조회 3,234
11-08-18 조회 3,725
11-08-18 조회 2,256
11-08-18 조회 2,487
11-08-18 조회 2,315
11-08-18 조회 2,164
11-08-18 조회 2,285
11-08-18 조회 2,416
11-08-17 조회 3,217
11-08-17 조회 2,605
11-08-17 조회 2,748
11-08-17 조회 2,294
11-08-17 조회 2,711
11-08-17 조회 3,423
11-08-17 조회 2,829
11-08-16 조회 3,071
11-08-16 조회 2,574
11-08-16 조회 2,670
11-08-16 조회 2,525
11-08-16 조회 2,858
11-08-16 조회 2,658
11-08-16 조회 2,071
11-08-16 조회 2,232
11-08-16 조회 2,283
11-08-16 조회 2,646
11-08-16 조회 2,543
11-08-16 조회 2,546
11-08-15 조회 2,497
11-08-15 조회 3,022
11-08-13 조회 2,648
11-08-13 조회 2,688
11-08-13 조회 2,578
11-08-12 조회 2,362
11-08-12 조회 3,504
11-08-12 조회 3,288
11-08-12 조회 2,733
11-08-12 조회 2,536
11-08-11 조회 3,710
11-08-11 조회 3,004
11-08-11 조회 2,941
11-08-11 조회 2,470
11-08-10 조회 3,240
11-08-10 조회 5,457
11-08-10 조회 4,153
11-08-09 조회 2,731
11-08-09 조회 2,333
11-08-09 조회 2,412
11-08-09 조회 3,035
11-08-09 조회 2,687
11-08-09 조회 2,977
11-08-08 조회 3,072
11-08-08 조회 3,116
11-08-08 조회 2,210
11-08-08 조회 2,489
11-08-08 조회 4,406
11-08-08 조회 2,429
11-08-08 조회 2,958
11-08-08 조회 2,507
11-08-08 조회 2,310
11-08-08 조회 3,084
11-08-08 조회 2,343
11-08-07 조회 3,153
11-08-07 조회 2,705
11-08-06 조회 3,319
11-08-06 조회 3,323
11-08-06 조회 2,885