can i use equal_range to get range of values or end of container? - boost-multi-index

i know that equal_range(k) gets me range of values:
starting with k or element greater than k or end and
finishing with element greater than k or end.
i wanna modify this behaviour to give me only range of values :
starting with k or end
finishing with element greate than k or end
this is to reach result that if the range is not present in container then return iterators to end so that i could compare them to end of container.
how can i do this???
i tried to use range function ?
but when i pass make_tuple forming composite key to lower_bound it gives me huge errors so that i give up

i found that if i use the comparison between the two iterators resulting from equal_range then they are equal if the value used for equal_range is not present in container so the lower_bound of value is the value greater than it and the upper_bound is also the value greater than it so value is not present.
and if the value is present then the 2 iterators are not equal .
and i should not use end iterator.

Related

Exponentiation conversion

I am using:
For t = 1 To ActiveProject.Tasks.Count
With Tasks(t)
' DurSen is a Custom Field Name. It returns an application field Number (such as 188743770 for Number4)
Projectfield = FieldNameToFieldConstant("DurSen")
' the SpearmanRankCorrelation function occasionally produces a result like -6.06060606060606E-03.
.SetField (Projectfield), SpearmanRankCorrelation(ProjectDuration, TmpVec, NN)
End With
Next t
This is a number expressed as an exponent. Due to the presence of the "E" in the number, it fails the Setfield function. The .Setfield places the calculated value into the field referenced by ProjectField for a specific task, (t) but fails when the calculated value contains an "E".
Any ideas on avoiding this problem?
The SetField method expects the Value parameter to be a string. So the solution is to format the number as a string. (e.g. Format(-6.06060606060606E-03, "0.0#####") => -0.006061)
However, Microsoft Project will round the value to the nearest hundredth, so you will lose the precision.
Better to store numbers that require more than two decimal places in a text field and convert them back to numbers as needed in code.
I had a simpler answer hat I figured out yesterday. Instead of trying to deal with it as a number, I simply converted to a string, used the InStr method to see if an "E" was present and if it is, assign a value of 0.001 as the result, as any E value will be smaller than .01.
If InStr(SpearmanRankCorrelation(ProjectDuration, TmpVec, NN), "E") > 0 Then
.SetField (Dursen), 0.001
PD = 0.001
outputstr = ("575 - ID= " & tasks(t).ID & " SpearmanRankCorrelation= " & SpearmanRankCorrelation(ProjectDuration, TmpVec, NN) & " Replaced by 0.001")
Call Txt_Append(myfile, outputstr)
Else
.SetField (Dursen), SpearmanRankCorrelation(ProjectDuration, TmpVec, NN)
End If
Thank you, Rachel, for your answer. I am trying not to use Project Custom fields. The .Setfield above assigns the Result to the field which is identified by the value of Dursen ( which is an internal field reference).
Rachel, I tried your approach but whenever I encountered a result that was expressed with an "E" in the Result, the conversion failed. using >Setfield will only round to the nearest hundreth if the entire string is a numeric value (when converted). The >Setfield method does not work when there is an "E" in the expression.

Get index for minimum value

I'm trying to find the index of the minimum value inside an Array List in VB.net. I'll then use this index to split two array list at the same location.
I found the minimum value, but can't figure out how to get this value index.
Dim smallest As Double = Double.MaxValue
For Each element As Double In MyArray
smallest = Math.Min(smallest, element)
Next
Dim splitpoint = MyArray.IndexOf(smallest)
Console.WriteLine(smallest)
Console.WriteLine(splitpoint)
I get a result of -1 for split point which is not right. I'm sure this isn't the most efficient way to find the min value using with a loop.
Any help would me massive.
Thanks

Excel/VBA: Cannot get Variant/Boolean to return (should be trivial)

I cannot get this function to return my values to the output column in excel:
To overcome some intense lookup tables and speed up computation, I am using a pivot table with slicers to output row numbers from filtering. These rows then need to be converted into a column of true/false cells for a large table from which I then want to perform more calculations. To avoid lookups or matching I simply need to step through the list of rows and turn those cells to "true" in the output vector.
Function IncludedinSlicer(input_range As Variant) As Variant
Dim n As Long, j As Long, r As Long
n = input_range.Height ' Height of the column of reference values
' every row in the input_range contains a row number which in the output should be TRUE
' all other rows should be false
Dim output_range As Variant
ReDim output_range(1 To 300000)
' This covers the maximum number of rows
' Initialise all rows to FALSE
For j = 1 To 300000
output_range(j) = False
Next j
' Set only those rows listed in the reference to TRUE
For j = 1 To n
r = input_range(j).Value
If r = 0 Then ' If r=0 then we are beyond the end of the reference table and have captured some blank rows
Exit For ' Exit, to avoid outside-of-array errors
Else
output_range(r) = True
End If
'End If
Next j
' Return results to Excel
' THIS LAST BIT DOES NOT RETURN VALUES TO EXCEL
IncludedinSlicer = output_range
End Function
I know this should be trivial but somehow it has vexed me for literally hours. Please help! Thank you in advance!
EDIT: Found the issue!
First, thank you for pointing out the difference between Height and Rows.Count as I was not aware of this.
Unfortunately, that still left me with the same error in the final cell output (#Value). Luckily in the meantime I tried processing this via Matlab instead and when passing back the results I got the same error. This allowed me to narrow down the problem and I tracked the error to ... (drum roll) ... VBA's 2^16 limit for array size.
My table has close to 2^18 rows so this causes the error.
input_range.Height refers to the literal height in pixels of the range. Try instead input_range.Rows.Count to get the number of rows in the range.
For your function, you should be passing in (and potentially returning) a Range type instead of the ambiguous Variant type.
This will give you direct access to all of the properties of the Range type (i.e. Rows, Columns, Cells) and would probably make it easier to catch your issue that bobajob pointed out.

Absolute reference with R1C1 using a variable to dictat row number (VBA)

I have a code that creates a number of separate lists under each other. For each of these lists I'm running a for loop to assign an equation to each row at the end column. This formula should multiply the neighbour cell (a relative reference) with cell at the top of that particular list (an absolute referance). The problem is that the lists are of arbitrary length and generated earlier in the code, so I can't assign the absolute referance beforhand.
I was thinking on saving the row number (row 1 = 1, row 2 = 2 etc) in a variable and then use the variable in the R1C1 notation (= "R(variable)C5*RC[-1]), but I cant seem to get this to work... The variable will be the same throughout the for loop given in the example below, but will change next time the same for loop is entered.
Is this even possible?
(I know the parantheses in the R1C1 are not the proper notation, but this is to show where I want my variable)
...
variable = 3
For i = 1 to count
last = ActiveSheet.Cells(Rows.Count, "C").End(xlUp).Row + 1
Cells(siste, "E").FormulaR1C1 = "=R(variable)C5*RC[-1]"
Next
Just one small change:
Cells(siste, "E").FormulaR1C1 = "=R" & variable & "C5*RC[-1]"

Visual Basic in Excel - WorksheetFunction.max

What is the behavior of this function to be when the values being compared are equal?
If
Cell1 = 10
Cell2 = 11
Debug.Print WorksheetFunction.max(Cell1,Cell2)
Will return Cell2. But if
Cell1 = 10
Cell2 = 10
It returns Cell1.
What am I missing here?
UPDATE:
I have a label at Offset(0,-3)
If one value is greater then I get the appropriate label.
When the values are equal I get on label in particular.
Since I have not defined a consequence when the vales are equal, I wonder what I am missing
about the behavior of this function such that it will choose one value over the other when they are equal.
How much more coding should I assume is necessary?
Function testWinner(rng As Range) As String
testWinner = WorksheetFunction.Index(rng, WorksheetFunction.Match(WorksheetFunction.max(rng), rng, 0)).Offset(0, -3)
End Function
It's nothing to do with Max (now that you've edited your question). Max simply returns the value (since they are all the same) and your Match function is looking for an exact match so it scans through the range until it finds the first match then that position is passed to Index.
WorksheetFunction.Max does not return a cell/range. It returns the largest value in a set of value.
By the way: in case of when both values are equal (10,10), the result of Min, Max and Average function will be the same: 10.
If by saying the behaviour of function you mean how it works, it sorts data and gets the highest value. So, in case of set of value: {10, 10, 8}
Max(10,10,8)** = 10
Max.K({10,10,8},2) = 10 'where K = 2, which means the second highest value
Max.K({10,10,8},3) = 8 'where K = 3, which means the third highest value
On the other hand...
row/column A B
1 10 =RANK(A$1:A$3,A1) 'returns 1
2 10 =RANK(A$1:A$3,A2) 'returns 1
3 8 =RANK(A$1:A$3,A3) 'returns 3
Is it more understandable, now?
In your updated example the following happens in your testWinner function:
The Max value is determined: in example one this is 15 (in example 2 it's 14). Note that only the value but not a reference to any cell is returned
The MATCH function searches through through D2:D3 for the value 15 (14). It does so top down and return the first exact match. I.e. in the first example, this results in 1 (in the second example the result is 2)
INDEX will return the 2nd element of D2:D3 (the 1st in the second example), i.e. cell D2 (D3 in ex. 2)
OFFSET returns the cell three to the left
Thus, the answer to your question is not the return of the MAX function - but the MATCH function - which goes top to bottom until it finds the first element matching your criteria, in your case the max.
Independent of this: why do you use a User-Defined-Function for this? If you'd build this with the normal Excel functions, performance would be higher - and non-VBA users could still understand it. Using =INDEX($A$2:$A$3,MATCH(MAX($D$2:$D$3),$D$2:$D$3,0)) you would save the OFFSET - and make it less error prone, as you can now insert columns between A and D without breaking your model!