mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
implement menuOptions
This commit is contained in:
101
utils/containers.d.ts
vendored
101
utils/containers.d.ts
vendored
@@ -1,101 +0,0 @@
|
||||
declare module "utils/containers" {
|
||||
/**
|
||||
* An interface used to compare two instances of a same class.
|
||||
*/
|
||||
interface IEqualityComparer<T> {
|
||||
/**
|
||||
* Compares two instances of a same class.
|
||||
* @param x - First object to compare.
|
||||
* @param y - Second object to compare.
|
||||
* Returns true if objects are equal, otherwise false.
|
||||
*/
|
||||
equals(x: T, y: T): boolean;
|
||||
|
||||
/**
|
||||
* Generates an unique hash code for an object instance.
|
||||
*/
|
||||
getHashCode(obj: T): number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper class used to sort arrays.
|
||||
*/
|
||||
class ArraySortHelper {
|
||||
/**
|
||||
* Sorts an array using a comparer function to order elements.
|
||||
* @param keys - The array which will be sorted.
|
||||
* @param index - Starting index for sorting
|
||||
* @param length - How many items to sort.
|
||||
* @param compareFn - A function that compares two array members.
|
||||
*/
|
||||
public static sort<T>(keys: Array<T>, index: number, length: number, compareFn: (a: T, b: T) => number);
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a collection of keys and values.
|
||||
*/
|
||||
class Dictionary<TKey, TValue> {
|
||||
/**
|
||||
* The size of the dictionary.
|
||||
*/
|
||||
count: number;
|
||||
/**
|
||||
* Creates an instance of a Dictionary.
|
||||
*/
|
||||
constructor(comparer: IEqualityComparer<TKey>);
|
||||
|
||||
/**
|
||||
* Iterates through all items and executes a callback function.
|
||||
* @param callbackfn - A function that will be executed for each item.
|
||||
*/
|
||||
public forEach(callbackfn: (key: TKey, value: TValue) => void);
|
||||
|
||||
/**
|
||||
* Clears the entire Dictionary.
|
||||
*/
|
||||
public clear(): void;
|
||||
|
||||
/**
|
||||
* Removes the item associated with a given key.
|
||||
* @param key - A key to remove.
|
||||
*/
|
||||
public remove(key: TKey): boolean;
|
||||
|
||||
/**
|
||||
* Returns the item associated with a given key.
|
||||
* @param key - A lookup key.
|
||||
*/
|
||||
public get(key: TKey): TValue;
|
||||
|
||||
/**
|
||||
* Returns if an item associated with a given key exist in the Dictionary.
|
||||
* @param key - A lookup key.
|
||||
*/
|
||||
public has(key: TKey): boolean;
|
||||
|
||||
/**
|
||||
* Associates a value with a key.
|
||||
* @param key - A key for the value.
|
||||
* @param value - The real value.
|
||||
*/
|
||||
public set(key: TKey, value: TValue): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* An implementation of IEqualityComparer that works with strings.
|
||||
*/
|
||||
class StringComparer implements IEqualityComparer<string> {
|
||||
/**
|
||||
* Compares two strings.
|
||||
* @param x - First string to compare.
|
||||
* @param y - Second string to compare.
|
||||
* Returns true if strings are equal, otherwise false.
|
||||
*/
|
||||
equals(x: string, y: string): boolean;
|
||||
|
||||
/**
|
||||
* Generates an unique hash code for a string.
|
||||
*/
|
||||
getHashCode(str: string): number;
|
||||
}
|
||||
}
|
||||
@@ -1,166 +0,0 @@
|
||||
import definition = require("utils/containers");
|
||||
|
||||
export class ArraySortHelper implements definition.ArraySortHelper {
|
||||
|
||||
public static sort<T>(keys: Array<T>, index: number, length: number, compareFn: (a: T, b: T) => number) {
|
||||
|
||||
if (length < 2) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (keys.length === length) {
|
||||
keys.sort(compareFn);
|
||||
return;
|
||||
}
|
||||
|
||||
var sorted = keys.splice(index, length);
|
||||
sorted.sort(compareFn);
|
||||
|
||||
var args: Array<Object> = [0, index];
|
||||
keys.splice.apply(keys, args.concat(sorted));
|
||||
}
|
||||
}
|
||||
|
||||
export class StringComparer implements definition.IEqualityComparer<string> {
|
||||
public equals(x: string, y: string): boolean {
|
||||
return x === y;
|
||||
}
|
||||
|
||||
public getHashCode(str: string): number {
|
||||
var res = 0, len = str.length;
|
||||
for (var i = 0; i < len; i++) {
|
||||
res = res * 31 + str.charCodeAt(i);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
interface Entry<TKey, TValue> {
|
||||
hashCode: number;
|
||||
next: Entry<TKey, TValue>;
|
||||
key: TKey;
|
||||
value: TValue;
|
||||
}
|
||||
|
||||
export class Dictionary<TKey, TValue> implements definition.Dictionary<TKey, TValue> {
|
||||
|
||||
private _comparer: definition.IEqualityComparer<TKey>;
|
||||
private _count: number = 0;
|
||||
private _entries: Array<Entry<TKey, TValue>>;
|
||||
private _version: number = 0;
|
||||
|
||||
constructor(comparer: definition.IEqualityComparer<TKey>) {
|
||||
this._comparer = comparer;
|
||||
this._entries = new Array<Entry<TKey, TValue>>();
|
||||
}
|
||||
|
||||
get count(): number {
|
||||
return this._count;
|
||||
}
|
||||
|
||||
public forEach(callbackfn: (key: TKey, value: TValue) => void) {
|
||||
var currentVersion = this._version;
|
||||
for (var index in this._entries) {
|
||||
var entry = this._entries[index];
|
||||
callbackfn(entry.key, entry.value);
|
||||
if (currentVersion !== this._version) {
|
||||
throw new Error("Cannot modify Dictionary while enumerating.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public clear(): void {
|
||||
if (this.count > 0) {
|
||||
this._entries = new Array<Entry<TKey, TValue>>();
|
||||
this._count = 0;
|
||||
this._version++;
|
||||
}
|
||||
}
|
||||
|
||||
public remove(key: TKey): boolean {
|
||||
if (!key) {
|
||||
throw new Error("key cannot be null/undefined.");
|
||||
}
|
||||
|
||||
var hash = this._comparer.getHashCode(key);
|
||||
var previousEntry: Entry<TKey, TValue> = null;
|
||||
for (var entry = this._entries[hash]; entry; entry = entry.next) {
|
||||
if (entry.hashCode === hash && this._comparer.equals(entry.key, key)) {
|
||||
if (previousEntry) {
|
||||
previousEntry.next = entry.next;
|
||||
}
|
||||
else {
|
||||
this._entries[hash] = entry.next;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
this._count--;
|
||||
this._version++;
|
||||
|
||||
previousEntry = entry;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public get(key: TKey): TValue {
|
||||
var entry = this.findEntry(key);
|
||||
if (entry) {
|
||||
return entry.value;
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
public has(key: TKey): boolean {
|
||||
return (this.findEntry(key) ? true : false);
|
||||
}
|
||||
|
||||
public set(key: TKey, value: TValue): void {
|
||||
if (!key) {
|
||||
throw new Error("key cannot be null or undefined.");
|
||||
}
|
||||
|
||||
var hash = this._comparer.getHashCode(key);
|
||||
var lastEntryForHash: Entry<TKey, TValue> = null;
|
||||
for (var entry = this._entries[hash]; entry; entry = entry.next) {
|
||||
lastEntryForHash = entry;
|
||||
if (entry.hashCode === hash && this._comparer.equals(entry.key, key)) {
|
||||
entry.value = value;
|
||||
this._version++;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
this._count++;
|
||||
|
||||
var newEntry = <Entry<TKey, TValue>>{};
|
||||
newEntry.hashCode = hash;
|
||||
newEntry.key = key;
|
||||
newEntry.value = value;
|
||||
if (lastEntryForHash) {
|
||||
lastEntryForHash.next = newEntry;
|
||||
}
|
||||
else {
|
||||
this._entries[hash] = newEntry;
|
||||
}
|
||||
|
||||
this._version++;
|
||||
}
|
||||
|
||||
private findEntry(key: TKey): Entry<TKey, TValue> {
|
||||
if (!key) {
|
||||
throw new Error("key cannot be null or undefined.");
|
||||
}
|
||||
|
||||
var hash = this._comparer.getHashCode(key);
|
||||
for (var entry = this._entries[hash]; entry; entry = entry.next) {
|
||||
if (entry.hashCode === hash && this._comparer.equals(entry.key, key)) {
|
||||
return entry;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user