1 /****************************************************************************
  2  Copyright (c) 2012 cocos2d-x.org
  3  Copyright (c) 2010 Sangwoo Im
  4 
  5  http://www.cocos2d-x.org
  6 
  7  Permission is hereby granted, free of charge, to any person obtaining a copy
  8  of this software and associated documentation files (the "Software"), to deal
  9  in the Software without restriction, including without limitation the rights
 10  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 11  copies of the Software, and to permit persons to whom the Software is
 12  furnished to do so, subject to the following conditions:
 13 
 14  The above copyright notice and this permission notice shall be included in
 15  all copies or substantial portions of the Software.
 16 
 17  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 18  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 19  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 20  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 21  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 22  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 23  THE SOFTWARE.
 24  ****************************************************************************/
 25 
 26 cc.SortableObject = cc.Class.extend({
 27     setObjectID:function (objectId) {
 28     },
 29     getObjectID:function () {
 30         return 0;
 31     }
 32 });
 33 
 34 cc.SortedObject = cc.SortableObject.extend({
 35     _objectID:0,
 36 
 37     ctor:function () {
 38         this._objectID = 0;
 39     },
 40 
 41     setObjectID:function (objectID) {
 42         this._objectID = objectID;
 43     },
 44 
 45     getObjectID:function () {
 46         return this._objectID;
 47     }
 48 });
 49 
 50 var _compareObject = function (val1, val2) {
 51     return (val1.getObjectID() - val2.getObjectID());
 52 };
 53 
 54 cc.ArrayForObjectSorting = cc.Class.extend({
 55     _saveObjectArr:null,
 56 
 57     ctor:function () {
 58         this._saveObjectArr = [];
 59     },
 60     /**
 61      * Inserts a given object into array.
 62      *
 63      * Inserts a given object into array with key and value that are used in
 64      * sorting. "value" must respond to message, compare:, which returns
 65      * (NSComparisonResult). If it does not respond to the message, it is appended.
 66      * If the compare message does not result NSComparisonResult, sorting behavior
 67      * is not defined. It ignores duplicate entries and inserts next to it.
 68      *
 69      * @param {object} addObject
 70      */
 71     insertSortedObject:function (addObject) {
 72         if(!addObject)
 73             throw "cc.ArrayForObjectSorting.insertSortedObject(): addObject should be non-null.";
 74         var idx = this.indexOfSortedObject(addObject);
 75         this.insertObject(addObject, idx);
 76     },
 77 
 78     /*!
 79      * Removes an object in array.
 80      *
 81      * Removes an object with given key and value. If no object is found in array
 82      * with the key and value, no action is taken.
 83      *
 84      * @param value to remove
 85      */
 86     removeSortedObject:function (delObject) {
 87         if (this.count() == 0) {
 88             return;
 89         }
 90 
 91         var idx = this.indexOfSortedObject(delObject);
 92         if (idx < this.count() && idx != cc.INVALID_INDEX) {
 93             var foundObj = this.objectAtIndex(idx);
 94             if (foundObj.getObjectID() == delObject.getObjectID()) {
 95                 this.removeObjectAtIndex(idx);
 96             }
 97         }
 98     },
 99 
100     /*!
101      * Sets a new value of the key for the given object.
102      *
103      * In case where sorting value must be changed, this message must be sent to
104      * keep consistency of being sorted. If it is changed externally, it must be
105      * sorted completely again.
106      *
107      * @param value to set
108      * @param object the object which has the value
109      */
110     setObjectID_ofSortedObject:function (tag, setObject) {
111         var idx = this.indexOfSortedObject(setObject);
112         if (idx < this.count() && idx != cc.INVALID_INDEX) {
113             var foundObj = this.objectAtIndex(idx);
114             if (foundObj.getObjectID() == setObject.getObjectID()) {
115                 this.removeObjectAtIndex(idx);
116                 foundObj.setObjectID(tag);
117                 this.insertSortedObject(foundObj);
118             }
119         }
120     },
121 
122     objectWithObjectID:function (tag) {
123         if (this.count() == 0) {
124             return null;
125         }
126         var foundObj = new cc.SortedObject();
127         foundObj.setObjectID(tag);
128 
129         var idx = this.indexOfSortedObject(foundObj);
130         if (idx < this.count() && idx != cc.INVALID_INDEX) {
131             foundObj = this.objectAtIndex(idx);
132             if (foundObj.getObjectID() != tag)
133                 foundObj = null;
134         }
135         return foundObj;
136     },
137 
138     /*!
139      * Returns an object with given key and value.
140      *
141      * Returns an object with given key and value. If no object is found,
142      * it returns nil.
143      *
144      * @param value to locate object
145      * @return object found or nil.
146      */
147     getObjectWithObjectID:function (tag) {
148         return null;
149     },
150 
151     /*!
152      * Returns an index of the object with given key and value.
153      *
154      * Returns the index of an object with given key and value.
155      * If no object is found, it returns an index at which the given object value
156      * would have been located. If object must be located at the end of array,
157      * it returns the length of the array, which is out of bound.
158      *
159      * @param value to locate object
160      * @return index of an object found
161      */
162     indexOfSortedObject:function (idxObj) {
163         var idx = 0;
164         if (idxObj) {
165             //       CCObject* pObj = (CCObject*)bsearch((CCObject*)&object, data.arr, data.num, sizeof(CCObject*), _compareObject);
166             // FIXME: need to use binary search to improve performance
167             var uPrevObjectID = 0;
168             var uOfSortObjectID = idxObj.getObjectID();
169 
170             var locObjectArr = this._saveObjectArr;
171             for (var i = 0; i < locObjectArr.length; i++) {
172                 var pSortableObj = locObjectArr[i];
173                 var curObjectID = pSortableObj.getObjectID();
174                 if ((uOfSortObjectID == curObjectID) ||
175                     (uOfSortObjectID >= uPrevObjectID && uOfSortObjectID < curObjectID)) {
176                     break;
177                 }
178                 uPrevObjectID = curObjectID;
179                 idx++;
180             }
181         } else {
182             idx = cc.INVALID_INDEX;
183         }
184         return idx;
185     },
186 
187     //implement array method
188     count:function () {
189         return this._saveObjectArr.length;
190     },
191 
192     lastObject:function () {
193         var locObjectArr = this._saveObjectArr;
194         if (locObjectArr.length == 0)
195             return null;
196         return locObjectArr[locObjectArr.length - 1];
197     },
198 
199     objectAtIndex:function (idx) {
200         return this._saveObjectArr[idx];
201     },
202 
203     addObject:function (addObj) {
204         this._saveObjectArr.push(addObj);
205         this._saveObjectArr.sort(_compareObject);
206     },
207 
208     removeObjectAtIndex:function (idx) {
209         cc.ArrayRemoveObjectAtIndex(this._saveObjectArr, idx);
210         this._saveObjectArr.sort(_compareObject);
211     },
212 
213     insertObject:function (addObj, idx) {
214         this._saveObjectArr = cc.ArrayAppendObjectToIndex(this._saveObjectArr, addObj, idx);
215         this._saveObjectArr.sort(_compareObject);
216     }
217 });
218