⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 valuesets.h

📁 遗传算法做图像的模式匹配
💻 H
📖 第 1 页 / 共 3 页
字号:
				T diff = value < _values[ 0 ] ? _values[ 0 ] - value : value - _values[ 0 ];

				// find closest value
				for( typename vector<T>::const_iterator it = _values.begin() + 1; it != _values.end(); it++ )
				{
					if( value == *it )
						return value;

					// calculate the distance
					T d = value > *it ? value > *it : *it - value;

					// closer
					if( d < diff )
					{
						closest = &*it;
						diff = d;
					}
				}

				if( this->_viceVersa )
				{
					// find closest value
					for( typename vector<T>::const_iterator it = _invertedValues.begin(); it != _invertedValues.end(); it++ )
					{
						if( value == *it )
							return value;
						else if( value > *it )
						{
							// closer
							if( value - *it < *closest )
								closest = &*it;
						}
						else
						{
							// closer
							if( *it - value < *closest )
								closest = &*it;
						}
					}
				}

				return *closest;
			}

			/// <summary><c>Add</c> method inserts new value and its counterpart into value set.
			/// If original value is already in set call fails and method returns <c>false</c>.
			///
			/// This method is not thread-safe.</summary>
			/// <param name="original">original value which is going to be added.</param>
			/// <param name="inverted">inverted value, counterpart of original value.</param>
			/// <returns>Method returns <c>true</c> if new value is added successfully.
			/// If original value already exists in set, method returns <c>false</c>.</returns>
			bool GACALL Add(const T& original,
				const T& inverted)
			{
				if( Belongs( original ) || ( this->_viceVersa && Belongs( inverted ) ) )
					return false;

				_values.push_back( original );
				_invertedValues.push_back( inverted );

				return true;
			}

			/// <summary><c>Add</c> method inserts new values and their counterpart into value set. New values which has duplicates in the set are ignored.
			/// Both arrays of values must be the same size.
			///
			/// This method is not thread-safe.</summary>
			/// <param name="values">array of original values which are going to be added.</param>
			/// <param name="invertedValues">array of inverted values, counterparts of original values.</param>
			/// <param name="size">number of values which are going to be added.</param>
			/// <returns>Method returns number of successfully inserted values into set.</returns>
			int Add(const T* values,
				const T* invertedValues,
				int size)
			{
				int added = 0;

				for( int i = size - 1; i >= 0; i-- )
				{
					if( !( Belongs( values[ i ] ) || ( this->_viceVersa && Belongs( invertedValues[ i ] ) ) ) )
					{
						_values.push_back( values[ i ] );
						_invertedValues.push_back( invertedValues[ i ] );

						added++;
					}
				}

				return added;
			}

			/// <summary><c>Remove</c> method removes value and its counterpart from the set.
			/// It removes value from the set if it's equal to specified value or if specified value is equal to inverted value.
			///
			/// This method is not thread-safe.</summary>
			/// <param name="value">value which is should be removed.</param>
			/// <returns>Method returns <c>true</c> if it removed value successfully. It value doesn't belong to the set, method returns <c>false</c>.</returns>
			bool GACALL Remove(const T& value)
			{
				typename vector<T>::iterator it1 = _values.begin();
				typename vector<T>::iterator it2 = _invertedValues.begin();

				for( ; it1 != _values.end(); it1++, it2++ )
				{
					if( value == *it1 || ( this->_viceVersa && value == *it2 ) )
					{
						_values.erase( it1 );
						_invertedValues.erase( it2 );
						return;
					}
				}
			}

			/// <summary>Remove method removes value and its counterpart from the set. It removes value by its position in the set.
			///
			/// This method is not thread-safe.</summary>
			/// <param name="pos">position of value which is should be removed.</param>
			/// <returns>Method returns <c>true</c> if it removed value successfully. It position is out of range it returns <c>false</c>.</returns>
			bool GACALL Remove(int pos)
			{
				typename vector<T>::iterator it1 = _values.begin();
				typename vector<T>::iterator it2 = _invertedValues.begin();

				for( int i = 0; it1 != _values.end(); it1++, it2++, i++ )
				{
					if( i == pos )
					{
						_values.erase( it1 );
						_invertedValues.erase( it2 );

						return;
					}
				}
			}

			/// <summary><c>GetValue</c> method returns original value at given position.
			/// 
			/// This method is not thread-safe.</summary>
			/// <param name="pos">position of the value.</param>
			/// <returns>Method returns original value at given position.</returns>
			inline const T& GACALL GetValue(int pos) const { return _values[ pos ]; }

			/// <summary>GetValue method returns value and its counterpart at given position.
			///
			/// This method is not thread-safe.</summary>
			/// <param name="pos">position of the value.</param>
			/// <param name="original">reference to variable to which original value is saved.</param>
			/// <param name="inverted">reference to variable to which inverted value is saved.</param>
			inline void GACALL GetValue(int pos,
				T& original,
				T& inverted)
			{
				original = _values[ pos ];
				inverted = _invertedValues[ pos ];
			}

			/// <summary><c>GetInvertedValue</c> method returns inverted value at given position.
			///
			/// This method is not thread-safe.</summary>
			/// <param name="pos">position of the value.</param>
			/// <returns>Method returns inverted value at given position.</returns>
			inline const T& GACALL GetInvertedValue(int pos) const { return _invertedValues[ pos ]; }

			/// <summary><c>SetValue</c> method sets original value at given position.
			///
			/// This method is not thread-safe.</summary>
			/// <param name="pos">position of the value.</param>
			/// <param name="value">new original value.</param>
			inline void GACALL SetValue(int pos,
				const T& value) { _values[ pos ] = value; }

			/// <summary><c>SetValue</c> method sets value and its counterpart at given position.
			///
			/// This method is not thread-safe.</summary>
			/// <param name="pos">position of the value.</param>
			/// <param name="original">new original value.</param>
			/// <param name="inverted">new inverted value.</param>
			inline void GACALL SetValue(int pos,
				const T& original,
				const T& inverted)
			{
				_values[ pos ] = original;
				_invertedValues[ pos ] = inverted;
			}

			/// <summary><c>SetInvertedValue</c> method sets inverted value at given position.
			///
			/// This method is not thread-safe.</summary>
			/// <param name="pos">position of the value.</param>
			/// <param name="value">new inverted value.</param>
			inline void GACALL SetInvertedValue(int pos,
				const T& value) { _invertedValues[ pos ] = value; }

		};// END CLASS DEFINITION GaMultiValueSet

		/// <summary>This template class represents bounds of interval value set.
		///
		/// This class has no built-in synchronizator, so <c>LOCK_OBJECT</c> and <c>LOCK_THIS_OBJECT</c> macros cannot be used with instances of this class.
		/// No public or private methods are thread-safe.</summary>
		/// <param name="T">type of bounds. This type must support operators &gt;, &gt;=, &lt; and &lt;=.</param>
		template <typename T>
		class GaValueIntervalBounds
		{

		private:

			/// <summary>Lower bound.</summary>
			T _lower;

			/// <summary>Higher bound.</summary>
			T _higher;

		public:

			/// <summary>This constructor initializes bounds with user-defined values. Bounds are automatically sorted.</summary>
			/// <param name="lower">value of lower bound.</param>
			/// <param name="higher">value of higher bound.</param>
			GaValueIntervalBounds(const T& lower,
				const T& higher)
			{
				if( lower <= higher )
				{
					_lower = lower;
					_higher = higher;
				}
				else
				{
					_lower = higher;
					_higher = lower;
				}
			}

			/// <summary>This constructor creates undefined bounds.</summary>
			GaValueIntervalBounds() { }

			/// <summary>This method is not thread-safe.</summary>
			/// <returns>Method returns lower bound.</returns>
			inline const T& GACALL GetLowerBound() const { return _lower; }

			/// <summary>This method is not thread-safe.</summary>
			/// <returns>Method returns higher bound.</returns>
			inline const T& GACALL GetHigherBound() const { return _higher; }

			/// <summary><c>GetBounds</c> method returns both bounds.
			///
			/// This method is not thread-safe.</summary>
			/// <param name="lower">reference to variable to which lower bound is going to be stored.</param>
			/// <param name="higher">reference to variable to which higher bound is going to be stored.</param>
			inline void GACALL GetBounds(T& lower,
				T& higher)
			{
				lower = _lower;
				higher = _higher;
			}

			/// <summary><c>SetLowerBound</c> method sets new lower bound. If new value for lower bound is higher then higher bound, call is ignored.
			///
			/// This method is not thread-safe.</summary>
			/// <param name="lower">new value for lower bound.</param>
			inline void GACALL SetLowerBound(const T& lower)
			{
				if( lower <= _higher )
					_lower = lower;
			}

			/// <summary><c>SetHigherBound</c> method sets new higher bound. If new value for higher bound is lower then lower bound, call is ignored.
			///
			/// This method is not thread-safe.</summary>
			/// <param name="higher">new value for higher bound.</param>
			inline void GACALL SetHigherBound(const T& higher)
			{
				if( _lower <= higher )
					_higher = higher;
			}

			/// <summary><c>SetBound</c> method sets both bounds. Bounds are automatically sorted.
			/// 
			/// This method is not thread-safe.</summary>
			/// <param name="lower">new value for lower bound.</param>
			/// <param name="higher">new value for higher bound.</param>
			void GACALL SetBounds(const T& lower,
				const T& higher)
			{
				if( lower <= higher )
				{
					_lower = lower;
					_higher = higher;
				}
				else
				{
					_lower = higher;
					_higher = lower;
				}
			}

			/// <summary><c>InBounds</c> method checks value to see if it is in bounds of the interval.
			///
			/// This method is not thread-safe.</summary>
			/// <param name="value">value which is checked.</param>
			/// <returns>Method returns <c>true</c> if value is in bound, otherwise it returns <c>false</c>.</returns>
			inline bool GACALL InBounds(const T& value) const  { return value >= _lower && value <= _higher; }

		};// END CLASS DEFINITION GaValueIntervalBounds

		/// <summary>This template represents value set which has interval of values. Intervals as specified by theirs bounds.
		/// This value set uses user-specified random generator to generate values within the interval. 
		/// 
		/// This class has built-in synchronizator so it is allowed to use <c>LOCK_OBJECT</c> and <c>LOCK_THIS_OBJECT</c> macros
		/// with instances of this class, but no public or private methods are thread-safe.</summary>
		/// <param name="T">type of bounds. This type must support operators &gt;, &gt;=, &lt; and &lt;=.</param>
		template <typename T>
		class GaIntervalValueSet : public GaValueSet<T>
		{

		private:

			/// <summary>Bounds of interval of original values. </summary>
			GaValueIntervalBounds<T> _values;

			/// <summary>Bounds of interval of inverted values.</summary>
			GaValueIntervalBounds<T> _invertedValues;

			/// <summary>This attribute is pointer to random generator which is used for generating of random values within interval.</summary>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -