What is the time complexity of Array.enumerated() in swift? - swift

I used enumerated() quite often in my code like:
for (index, element) in array.enumerated() {
// ...
}
If enumerated() takes O(n) time then the above execution will take O(2n).
I am confused here since Apple's documentation doesn't provide any info of time complexity.
Will this cause O(2n) or just O(n)?

Swift is open source now and you can get this info from public sources.
From the GitHub.
Method description.
/// Returns a sequence of pairs (*n*, *x*), where *n* represents a
/// consecutive integer starting at zero and *x* represents an element of
/// the sequence.
///
/// This example enumerates the characters of the string "Swift" and prints
/// each character along with its place in the string.
///
/// for (n, c) in "Swift".enumerated() {
/// print("\(n): '\(c)'")
/// }
/// // Prints "0: 'S'"
/// // Prints "1: 'w'"
/// // Prints "2: 'i'"
/// // Prints "3: 'f'"
/// // Prints "4: 't'"
///
/// When you enumerate a collection, the integer part of each pair is a counter
/// for the enumeration, but is not necessarily the index of the paired value.
/// These counters can be used as indices only in instances of zero-based,
/// integer-indexed collections, such as `Array` and `ContiguousArray`. For
/// other collections the counters may be out of range or of the wrong type
/// to use as an index. To iterate over the elements of a collection with its
/// indices, use the `zip(_:_:)` function.
///
/// This example iterates over the indices and elements of a set, building a
/// list consisting of indices of names with five or fewer letters.
///
/// let names: Set = ["Sofia", "Camilla", "Martina", "Mateo", "Nicolás"]
/// var shorterIndices: [SetIndex<String>] = []
/// for (i, name) in zip(names.indices, names) {
/// if name.count <= 5 {
/// shorterIndices.append(i)
/// }
/// }
///
/// Now that the `shorterIndices` array holds the indices of the shorter
/// names in the `names` set, you can use those indices to access elements in
/// the set.
///
/// for i in shorterIndices {
/// print(names[i])
/// }
/// // Prints "Sofia"
/// // Prints "Mateo"
///
/// - Returns: A sequence of pairs enumerating the sequence.

To be short its O(1).
It's very well documented. You can get that info even in Xcode directly.

Related

How do I use STL std::list with objects?

I want to create linked lists of objects sorted by an object attribute (physical size); but so far it seems I will have to code them myself...
These lists will be short, typically a dozen nodes each; but I may have up to a thousand lists; so I can't afford the extra weight of using std::map's. In fact, I'd be quite happy with single linked list.
But I need the node to be more than just a value.
The key value in my objects is rarely going to change; however elements will have to come out of one list and move to another quite often.
((Actual use: One list per quad, in a quad-tree (as for collision detection, etc); objects sorted by size, as the larger objects are less numerous but need to be picked quickly from larger ranges, so they should come up first in the lists.))
But every example I find for using std::list to maintain sorted lists uses a list of integers as the example; but that's not very useful; what I have is objects that have one value member to be sorted by.
I was thinking of using lower_bound to find the insertion point, then insert the object; but the lower_bound iterator takes a begin, and end, and a plain value as the third argument; I see no mechanism by which I can specify to use a particular member of my objects to sort by.
I could, of course, define a conversion operator,
my_object_type::int(){ return sortby; }
Would that work? Is there a better way?
I seem to have found my answer in this reference:
https://www.geeksforgeeks.org/lower_bound-in-cpp/
under "Syntax 2"; there is provision for a fourth
argument being a comparison functor. So, something
like this should work (not tested yet):
class pnt_proxy
{
int x; //position
int y;
point* real_pnt; //the real point this represents
public:
float sz; //rough largest diagonal across object
}
class pproxy_cmp : public std::binary_function< pnt_proxy, pnt_proxy, bool >
{
public:
bool operator()( pnt_proxy const & a, pnt_proxy const & b ) const
{
return a.sz < b.sz;
}
};
std::list< pnt_proxy > ll;
void insert_sorted( pnt_proxy const & pp )
{
if( ll.size() )
{
std::list<pnt_proxy>::iterator insert_at;
insert_at =
std::lower_bound( ll.begin(), ll.end(), pp, pproxy_cmp() );
ll.insert( insert_at, pp );
}
else ll.push_back( pp );
}

How to put Doc comments for Dart function parameters?

We can easily put Doc comments for Dart Class variables e.g.
class SomeClass {
/// Class variable Doc Comment.
var someVariable;
}
How can I do the same for Dart Function parameters e.g. I tried this
void someFunction(
{/// Function parameter documentation
String funParameter="Some Default Value"}
) {
}
But it's not showing anything. If it's not possible please suggest me any alternative.
It is against the Effective Dart conventions to document parameters of functions using a direct syntax like that. Instead, use prose to describe the parameter and how it relates to the function's purpose.
// Instead of this
/// someFunction
/// #funParameter Does something fun
void someFunction({ String funParameter="Some Default Value" }) ...
// Or this
/// someFunction
void someFunction({
/// Does something fun
String funParameter="Some Default Value"
}) ...
// Do this
/// Does something fun with the [funParameter].
void someFunction({ String funParameter="Some Default Value" }) ...
Here's perhaps a more practical example:
/// Takes the values [a] and [b] and returns their sum. Optionally a
/// third parameter [c] can be provided and it will be added to the
/// sum as well.
int add(int a, int b, [int c = 0]) ...
You should use the doc comment like this:
/// the function uses [funParameter] to do stuff
void someFunction({String funParameter = "Some Default Value"}) {
// ..
}

Documentation comment for multi-return-value functions in Swift3

Swift3 supports functions with multiple return values, for example
func foo(param1: Int, param2: Double) -> (retA: String, retB: [Int]) {
// ...
}
when creating documentation comment for this function, we can use
/// function `foo` .....
///
/// - Parameters:
/// - param1: xxx
/// - param2: xxx
to create statements of param1 and param2. Then, how to create statements for retA and retB? If I use
/// - Returns:
/// - retA: xxx
/// - retB: xxx
the quick help just treat retA and retB as two Markdown items, and cannot get the same effect with param1 and param2.
I don't believe it's possible to get what you're trying to do.
This is because although you can use multiple return types, it's internally transformed into a tuple (which is a single value). Taking this into account and the fact that the quickhelp documentation says:
The Parameters section lists the parameters for a method or function.
The Returns section documents any return value for a method or function.
Notice how the parameters is shown as a plural while the return is a single value (in this case a tuple).
You might have better luck simply formatting the return using markdown to give it a nicer format.

Doxygen warning "does not have an associated number"

I am using doxygen 1.8.9.1 and bibtex to generate the reference list. It looks doxygen (or bibtex) don't support longer key.
For example, my bib file looks like
#incollection{mcmaster_phenology_,
title = {The {DSSAT} cropping system model},
volume = {18},
journaltitle = {European Journal of Agronomy},
author = {A, F.},
}
#article{mcmaster_phenology_1,
title = {The {DSSAT} cropping system model},
volume = {18},
journaltitle = {European Journal of Agronomy},
author = {A, F.},
}
My text.cs file looks
namespace Test
{
/// <summary>
/// A test class
/// </summary>
/// <remarks>
/// This is test citation, see \cite mcmaster_phenology_ and
/// \cite mcmaster_phenology_1.
/// </remarks>
public class TestClass
{
}
}
The warning message is:
warning: \cite command to 'mcmaster_phenology_1' does not have an associated number
The final html is
This is test citation, see [1] and [mcmaster_phenology_1].
Seems the maximum length of key is 19 characters. It looks a bug for me. How should I fix it? Thank for any suggestions.

How to buffer an IPoint or IGeometry? (How to do buffered intersection checks on an IPoint?)

How would I buffer an IPoint to do an intersection check using IRelationalOperator?
I have, for arguments sake:
IPoint p1 = xxx;
IPoint p2 = yyy;
IRelationalOperator rel1 = (IRelationalOperator)p1;
if (rel.Intersects (p2))
// Do something
But now I want to add a tolerance to my check, so I assume the right way to do that is by either buffering p1 or p2. Right?
How do I add such a buffer?
Note: the Intersects method I am using is an extension method I wrote to simplify my code. Here it is:
/// <summary>
/// Returns true if the IGeometry is intersected.
/// This method negates the Disjoint method.
/// </summary>
/// <param name="relOp">The rel op.</param>
/// <param name="other">The other.</param>
/// <returns></returns>
public static bool Intersects (
this IRelationalOperator relOp,
IGeometry other)
{
return (!relOp.Disjoint (other));
}
OK, I found the answer. Or an answer.
Use the ITopologicalOperator interface.
IPoint p1 = xxx;
IPoint p2 = yyy;
ITopologicalOperator topoOp = (ITopologicalOperator)p2 ;
IGeometry p2Bufferd = topoOp.Buffer (bufferSize);
IRelationalOperator rel1 = (IRelationalOperator)p1;
if (rel.Intersects (p2Bufferd))
// Do something