v

[ActionScript 3.0] HashMap

· 2011-12-08 (목) 14:36:31 · 3302
 
 - 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-12-23 조회 3,928
11-12-23 조회 2,768
11-12-23 조회 4,676
11-12-23 조회 4,173
11-12-23 조회 2,651
11-12-23 조회 7,970
11-12-22 조회 2,957
11-12-22 조회 3,694
11-12-22 조회 3,421
11-12-22 조회 3,209
11-12-22 조회 3,489
11-12-21 조회 3,360
11-12-21 조회 3,150
11-12-21 조회 3,476
11-12-21 조회 3,873
11-12-21 조회 2,909
11-12-21 조회 3,373
11-12-21 조회 4,345
11-12-21 조회 4,164
11-12-21 조회 3,170
11-12-20 조회 3,623
11-12-19 조회 6,139
11-12-19 조회 2,810
11-12-19 조회 4,896
11-12-17 조회 3,634
11-12-17 조회 4,105
11-12-17 조회 3,314
11-12-16 조회 3,040
11-12-16 조회 3,130
11-12-15 조회 3,242
11-12-15 조회 3,320
11-12-15 조회 4,148
11-12-15 조회 5,414
11-12-15 조회 4,414
11-12-15 조회 3,090
11-12-15 조회 3,866
11-12-15 조회 4,020
11-12-14 조회 2,780
11-12-14 조회 4,175
11-12-14 조회 5,437
11-12-13 조회 3,992
11-12-13 조회 4,910
11-12-13 조회 5,950
11-12-13 조회 6,234
11-12-13 조회 4,632
11-12-12 조회 3,047
11-12-12 조회 3,404
11-12-12 조회 6,107
11-12-12 조회 4,478
11-12-12 조회 3,633
11-12-12 조회 6,017
11-12-11 조회 4,570
11-12-10 조회 2,689
11-12-10 조회 4,642
11-12-09 조회 2,745
11-12-09 조회 3,097
11-12-09 조회 4,039
11-12-09 조회 3,668
11-12-09 조회 3,877
11-12-09 조회 3,399
11-12-09 조회 4,395
11-12-09 조회 3,072
11-12-09 조회 3,124
11-12-09 조회 2,864
11-12-09 조회 3,658
11-12-08 조회 3,398
11-12-08 조회 3,303
11-12-08 조회 7,598
11-12-08 조회 4,045
11-12-08 조회 3,464
11-12-08 조회 2,819
11-12-08 조회 1.2만
11-12-08 조회 3,897
11-12-07 조회 2,762
11-12-07 조회 2,795
11-12-07 조회 3,777
11-12-07 조회 4,370
11-12-07 조회 3,830
11-12-07 조회 2,996
11-12-07 조회 4,326
11-12-06 조회 4,230
11-12-06 조회 5,461
11-12-06 조회 3,590
11-12-06 조회 3,546
11-12-05 조회 2,979
11-12-05 조회 3,224
11-12-05 조회 4,468
11-12-05 조회 3,225
11-12-05 조회 4,837
11-12-05 조회 2,821
11-12-05 조회 5,848
11-12-05 조회 2,964
11-12-05 조회 4,340
11-12-04 조회 5,658
11-12-04 조회 2,901
11-12-04 조회 3,170
11-12-03 조회 3,544
11-12-03 조회 2,929
11-12-03 조회 3,496
11-12-03 조회 3,398