1 /**************************************************************************** 2 Copyright (c) 2010-2012 cocos2d-x.org 3 Copyright (c) 2008-2010 Ricardo Quesada 4 Copyright (c) 2011 Zynga Inc. 5 6 http://www.cocos2d-x.org 7 8 Permission is hereby granted, free of charge, to any person obtaining a copy 9 of this software and associated documentation files (the "Software"), to deal 10 in the Software without restriction, including without limitation the rights 11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 copies of the Software, and to permit persons to whom the Software is 13 furnished to do so, subject to the following conditions: 14 15 The above copyright notice and this permission notice shall be included in 16 all copies or substantial portions of the Software. 17 18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 THE SOFTWARE. 25 ****************************************************************************/ 26 27 /** 28 * @class 29 * @extends cc.Class 30 */ 31 cc.Set = cc.Class.extend(/** @lends cc.Set# */{ 32 /** 33 * Constructor 34 * @param {cc.Set} setObject 35 */ 36 ctor:function (setObject) { 37 if (setObject) { 38 this._set = [].concat(setObject._set); 39 } else { 40 this._set = []; 41 } 42 }, 43 44 /** 45 * Return a copy of the cc.Set, it will copy all the elelments. 46 * @return {cc.Set} 47 */ 48 copy:function () { 49 return new cc.Set(this); 50 }, 51 52 /** 53 * It is the same as copy(). 54 * @return {cc.Set} 55 */ 56 mutableCopy:function () { 57 return this.copy(); 58 }, 59 60 /** 61 * Return the number of elements the cc.Set contains. 62 * @return {Number} 63 */ 64 count:function () { 65 return this._set.length; 66 }, 67 68 /** 69 * Add a element into cc.Set, it will retain the element. 70 * @param {object} obj 71 */ 72 addObject:function (obj) { 73 if(cc.ArrayContainsObject(this._set,obj)) 74 return; 75 this._set.push(obj); 76 //sort 77 this._set.sort(function(a,b){return a-b;}); 78 }, 79 80 /** 81 * Remove the given element, nothing todo if no element equals obj. 82 * @param {object} obj 83 */ 84 removeObject:function (obj) { 85 var k = 0; 86 for (var i = 0, n = 0; i < this._set.length; i++) { 87 if (this._set[i] != obj) { 88 this._set[n++] = this._set[i]; 89 k++; 90 } 91 } 92 this._set.length = k; 93 }, 94 95 /** 96 * Return the iterator that points to the first element. 97 * @reture Object 98 */ 99 begin:function(){ 100 if(this._set && this._set.length > 0) 101 return this._set[0]; 102 return null; 103 }, 104 105 /** 106 * Return the iterator that points to the poisition after the last element. 107 * @reture Object 108 */ 109 end:function(){ 110 if(this._set && this._set.length > 0) 111 return this._set[this._set.length -1]; 112 return null; 113 }, 114 115 /** 116 * Check if cc.Set contains a element equals obj. 117 * @param {object} obj 118 * @return {Boolean} 119 */ 120 containsObject:function (obj) { 121 return cc.ArrayContainsObject(this._set,obj); 122 }, 123 124 /** 125 * Return the first element if it contains elements, or null if it doesn't contain any element. 126 * @return {object|Null} 127 */ 128 anyObject:function () { 129 if (this._set && this._set.length > 0) { 130 return this._set[0]; 131 } else { 132 return null; 133 } 134 }, 135 136 _set:null 137 }); 138 139 /** 140 * cc.NSMutableSet is the same as cc.Set 141 * @class 142 * @extends cc.Set 143 */ 144 cc.NSMutableSet = cc.Set; 145