v

[ActionScript 3.0] HashMap

· 2011-12-08 (목) 14:36:31 · 3581
 
 - 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-02-26 조회 3,177
12-02-26 조회 3,495
12-02-26 조회 3,340
12-02-26 조회 3,419
12-02-26 조회 3,465
12-02-26 조회 3,317
12-02-26 조회 3,641
12-02-26 조회 3,473
12-02-26 조회 3,558
12-02-26 조회 4,040
12-02-26 조회 2,990
12-02-26 조회 5,183
12-02-25 조회 2,748
12-02-25 조회 3,226
12-02-24 조회 2,931
12-02-24 조회 3,312
12-02-24 조회 3,290
12-02-24 조회 3,605
12-02-24 조회 3,135
12-02-24 조회 3,943
12-02-24 조회 3,911
12-02-24 조회 3,509
12-02-24 조회 9,369
12-02-23 조회 2,875
12-02-23 조회 3,322
12-02-23 조회 3,222
12-02-23 조회 3,161
12-02-23 조회 3,806
12-02-23 조회 2,892
12-02-23 조회 3,373
12-02-23 조회 3,051
12-02-23 조회 2,808
12-02-22 조회 3,707
12-02-22 조회 5,471
12-02-22 조회 3,024
12-02-22 조회 4,420
12-02-22 조회 3,293
12-02-22 조회 3,117
12-02-22 조회 4,032
12-02-22 조회 3,916
12-02-22 조회 2,844
12-02-22 조회 3,884
12-02-22 조회 4,344
12-02-22 조회 3,767
12-02-22 조회 2,948
12-02-22 조회 4,598
12-02-21 조회 3,160
12-02-21 조회 2,938
12-02-21 조회 2,844
12-02-21 조회 3,752
12-02-21 조회 3,992
12-02-21 조회 3,416
12-02-21 조회 2,929
12-02-20 조회 3,172
12-02-20 조회 3,805
12-02-20 조회 3,755
12-02-20 조회 3,032
12-02-20 조회 5,819
12-02-20 조회 3,615
12-02-20 조회 6,828
12-02-20 조회 3,227
12-02-20 조회 4,204
12-02-19 조회 3,387
12-02-18 조회 3,120
12-02-18 조회 3,863
12-02-17 조회 2,997
12-02-17 조회 4,187
12-02-17 조회 3,069
12-02-17 조회 3,631
12-02-17 조회 3,404
12-02-17 조회 6,269
12-02-17 조회 4,465
12-02-17 조회 3,093
12-02-17 조회 3,118
12-02-17 조회 3,481
12-02-16 조회 3,308
12-02-16 조회 3,380
12-02-16 조회 4,744
12-02-16 조회 2,992
12-02-16 조회 5,381
12-02-16 조회 5,127
12-02-15 조회 3,673
12-02-15 조회 3,291
12-02-15 조회 4,833
12-02-15 조회 3,324
12-02-15 조회 3,717
12-02-15 조회 2,795
12-02-15 조회 4,066
12-02-14 조회 3,582
12-02-14 조회 3,687
12-02-14 조회 4,096
12-02-14 조회 3,135
12-02-14 조회 6,219
12-02-14 조회 3,169
12-02-14 조회 4,191
12-02-13 조회 3,468
12-02-13 조회 3,087
12-02-13 조회 7,231
12-02-12 조회 2,908
12-02-12 조회 5,286