| 
									
										
										
										
											2020-10-04 04:10:08 +03:00
										 |  |  | /********************************************************************** | 
					
						
							|  |  |  | *  | 
					
						
							|  |  |  | * | 
					
						
							|  |  |  | * | 
					
						
							| 
									
										
										
										
											2020-10-06 01:32:33 +03:00
										 |  |  | **********************************************/  /* c8 ignore next 2 */ | 
					
						
							| 
									
										
										
										
											2020-10-04 04:10:08 +03:00
										 |  |  | ((typeof define)[0]=='u'?function(f){module.exports=f(require)}:define) | 
					
						
							|  |  |  | (function(require){ var module={} // make module AMD/node compatible...
 | 
					
						
							|  |  |  | /*********************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 23:50:05 +03:00
										 |  |  | var object = require('ig-object') | 
					
						
							| 
									
										
										
										
											2020-10-04 04:10:08 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*********************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 23:50:05 +03:00
										 |  |  | var SetProtoMixin = | 
					
						
							|  |  |  | module.SetProtoMixin = | 
					
						
							|  |  |  | object.Mixin('SetMixin', 'soft', { | 
					
						
							|  |  |  | 	// Set set operation shorthands...
 | 
					
						
							|  |  |  | 	unite: function(other=[]){  | 
					
						
							|  |  |  | 		return new Set([...this, ...other]) }, | 
					
						
							|  |  |  | 	intersect: function(other){ | 
					
						
							|  |  |  | 		var test = other.has ?   | 
					
						
							|  |  |  | 			'has'  | 
					
						
							|  |  |  | 			: 'includes' | 
					
						
							|  |  |  | 		return new Set([...this] | 
					
						
							|  |  |  | 			.filter(function(e){  | 
					
						
							|  |  |  | 				return other[test](e) })) }, | 
					
						
							|  |  |  | 	subtract: function(other=[]){ | 
					
						
							|  |  |  | 		other = new Set(other) | 
					
						
							|  |  |  | 		return new Set([...this] | 
					
						
							|  |  |  | 			.filter(function(e){  | 
					
						
							|  |  |  | 				return !other.has(e) })) }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-03 02:11:02 +03:00
										 |  |  | 	sort: function(values=[]){ | 
					
						
							|  |  |  | 		values = (typeof(values) == 'function'  | 
					
						
							|  |  |  | 				|| values === undefined) ? | 
					
						
							|  |  |  | 			[...this].sort(values) | 
					
						
							|  |  |  | 			: values | 
					
						
							| 
									
										
										
										
											2020-11-22 23:50:05 +03:00
										 |  |  | 		var del = this.delete.bind(this) | 
					
						
							|  |  |  | 		var add = this.add.bind(this) | 
					
						
							| 
									
										
										
										
											2021-04-03 02:11:02 +03:00
										 |  |  | 		new Set([...values, ...this]) | 
					
						
							| 
									
										
										
										
											2020-11-22 23:50:05 +03:00
										 |  |  | 			.forEach(function(e){ | 
					
						
							|  |  |  | 				if(this.has(e)){ | 
					
						
							|  |  |  | 					del(e) | 
					
						
							|  |  |  | 					add(e) } }.bind(this)) | 
					
						
							|  |  |  | 		return this }, | 
					
						
							| 
									
										
										
										
											2021-06-08 16:44:51 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-08 16:49:11 +03:00
										 |  |  | 	replace: function(old, value, ordered=true){ | 
					
						
							| 
									
										
										
										
											2021-06-08 16:44:51 +03:00
										 |  |  | 		// nothing to do...
 | 
					
						
							|  |  |  | 		if(!this.has(old) || old === value){ | 
					
						
							|  |  |  | 			return this } | 
					
						
							| 
									
										
										
										
											2021-06-08 16:49:11 +03:00
										 |  |  | 		if(ordered){ | 
					
						
							|  |  |  | 			var order = [...this] | 
					
						
							|  |  |  | 			// XXX is this fast enough???
 | 
					
						
							|  |  |  | 			order[order.lastIndexOf(old)] = value } | 
					
						
							| 
									
										
										
										
											2021-06-08 17:00:55 +03:00
										 |  |  | 		// replace...
 | 
					
						
							|  |  |  | 		this.delete(old) | 
					
						
							|  |  |  | 		this.add(value) | 
					
						
							|  |  |  | 		ordered | 
					
						
							|  |  |  | 			&& this.sort(order) | 
					
						
							|  |  |  | 		return this }, | 
					
						
							|  |  |  | 	replaceIndex: function(index, value, ordered=true){ | 
					
						
							|  |  |  | 		// nothing to do...
 | 
					
						
							|  |  |  | 		if(this.size < index || old === value){ | 
					
						
							|  |  |  | 			return this } | 
					
						
							|  |  |  | 		var order = [...this] | 
					
						
							|  |  |  | 		var old = order[index] | 
					
						
							|  |  |  | 		ordered | 
					
						
							|  |  |  | 			&& (order[index] = value) | 
					
						
							|  |  |  | 		// replace...
 | 
					
						
							| 
									
										
										
										
											2021-06-08 16:44:51 +03:00
										 |  |  | 		this.delete(old) | 
					
						
							|  |  |  | 		this.add(value) | 
					
						
							| 
									
										
										
										
											2021-06-08 16:49:11 +03:00
										 |  |  | 		ordered | 
					
						
							|  |  |  | 			&& this.sort(order) | 
					
						
							| 
									
										
										
										
											2021-06-08 16:44:51 +03:00
										 |  |  | 		return this }, | 
					
						
							| 
									
										
										
										
											2020-11-22 23:50:05 +03:00
										 |  |  | }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SetProtoMixin(Set.prototype) | 
					
						
							| 
									
										
										
										
											2020-10-07 16:46:59 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-04 04:10:08 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /********************************************************************** | 
					
						
							|  |  |  | * vim:set ts=4 sw=4 :                               */ return module }) |