v

[ActionScript 3.0] HashMap

· 2011-12-08 (목) 14:36:31 · 3169
 
 - 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-08-29 조회 5,448
12-08-21 조회 3,443
12-08-21 조회 3,528
12-08-05 조회 3,341
12-08-04 조회 4,308
12-07-31 조회 9,196
12-07-26 조회 3,857
12-07-24 조회 3,490
12-07-20 조회 3,398
12-07-15 조회 3,283
12-07-09 조회 4,660
12-07-09 조회 3,369
12-07-09 조회 3,275
12-07-06 조회 4,099
12-07-05 조회 3,198
12-07-05 조회 3,272
12-06-30 조회 3,401
12-06-29 조회 3,198
12-06-26 조회 3,385
12-06-18 조회 3,263
12-06-18 조회 3,362
12-05-31 조회 3,590
12-05-29 조회 4,339
12-05-23 조회 3,371
12-05-22 조회 3,616
12-05-17 조회 4,440
12-05-10 조회 3,465
12-05-06 조회 3,333
12-05-04 조회 3,021
12-04-25 조회 3,461
12-04-21 조회 2,982
12-04-21 조회 4,376
12-04-20 조회 3,536
12-04-10 조회 2,952
12-04-06 조회 2,825
12-04-05 조회 3,170
12-04-05 조회 3,842
12-03-31 조회 2,947
12-03-30 조회 3,056
12-03-29 조회 2,975
12-03-28 조회 2,993
12-03-27 조회 3,308
12-03-26 조회 3,703
12-03-23 조회 2,892
12-03-20 조회 2,953
12-03-12 조회 2,880
12-03-12 조회 3,225
12-03-12 조회 3,138
12-03-12 조회 4,090
12-03-12 조회 5,589
12-03-12 조회 3,681
12-03-12 조회 5,487
12-03-12 조회 3,679
12-03-12 조회 6,040
12-03-06 조회 3,009
12-03-04 조회 4,140
12-03-02 조회 3,982
12-02-16 조회 5,124
12-02-15 조회 4,458
12-02-11 조회 2,969
12-02-08 조회 2,819
12-02-08 조회 3,834
12-02-07 조회 3,235
12-01-30 조회 2,932
12-01-29 조회 3,358
12-01-25 조회 3,349
12-01-19 조회 2,872
12-01-12 조회 3,019
11-12-30 조회 3,661
11-12-29 조회 3,013
11-12-26 조회 3,047
11-12-26 조회 2,935
11-12-17 조회 4,062
11-12-17 조회 3,263
11-12-01 조회 3,040
11-11-19 조회 4,214
11-11-14 조회 4,099
11-11-14 조회 2,722
11-11-10 조회 2,956
11-11-10 조회 3,624
11-10-27 조회 2,452
11-10-26 조회 2,582
11-10-25 조회 2,447
11-10-17 조회 3,606
11-10-10 조회 2,531
11-10-03 조회 2,586
11-09-28 조회 2,465
11-09-28 조회 3,966
11-09-26 조회 2,449
11-09-26 조회 2,566
11-09-25 조회 2,836
11-09-19 조회 6,014
11-09-09 조회 2,920
11-09-08 조회 3,296
11-08-31 조회 3,096
11-08-29 조회 5,087
11-08-25 조회 2,766
11-08-23 조회 2,444
11-08-21 조회 2,455
11-08-19 조회 2,376