Skip to content

Commit

Permalink
Update array_static.h
Browse files Browse the repository at this point in the history
  • Loading branch information
gammasoft71 committed Sep 20, 2024
1 parent 98b904e commit 456300c
Showing 1 changed file with 127 additions and 4 deletions.
131 changes: 127 additions & 4 deletions src/xtd.core/include/xtd/array_static.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@
/// @copyright Copyright (c) 2024 Gammasoft. All rights reserved.

#if !defined(__XTD_ARRAY_INTERNAL__)
#error "Do not include this file: Internal use only. Include <array> or <array.h> instead."
#error "Do not include this file: Internal use only. Include <xtd/array> or <xtd/array.h> instead."
#endif

#include "collections/generic/comparer.h"

/// @brief The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more.
namespace xtd {
namespace xtd {
/// @cond
namespace collections::object_model {
template<typename type_t>
class read_only_collection;
}
/// @endcond

/// @brief Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the base class for all arrays.
/// @par Definition
/// ```cpp
Expand All @@ -34,14 +43,128 @@ namespace xtd {
/// The following code example demonstrates different methods to create an array.
/// @include array1.cpp
/// @par Examples
/// The following code example creates and initializes an Array and displays its properties and its elements.
/// The following code example creates and initializes an array_ and displays its properties and its elements.
/// @include array2.cpp
template<>
class array_<> static_ {
public:
/// @name Public Statix Methods
/// @name Public Aliases

/// @{
/// @}

/// @name Public Static Methods

/// @{
/// @brief Returns a read-only wrapper for the specified array.
/// @param array The one-dimensional, zero-based array to wrap in a read-only ReadOnlyCollection<type_t> wrapper.
/// @return A read-only ReadOnlyCollection<type_t> wrapper for the specified array.
/// @remarks To prevent any modifications to the array, expose the array only through this wrapper.
/// @remarks A collection that is read-only is simply a collection with a wrapper that prevents modifying the collection; therefore, if changes are made to the underlying collection, the read-only collection reflects those changes.
/// @remarks This method is an O(n) operation.
/// @par Examples
/// The following example wraps an array in a read-only ReadOnlyCollection<type_t>.
/// @include ArrayAsReadOnly.cpp
template<typename type_t, typename allocator_t>
static xtd::collections::object_model::read_only_collection<type_t> as_read_only(const xtd::array_<type_t, 1, allocator_t>& array);

/// @brief Searches a range of elements in a one-dimensional sorted array for a value, using the IComparable interface implemented by each element of the array and by the specified value.
/// @param array The sorted one-dimensional array_ to search.
/// @param index The starting index of the range to search.
/// @param length The length of the range to search.
/// @param value The object to search for.
/// @return int32 The index of the specified value in the specified array, if value is found; otherwise, a negative number. If value is not found and value is less than one or more elements in array, the negative number returned is the bitwise complement of the index of the first element that is larger than value. If value is not found and value is greater than all elements in array, the negative number returned is the bitwise complement of (the index of the last element plus 1). If this method is called with a non-sorted array, the return value can be incorrect and a negative number could be returned, even if value is present in array.
/// @exception RankException array is multidimensional.
/// @exception ArgumentOutOfRangeException index is less than the lower bound of array. -or- length is less than zero.
/// @exception ArgumentException index and length do not specify a valid range in array. -or- value is of a type that is not compatible with the elements of array.
/// @exception InvalidOperationException value does not implement the IComparable interface, and the search encounters an element that does not implement the IComparable interface.
/// @remarks This method does not support searching arrays that contain negative indexes. array must be sorted before calling this method.
/// @remarks If the array_ does not contain the specified value, the method returns a negative integer. You can apply the bitwise complement operator ~ to the negative result to produce an index. If this index is one greater than the upper bound of the array, there are no elements larger than value in the array. Otherwise, it is the index of the first element that is larger than value.
/// @remarks Either value or every element of array must implement the IComparable interface, which is used for comparisons. The elements of array must already be sorted in increasing value according to the sort order defined by the IComparable implementation; otherwise, the result might be incorrect.
/// @note If value does not implement the IComparable interface, the elements of array are not tested for IComparable before the search begins. An exception is thrown if the search encounters an element that does not implement IComparable.
/// @remarks Duplicate elements are allowed. If the array_ contains more than one element equal to value, the method returns the index of only one of the occurrences, and not necessarily the first one.
/// @remarks null can always be compared with any other reference type; therefore, comparisons with null do not generate an exception.
/// @note For every element tested, value is passed to the appropriate IComparable implementation, even if value is null. That is, the IComparable implementation determines how a given element compares to null.
/// @remarks This method is an O(log n) operation, where n is length.
//template<typename type_t, typename allocator_t>
//static int32 binary_search(const array_<type_t, 1, allocator_t>& array, int32 index, int32 length, const type_t& value) {return binary_search(array, index, length, value, xtd::collections::generic::comparer<type_t>::default_comparer);}

/// @brief Searches a range of elements in a one-dimensional sorted array for a value, using the specified IComparer interface.
/// @param array The sorted one-dimensional array_ to search.
/// @param index The starting index of the range to search.
/// @param length The length of the range to search.
/// @param value The object to search for.
/// @param comparer The IComparer implementation to use when comparing elements. -or- null to use the IComparable implementation of each element.
/// @return int32 The index of the specified value in the specified array, if value is found; otherwise, a negative number. If value is not found and value is less than one or more elements in array, the negative number returned is the bitwise complement of the index of the first element that is larger than value. If value is not found and value is greater than all elements in array, the negative number returned is the bitwise complement of (the index of the last element plus 1). If this method is called with a non-sorted array, the return value can be incorrect and a negative number could be returned, even if value is present in array.
/// @exception RankException array is multidimensional.
/// @exception ArgumentOutOfRangeException index is less than the lower bound of array. -or- length is less than zero.
/// @exception ArgumentException index and length do not specify a valid range in array. -or- value is of a type that is not compatible with the elements of array.
/// @exception InvalidOperationException value does not implement the IComparable interface, and the search encounters an element that does not implement the IComparable interface.
/// @remarks This method does not support searching arrays that contain negative indexes. array must be sorted before calling this method.
/// @remarks If the array_ does not contain the specified value, the method returns a negative integer. You can apply the bitwise complement operator ~ to the negative result to produce an index. If this index is one greater than the upper bound of the array, there are no elements larger than value in the array. Otherwise, it is the index of the first element that is larger than value.
/// @remarks The comparer customizes how the elements are compared. For example, you can use a System.Collections.CaseInsensitiveComparer as the comparer to perform case-insensitive string searches.
/// @remarks If comparer is not null, the elements of array are compared to the specified value using the specified IComparer implementation. The elements of array must already be sorted in increasing value according to the sort order defined by comparer; otherwise, the result might be incorrect.
/// @remarks If comparer is null, the comparison is done using the IComparable implementation provided by the element itself or by the specified value. The elements of array must already be sorted in increasing value according to the sort order defined by the IComparable implementation; otherwise, the result might be incorrect.
/// @note If comparer is null and value does not implement the IComparable interface, the elements of array are not tested for IComparable before the search begins. An exception is thrown if the search encounters an element that does not implement IComparable.
/// @remarks Duplicate elements are allowed. If the array_ contains more than one element equal to value, the method returns the index of only one of the occurrences, and not necessarily the first one.
/// @remarks null can always be compared with any other reference type; therefore, comparisons with null do not generate an exception when using IComparable.
/// @note For every element tested, value is passed to the appropriate IComparable implementation, even if value is null. That is, the IComparable implementation determines how a given element compares to null.
/// @remarks This method is an O(log n) operation, where n is length.
/*
template<typename type_t, typename allocator_t>
static xtd::size binary_search(const array_<type_t, 1, allocator_t>& array, xtd::size index, xtd::size count, const type_t& value, const xtd::collections::generic::icomparer<type_t>& comparer) {
if (index + count > array->Length) throw ArgumentException(caller_);
typename std::vector<type_t>::const_iterator first = array.array.begin();
typename std::vector<type_t>::const_iterator last = array.array.begin();
std::advance(first, index);
std::advance(last, index + count);
typename std::vector<type_t>::const_iterator position = std::lower_bound(first, last, value, array_<type_t>::Comparer(&comparer));
if (position != array.array.end() && !comparer->Compare(value, *position))
return (int32)std::distance(array.array.begin(), position);
return (int32)~std::distance(array.array.begin(), position);
}*/

/// @brief Searches an entire one-dimensional sorted array for a specific element, using the IComparable interface implemented by each element of the array and by the specified object.
/// @param array The sorted one-dimensional array_ to search.
/// @param value The object to search for.
/// @return int32 The index of the specified value in the specified array, if value is found; otherwise, a negative number. If value is not found and value is less than one or more elements in array, the negative number returned is the bitwise complement of the index of the first element that is larger than value. If value is not found and value is greater than all elements in array, the negative number returned is the bitwise complement of (the index of the last element plus 1). If this method is called with a non-sorted array, the return value can be incorrect and a negative number could be returned, even if value is present in array.
/// @exception RankException array is multidimensional.
/// @exception ArgumentException value is of a type that is not compatible with the elements of array.
/// @exception InvalidOperationException value does not implement the IComparable interface, and the search encounters an element that does not implement the IComparable interface.
/// @remarks This method does not support searching arrays that contain negative indexes. array must be sorted before calling this method.
/// @remarks If the array_ does not contain the specified value, the method returns a negative integer. You can apply the bitwise complement operator ~ to the negative result to produce an index. If this index is one greater than the upper bound of the array, there are no elements larger than value in the array. Otherwise, it is the index of the first element that is larger than value.
/// @remarks Either value or every element of array must implement the IComparable interface, which is used for comparisons. The elements of array must already be sorted in increasing value according to the sort order defined by the IComparable implementation; otherwise, the result might be incorrect.
/// @note Ifvalue does not implement the IComparable interface, the elements of array are not tested for IComparable before the search begins. An exception is thrown if the search encounters an element that does not implement IComparable.
/// @remarks Duplicate elements are allowed. If the array_ contains more than one element equal to value, the method returns the index of only one of the occurrences, and not necessarily the first one.
/// @remarks null can always be compared with any other reference type; therefore, comparisons with null do not generate an exception.
/// @note For every element tested, value is passed to the appropriate IComparable implementation, even if value is null. That is, the IComparable implementation determines how a given element compares to null.
/// @remarks This method is an O(log n) operation, where n is the Length of array.
//template<typename type_t, typename allocator_t>
//static xtd::size binary_search(const array_<type_t, 1, allocator_t>& array, const type_t& value) {return binary_search(array, 0, array.Length, value, xtd::collections::generic::comparer<type_t>::default_comparer.release());}

/// @brief Searches a range of elements in a one-dimensional sorted array for a value, using the specified IComparer interface.
/// @param array The sorted one-dimensional array_ to search.
/// @param value The object to search for.
/// @param comparer The IComparer implementation to use when comparing elements. -or- null to use the IComparable implementation of each element.
/// @return int32 The index of the specified value in the specified array, if value is found; otherwise, a negative number. If value is not found and value is less than one or more elements in array, the negative number returned is the bitwise complement of the index of the first element that is larger than value. If value is not found and value is greater than all elements in array, the negative number returned is the bitwise complement of (the index of the last element plus 1). If this method is called with a non-sorted array, the return value can be incorrect and a negative number could be returned, even if value is present in array.
/// @exception RankException array is multidimensional.
/// @exception ArgumentOutOfRangeException index is less than the lower bound of array. -or- length is less than zero.
/// @exception ArgumentException index and length do not specify a valid range in array. -or- value is of a type that is not compatible with the elements of array.
/// @exception InvalidOperationException value does not implement the IComparable interface, and the search encounters an element that does not implement the IComparable interface.
/// @remarks This method does not support searching arrays that contain negative indexes. array must be sorted before calling this method.
/// @remarks If the array_ does not contain the specified value, the method returns a negative integer. You can apply the bitwise complement operator ~ to the negative result to produce an index. If this index is one greater than the upper bound of the array, there are no elements larger than value in the array. Otherwise, it is the index of the first element that is larger than value.
/// @remarks The comparer customizes how the elements are compared. For example, you can use a System.Collections.CaseInsensitiveComparer as the comparer to perform case-insensitive string searches.
/// @remarks If comparer is not null, the elements of array are compared to the specified value using the specified IComparer implementation. The elements of array must already be sorted in increasing value according to the sort order defined by comparer; otherwise, the result might be incorrect.
/// @remarks If comparer is null, the comparison is done using the IComparable implementation provided by the element itself or by the specified value. The elements of array must already be sorted in increasing value according to the sort order defined by the IComparable implementation; otherwise, the result might be incorrect.
/// @note If comparer is null and value does not implement the IComparable interface, the elements of array are not tested for IComparable before the search begins. An exception is thrown if the search encounters an element that does not implement IComparable.
/// @remarks Duplicate elements are allowed. If the array_ contains more than one element equal to value, the method returns the index of only one of the occurrences, and not necessarily the first one.
/// @remarks null can always be compared with any other reference type; therefore, comparisons with null do not generate an exception when using IComparable.
/// @note For every element tested, value is passed to the appropriate IComparable implementation, even if value is null. That is, the IComparable implementation determines how a given element compares to null.
/// @remarks This method is an O(log n) operation, where n is length.
//template<typename type_t, typename allocator_t>
//static xtd::size binary_search(const array_<type_t, 1, allocator_t>& array, const type_t& value, const xtd::collections::generic::icomparer<type_t>& comparer) {return binary_search(array, 0, array.Length, value, comparer);}
/// @}
};
}

0 comments on commit 456300c

Please sign in to comment.