v

[ActionScript 3.0] HashMap

· 2011-12-08 (목) 14:36:31 · 3563
 
 - 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건
+
제목 글쓴이 날짜 조회
12-01-21 조회 3,365
12-01-21 조회 4,157
12-01-21 조회 3,557
12-01-21 조회 3,230
12-01-20 조회 3,123
12-01-20 조회 2,763
12-01-20 조회 3,518
12-01-20 조회 3,351
12-01-20 조회 2,738
12-01-20 조회 3,181
12-01-20 조회 6,270
12-01-19 조회 3,299
12-01-19 조회 4,954
12-01-19 조회 3,229
12-01-19 조회 3,234
12-01-19 조회 3,193
12-01-19 조회 2,817
12-01-18 조회 3,756
12-01-18 조회 2,968
12-01-18 조회 2,712
12-01-18 조회 3,282
12-01-18 조회 2,936
12-01-18 조회 3,128
12-01-17 조회 2,921
12-01-17 조회 4,012
12-01-17 조회 4,445
12-01-17 조회 3,299
12-01-17 조회 3,414
12-01-17 조회 3,168
12-01-17 조회 5,239
12-01-17 조회 3,032
12-01-16 조회 2,770
12-01-16 조회 3,621
12-01-16 조회 4,112
12-01-15 조회 2,916
12-01-15 조회 3,735
12-01-15 조회 3,692
12-01-14 조회 3,581
12-01-14 조회 3,728
12-01-14 조회 3,459
12-01-14 조회 1.1만
12-01-14 조회 3,673
12-01-14 조회 3,992
12-01-14 조회 2,946
12-01-14 조회 2,791
12-01-13 조회 2,933
12-01-13 조회 3,518
12-01-13 조회 4,584
12-01-13 조회 2,928
12-01-12 조회 4,238
12-01-12 조회 4,376
12-01-12 조회 3,389
12-01-12 조회 3,212
12-01-11 조회 2,802
12-01-11 조회 3,248
12-01-11 조회 3,461
12-01-11 조회 3,180
12-01-11 조회 3,189
12-01-11 조회 3,295
12-01-11 조회 3,505
12-01-11 조회 4,286
12-01-11 조회 3,370
12-01-11 조회 2,828
12-01-11 조회 1.2만
12-01-11 조회 3,153
12-01-10 조회 2,965
12-01-10 조회 3,677
12-01-10 조회 4,744
12-01-10 조회 3,435
12-01-10 조회 3,149
12-01-10 조회 2,889
12-01-10 조회 3,799
12-01-10 조회 3,064
12-01-10 조회 4,579
12-01-10 조회 2,955
12-01-10 조회 2,998
12-01-10 조회 3,190
12-01-10 조회 3,823
12-01-10 조회 2,889
12-01-10 조회 4,521
12-01-10 조회 2,905
12-01-10 조회 5,377
12-01-10 조회 5,565
12-01-10 조회 5,442
12-01-10 조회 5,342
12-01-09 조회 2,862
12-01-09 조회 2,787
12-01-09 조회 3,320
12-01-09 조회 3,843
12-01-09 조회 2,795
12-01-09 조회 2,958
12-01-09 조회 2,758
12-01-08 조회 7,099
12-01-08 조회 2,775
12-01-08 조회 7,186
12-01-08 조회 3,709
12-01-08 조회 3,307
12-01-08 조회 2,710
12-01-08 조회 2,865
12-01-08 조회 3,502