v

[ActionScript 3.0] HashMap

· 2011-12-08 (목) 14:36:31 · 3550
 
 - 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건
+
제목 글쓴이 날짜 조회
13-03-03 조회 6,286
13-03-01 조회 5,616
13-03-01 조회 8,358
13-03-01 조회 6,361
13-02-26 조회 3,973
13-02-26 조회 3,927
13-02-26 조회 4,799
13-02-24 조회 8,201
13-02-24 조회 4,305
13-02-24 조회 3,534
13-02-24 조회 5,348
13-02-24 조회 5,339
13-02-24 조회 4,401
13-02-24 조회 5,736
13-02-24 조회 9,434
13-02-14 조회 5,180
13-02-13 조회 9,113
13-01-29 조회 7,725
13-01-24 조회 5,074
13-01-24 조회 4,951
13-01-17 조회 4,374
13-01-16 조회 4,410
13-01-08 조회 4,431
12-12-27 조회 4,125
12-12-20 조회 1.1만
12-12-20 조회 3,427
12-12-06 조회 4,436
12-12-01 조회 4,192
12-11-28 조회 5,767
12-11-25 조회 5,475
12-10-20 조회 4,704
12-10-03 조회 4,873
12-09-26 조회 5,028
12-09-15 조회 8,470
12-09-14 조회 5,484
12-09-11 조회 4,005
12-09-10 조회 3,997
12-09-05 조회 8,887
12-08-29 조회 4,712
12-08-26 조회 3,833
12-08-23 조회 3,534
12-08-22 조회 3,615
12-08-22 조회 4,429
12-08-21 조회 3,572
12-08-20 조회 3,497
12-08-20 조회 9,305
12-08-15 조회 4,425
12-08-13 조회 3,337
12-08-11 조회 4,746
12-08-05 조회 4,615
12-08-03 조회 3,383
12-08-03 조회 4,078
12-08-02 조회 5,067
12-07-29 조회 4,184
12-07-26 조회 3,750
12-07-26 조회 3,613
12-07-22 조회 3,539
12-07-20 조회 3,365
12-07-19 조회 4,234
12-07-19 조회 3,500
12-07-16 조회 3,611
12-07-16 조회 4,531
12-07-15 조회 3,614
12-07-12 조회 7,426
12-07-12 조회 5,071
12-07-11 조회 3,123
12-07-09 조회 3,280
12-07-05 조회 3,890
12-07-04 조회 4,474
12-07-04 조회 3,872
12-07-04 조회 4,400
12-06-29 조회 3,870
12-06-27 조회 3,465
12-06-24 조회 3,933
12-06-22 조회 4,244
12-06-22 조회 8,650
12-06-22 조회 3,306
12-06-21 조회 3,321
12-06-17 조회 3,970
12-06-14 조회 4,056
12-06-14 조회 3,352
12-06-08 조회 4,487
12-06-06 조회 3,438
12-06-04 조회 4,221
12-05-26 조회 5,020
12-05-26 조회 3,972
12-05-25 조회 3,267
12-05-24 조회 3,364
12-05-22 조회 3,424
12-05-20 조회 3,063
12-05-16 조회 3,178
12-05-15 조회 3,562
12-05-10 조회 3,987
12-05-08 조회 3,531
12-05-04 조회 3,372
12-05-03 조회 4,329
12-05-02 조회 3,465
12-05-02 조회 3,224
12-05-02 조회 3,333
12-04-26 조회 3,493