v

[ActionScript 3.0] HashMap

· 2011-12-08 (목) 14:36:31 · 3183
 
 - 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,454
12-08-21 조회 3,444
12-08-21 조회 3,531
12-08-05 조회 3,349
12-08-04 조회 4,322
12-07-31 조회 9,203
12-07-26 조회 3,860
12-07-24 조회 3,490
12-07-20 조회 3,420
12-07-15 조회 3,296
12-07-09 조회 4,662
12-07-09 조회 3,373
12-07-09 조회 3,284
12-07-06 조회 4,102
12-07-05 조회 3,202
12-07-05 조회 3,276
12-06-30 조회 3,405
12-06-29 조회 3,201
12-06-26 조회 3,401
12-06-18 조회 3,276
12-06-18 조회 3,380
12-05-31 조회 3,594
12-05-29 조회 4,349
12-05-23 조회 3,385
12-05-22 조회 3,636
12-05-17 조회 4,446
12-05-10 조회 3,467
12-05-06 조회 3,342
12-05-04 조회 3,025
12-04-25 조회 3,475
12-04-21 조회 2,990
12-04-21 조회 4,395
12-04-20 조회 3,537
12-04-10 조회 2,954
12-04-06 조회 2,832
12-04-05 조회 3,174
12-04-05 조회 3,845
12-03-31 조회 2,965
12-03-30 조회 3,060
12-03-29 조회 2,977
12-03-28 조회 2,995
12-03-27 조회 3,311
12-03-26 조회 3,706
12-03-23 조회 2,894
12-03-20 조회 2,955
12-03-12 조회 2,882
12-03-12 조회 3,228
12-03-12 조회 3,140
12-03-12 조회 4,093
12-03-12 조회 5,606
12-03-12 조회 3,684
12-03-12 조회 5,492
12-03-12 조회 3,694
12-03-12 조회 6,050
12-03-06 조회 3,011
12-03-04 조회 4,144
12-03-02 조회 4,007
12-02-16 조회 5,129
12-02-15 조회 4,470
12-02-11 조회 2,973
12-02-08 조회 2,823
12-02-08 조회 3,842
12-02-07 조회 3,242
12-01-30 조회 2,936
12-01-29 조회 3,360
12-01-25 조회 3,351
12-01-19 조회 2,883
12-01-12 조회 3,033
11-12-30 조회 3,664
11-12-29 조회 3,022
11-12-26 조회 3,050
11-12-26 조회 2,939
11-12-17 조회 4,071
11-12-17 조회 3,267
11-12-01 조회 3,043
11-11-19 조회 4,221
11-11-14 조회 4,102
11-11-14 조회 2,726
11-11-10 조회 2,961
11-11-10 조회 3,625
11-10-27 조회 2,467
11-10-26 조회 2,583
11-10-25 조회 2,460
11-10-17 조회 3,618
11-10-10 조회 2,535
11-10-03 조회 2,588
11-09-28 조회 2,471
11-09-28 조회 3,968
11-09-26 조회 2,451
11-09-26 조회 2,568
11-09-25 조회 2,854
11-09-19 조회 6,015
11-09-09 조회 2,931
11-09-08 조회 3,300
11-08-31 조회 3,101
11-08-29 조회 5,088
11-08-25 조회 2,767
11-08-23 조회 2,447
11-08-21 조회 2,457
11-08-19 조회 2,381