Skip to content

Instantly share code, notes, and snippets.

@monochromer
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monochromer/9064b89a4fa0d0803eed to your computer and use it in GitHub Desktop.
Save monochromer/9064b89a4fa0d0803eed to your computer and use it in GitHub Desktop.
Реализация структуры "множество"
/**
* Создание множества.
* @constructor
*/
function Set() {
/**
* Приватная переменная для хранения множества
*/
var items = {};
/**
* Проверка наличия элемента в множестве
* @param {object} value - значение элемента.
*/
this.has = function (value) {
return items.hasOwnProperty(value);
};
/**
* Добавление элемента в множество
* @param {object} value - значение элемента.
*/
this.add = function (value) {
if ( !this.has(value) ) {
items[value] = value;
return true;
}
return false;
};
/**
* Удаление элемента из множества
* @param {object} value - значение элемента.
*/
this.remove = function (value) {
if ( this.has(value) ) {
delete items[value];
return true;
}
return false;
};
/**
* Очистка множества
*/
this.clear = function () {
items = {};
};
/**
* Количество элементов в множестве
*/
this.size = function () {
return Object.keys(items).length;
};
this.sizeLegacy = function () {
var count = 0;
for(var prop in items) {
if(items.hasOwnProperty(prop))
++count;
}
return count;
};
/**
* Возвращает элементы множества в виде массива
*/
this.values = function () {
return Object.keys(items);
};
this.valuesLegacy = function () {
var keys = [];
for( var key in items ) {
keys.push(key);
}
return keys;
};
/**
* Операция объединения множеств
* @param {object} otherSet - множество, с которым будет объединяться текущее множество.
*/
this.union = function (otherSet) {
var unionSet = new Set();
var values = this.values();
for (var i = 0; i < values.length; i++ ) {
unionSet.add(values[i]);
}
values = otherSet.values();
for ( var i = 0; i < values.length; i++ ) {
unionSet.add(values[i]);
}
return unionSet;
};
/**
* Операция пересечения множеств
* @param {object} otherSet - множество, с которым будет пересекаться текущее множество.
*/
this.intersection = function (otherSet) {
var intersectionSet = new Set();
var values = this.values();
for ( var i = 0; i < values.length; i++ ) {
if ( otherSet.has(values[i]) ) {
intersectionSet.add(values[i]);
}
}
return intersectionSet;
}
/**
* Операция вычитания множеств
* @param {object} otherSet - множество, с которым будет производиться операция вычитания.
*/
this.difference = function (otherSet) {
var differenceSet = new Set();
var values = this.values();
for ( var i = 0; i < values.length; i++ ) {
if (!otherSet.has(values[i]) ) {
differenceSet.add(values[i]);
}
}
return differenceSet;
};
/**
* Операция проверки на подмножество
* @param {object} otherSet.
*/
this.subset = function (otherSet) {
if ( this.size() > otherSet.size() ) {
return false;
} else {
var values = this.values();
for ( var i = 0; i < values.length; i++ ) {
if ( !otherSet.has(values[i]) ) {
return false;
}
}
return true;
}
};
}
/**
* Пример использоваия
*/
var setA = new Set();
setA.add(1);
setA.add(2);
setA.add(3);
var setB = new Set();
setB.add(2);
setB.add(3);
setB.add(4);
var unionAB = setA.union(setB);
console.log(unionAB.values());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment