deUtil.js (2975B)
1 /*------------------------------------------------------------------------- 2 * drawElements Quality Program OpenGL ES Utilities 3 * ------------------------------------------------ 4 * 5 * Copyright 2014 The Android Open Source Project 6 * 7 * Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 * 19 */ 20 21 'use strict'; 22 goog.provide('framework.delibs.debase.deUtil'); 23 goog.require('framework.delibs.debase.deMath'); 24 25 goog.scope(function() { 26 27 var deUtil = framework.delibs.debase.deUtil; 28 var deMath = framework.delibs.debase.deMath; 29 30 //! Get an element of an array with a specified size. 31 /** 32 * @param {Array} array 33 * @param {number} offset 34 * @return {*} 35 */ 36 deUtil.getArrayElement = function(array, offset) { 37 assertMsgOptions(deMath.deInBounds32(offset, 0, array.length), 'Array element out of bounds', false, true); 38 return array[offset]; 39 }; 40 41 /** 42 * clone - If you need to pass/assign an object by value, call this 43 * @param {*} obj 44 * @return {*} 45 */ 46 deUtil.clone = function(obj) { 47 if (obj == null || typeof(obj) != 'object') 48 return obj; 49 50 var temp = {}; 51 if (ArrayBuffer.isView(obj)) { 52 temp = new obj.constructor(obj); 53 } else if (obj instanceof Array) { 54 temp = new Array(obj.length); 55 for (var akey in obj) 56 temp[akey] = deUtil.clone(obj[akey]); 57 } else if (obj instanceof ArrayBuffer) { 58 temp = new ArrayBuffer(obj.byteLength); 59 var dst = new Uint8Array(temp); 60 var src = new Uint8Array(obj); 61 dst.set(src); 62 } else { 63 temp = Object.create(obj.constructor.prototype); 64 temp.constructor = obj.constructor; 65 for (var key in obj) 66 temp[key] = deUtil.clone(obj[key]); 67 } 68 return temp; 69 }; 70 71 /** 72 * Add a push_unique function to Array. Will insert only if there is no equal element. 73 * @template T 74 * @param {Array<T>} array Any array 75 * @param {T} object Any object 76 */ 77 deUtil.dePushUniqueToArray = function(array, object) { 78 //Simplest implementation 79 for (var i = 0; i < array.length; i++) { 80 if (object.equals !== undefined) 81 if (object.equals(array[i])) 82 return undefined; 83 else if (object === array[i]) 84 return undefined; 85 } 86 87 array.push(object); 88 }; 89 90 });