v

[ActionScript 3.0] HashMap

· 2011-12-08 (목) 14:36:31 · 3225
 
 - 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-03-19 조회 3,377
12-03-16 조회 4,019
12-03-14 조회 3,885
12-03-13 조회 3,512
12-03-13 조회 4,350
12-03-12 조회 5,427
12-03-12 조회 1만
12-03-08 조회 5,967
12-03-06 조회 1.1만
12-03-06 조회 3,689
12-03-06 조회 6,227
12-03-02 조회 4,996
12-02-29 조회 3,559
12-02-27 조회 3,971
12-02-22 조회 3,625
12-02-20 조회 3,565
12-02-20 조회 6,699
12-02-01 조회 3,219
12-01-30 조회 8,741
12-01-29 조회 3,233
12-01-24 조회 3,401
12-01-21 조회 3,193
12-01-10 조회 3,664
11-12-26 조회 3,554
11-12-23 조회 4,978
11-12-23 조회 4,581
11-12-23 조회 4,155
11-12-23 조회 7,953
11-12-22 조회 3,685
11-12-17 조회 3,620
11-12-12 조회 3,025
11-12-08 조회 7,568
11-12-08 조회 3,439
11-12-07 조회 3,764
11-12-06 조회 5,440
11-12-02 조회 3,397
11-12-01 조회 3,103
11-11-30 조회 6,899
11-11-26 조회 3,823
11-11-24 조회 4,626
11-11-22 조회 2,987
11-11-22 조회 6,617
11-11-18 조회 4,966
11-11-16 조회 3,417
11-11-14 조회 3,436
11-11-12 조회 3,934
11-11-08 조회 3,254
11-11-06 조회 1만
11-11-06 조회 3,829
11-11-03 조회 5,518
11-11-02 조회 5,783
11-11-01 조회 9,306
11-10-19 조회 2,792
11-10-17 조회 2,593
11-10-10 조회 2,540
11-10-07 조회 3,330
11-10-06 조회 3,923
11-10-06 조회 2,474
11-10-02 조회 3,224
11-09-08 조회 4,230
11-09-08 조회 2,572
11-09-07 조회 2,602
11-09-02 조회 2,686
11-09-01 조회 3,614
11-08-28 조회 3,511
11-08-23 조회 3,357
11-08-22 조회 2,774
11-08-22 조회 2,370
11-08-13 조회 2,415
11-08-05 조회 2,869
11-08-03 조회 4,080
11-07-03 조회 2,500
11-06-15 조회 3,196
11-06-15 조회 3,038
11-05-15 조회 2,833
11-05-11 조회 5,570
11-04-12 조회 2,965
11-01-27 조회 3,809
11-01-26 조회 2,603
11-01-26 조회 4,453
11-01-18 조회 2,653
10-11-23 조회 2,761
10-10-24 조회 4,720
10-09-15 조회 4,406
10-09-07 조회 3,374
10-08-27 조회 4,063
10-08-20 조회 6,957
10-07-30 조회 5,633
10-06-30 조회 2,856
10-06-11 조회 4,616
10-06-11 조회 3,313
10-05-15 조회 4,143
10-05-10 조회 2,834
10-03-19 조회 5,400
10-03-05 조회 5,164
10-02-17 조회 5,566
09-11-21 조회 6,912
09-11-09 조회 3,986
09-11-04 조회 6,699
09-10-26 조회 3,843