CGAL::Surface_mesh - Accessing face/vertex using an integer index? - c

From the user manual of the CGAL Surface_mesh class:
the data structure uses integer indices as descriptors for vertices,
halfedges, edges and faces
I am interested in accessing a certain face/edge/vertex based on it's integer index, but cannot find how this is done.
Iterators obviously work, but I don't want to iterate a known number of times just to get to the relevant face_index/vertex_index if I can access based on an integer face/vertex index known a-priori.
Can someone please explain how (if actually possible) to use the integer indexing if I want to access the i-th face "directly" (without iterating)?

The following should work:
unsigned int i = 33;
Surface_mesh::Face_index fi(i);

Related

Access array elements from string argument in Modelica

I'm having a task in Modelica, where within a function, I want to read out values of a record (parameters) according to a given string type argument, similar to the dictionary type in Python.
For example I have a record containing coefficicents for different media, I want to read out the coefficients for methane, so my argument is the string "Methane".
Until now I solve this by presenting a second array in my coefficients-record storing the names of the media in strings. This array I parse in a for loop to match the requested media-name and then access the coefficients-array by using the found index.
This is obviously very complicated and leads to a lot of confusing code and nested for loops. Isn't there a more convenient way like the one Python presents with its dictionary type, where a string is directly linked to a value?
Thanks for the help!
There are several different alternatives you can use. I will add the pattern I like most:
model M
function index
input String[:] keys;
input String key;
output Integer i;
algorithm
i := Modelica.Math.BooleanVectors.firstTrueIndex({k == key for k in keys});
end index;
constant String[3] keys = {"A","B","C"};
Real[size(keys,1)] values = {1,2*time,3};
Real c = values[index(keys,"B")] "Coefficient";
annotation(uses(Modelica(version="3.2.1")));
end M;
The reason I like this code is because it can be made efficient by a Modelica compiler. You create a keys vector, and a corresponding data vector. The reason it is not a record is that you want the keys vector to be constant, and the values may vary over time (for a more generic dictionary than you wanted).
The compiler can then create a constant index for any constant names you want to lookup from this. This makes sorting and matching better in the compiler (since there are no unknown indexes). If there is a key you want to lookup at run-time, the code will work for this as well.

how to write order preserving minimal perfect hash for integer keys?

I have searched stackoverflow and google and cant find exactly what im looking for which is this:
I have a set of 4 byte unsigned integers keys, up to a million or so, that I need to use as an index into a table. The easiest would be to simply use the keys as an array index but I dont want to have a 4gb array when Im only going to use a couple of million entries! The table entries and keys are sequential so I need a hash function that preserves order.
e.g.
keys = {56, 69, 3493, 49956, 345678, 345679,....etc}
I want to translate the keys into {0, 1, 2, 3, 4, 5,....etc}
The keys could potentially be any integer but there wont be more than 2 million in total. The number will vary as keys (and corresponding array entries) will be deleted but new keys will always be higher numbered than the previous highest numbered key.
In the above example, if key 69 was deleted, then the hash integer returned on hashing 3493 should be 1 (rather than 2) as it then becomes the 2nd lowest number.
I hope I'm explaining this right. Is the above possible with any fast efficient hashing solution? I need the translation to take in the low 100s of nS though deletion I expect to take longer. I looked at CMPH but couldn't find any usage examples that didn't involved getting the data from a file. It needs to run under linux and compiled with gcc using pure C.
Actually, I don't know if I understand what exactly you want to do.
It seems you are trying to obtain the index number in the "array" (or "list") of sequentialy ordered integers that you have stored somewhere.
If you have stored these integer values in an array, then the algorithm that returns the index integer in optimal time is Binary Search.
Binary Search Algorithm
Since your list is known to be in order, then binary search works in O(log(N)) time, which is very fast.
If you delete an element in the list of "keys", the Binary Search Algorithm works anyway, without extra effort or space (however, the operation of removing one element in the list enforces to you, naturally, to move all the elements being at the right of the deleted element).
You only have to provide three data to the Ninary Search Algorithm: the array, the size of the array, and the desired key, of course.
There is a full Python implementation here. See also the materials available here. If you only need to decode the dictionary, the simplest way to go is to modify the Python code to make it spit out a C file defining the necessary array, and reimplement only the lookup function.
It could be solved by using two dynamic allocated arrays: One for the "keys" and one for the data for the keys.
To get the data for a specific key, you first find in in the key-array, and its index in the key-array is the index into the data array.
When you remove a key-data pair, or want to insert a new item, you reallocate the arrays, and copy over the keys/data to the correct places.
I don't claim this to be the best or most effective solution, but it is one solution to your problem anyway.
You don't need an order preserving minimal perfect hash, because any old hash would do. You don't want to use a 4GB array, but with 2 MB of items, you wouldn't mind using 3 MB of lookup entries.
A standard implementation of a hash map will do the job. It will allow you to delete and add entries and assign any value to entries as you add them.
This leaves you with the question "What hash function might I use on integers?" The usual answer is to take the remainder when dividing by a prime. The prime is chosen to be a bit larger than your expected data. For example, if you expect 2M of items, then choose a prime around 3M.

maximum size of array in vb.net

So I am working on a problem where I am dealing with very large amounts of data and I have come across a limitation I do not fully understand. I need to store sets of 6 integer values and associate each with an index. The approach I chose was to initially create my own type and then create a List(of Type). That failed with an 'Array dimensions exceeded supported range" error. Fine, I presumed that this was due to the Type I defined and perhaps the way the List/Collection was storing the data. I was expecting to make use of the full Integer.MaxValue number of indices in an array, as given in http://msdn.microsoft.com/en-us/library/wak0wfyt.aspx#BKMK_ArraySize but that seems to not apply (why?). I then proceeded to re-write the functions and ended up with an array of type Tuple(int,int,int,int,int,int). But again, I run into the same situation. Same for arrays of a type that has an array as its variable. I tried out several ways to see what the maximum size of the array could be and ended up with a maximum size of around 48E6 indices. The problem is that I need more than 10x that to store the data I have...
The only way I found to make this (sort of) work is to use a List(of List(of Integer())) and then add a new item to the top level list after every 40M indices or so. Nasty solution and not efficient, but it showed that it could be made to work...
Background: VS2010, .NET 4.0, Win7 x64, 32GB Ram.
Any ideas of how I would best store 6 integer values in either a collection or array (I need to be able to access them by index) for more than about 500 million combinations (ideally up to the 2.1B combinations)?
Thanks
The solution is actually quite simple (thanks coffee). Reading through the documentation in the link above, this should not be the problem, but... the maximum size of the array is no longer Int.MaxValue once the type isn't an integer (or so it would seem, though none of the documentation indicates this). The way around this is simply to go from something like this:
Dim _Array(Array_Size) as Tuple(of Integer,Integer,Integer,Integer,Integer,Integer)
to
Dim _Array1(Array_Size) as Integer
Dim _Array2(Array_Size) as Integer
Dim _Array3(Array_Size) as Integer
Dim _Array4(Array_Size) as Integer
Dim _Array5(Array_Size) as Integer
This allows each array the maximum size (or at least the size I need which is close enough to the max size). The only thing is that I then need to expand the rest of the code accordingly.
I am a bit surprised about this, considering that the MSDN states that 'The length of every dimension of an array is limited to the maximum value of the Integer data type' when it looks like it should actually read that 'The Total length [...] is limited to the maximum value'. That would explain that I receive an error (of the original statement) at a size that accounts for the additional 6 integer values plus some for accounting.

How to approximate LUT index?

I have an array
...
//a b
{860, -30},
{853, -29},
{846, -28},
{838, -27},
{830, -26},
{822, -25},
{814, -24},
...
What is the quickest way using C to find find b with given a value? I guess some approximation is required for that? For example when a = 851 I would like to find -29 as quick as possible.
The fastest general purpose algorithm is a binary search. Depending on the size of the mapping array, you might consider hand-coding the search; that might be plausible for size 32, but I wouldn't go much larger. On a micro-controller, the fully-expanded binary search might be 50% faster, if you're lucky.
But if the mapping is not too non-linear, there's a nice alternative.
Divide the range of a into k equal-sized ranges, where k is not much bigger than the number of entries in the mapping array, such that the mappings of each range endpoint is either the same as or one more than the next range endpoint. (If this is possible; that's precisely what I meant by "not too non-linear"). Create another array which maps each endpoint into an index into the original array. (You only need the indices, not the endpoints, because the endpoints are evenly spaced.) For each range, the bottom endpoint's corresponding index value is the index of the smallest a value in the original array not less than the range's top endpoint. Note that because of the requirement presented above, there can be at most one a value in every range, so the index of each endpoint will always point to the a value for the end of the range, and the a value for the beginning of the range will always be either the same or the previous index.
Now, to look up a value, you first figure out the appropriate range index, which is a simple linear computation ((val - minval)/k) and then compare the value with the indicated a value by looking up the index for the comparison. If the value is less than the looked up a, then subtract one from the index. Then return the b value from the index.
For an example of such an algorithm, see my answer here.

CS theory problem: evaluating each element in an array only once and choosing the largest value

We covered this problem in a theory class back in college.
The setup is this:
You're presented with an array of N values. You know the length of the array, but not the range of values. You are presented the elements one at a time. Each value can be examined only once, and if the value is not chosen when presented it is discarded. The goal is to choose the maximum value.
There is an algorithm that gives a better than 1/N chance of choosing the maximum, but I can't for the life of me recall what it is.
It's called the secretary problem.
The Simplest way I know of is to iterate through the array. You create a variable, insert the first array value into it, then as you iterate through each value in the array you compare the stored value to the current value and if the current value is larger than the stored value you put the current value into the stored value, replacing the lower value.
The syntax will change based on what programming language you use but a basic representation would be the following:
Dim MaxValue as integer
Dim ArrayOfN as Array
Dim N as integer
Dim i as integer
ArrayOfN = [0,10,87,6,59,1200,5] 'In this case there are 7 values in the array
N = 7 'You would typically want to programically get the size of the array but I can't remember the syntax
MaxValue = ArrayOfN(0)
For i = 0 to N - 1 'Assumes iteration of 1
If MaxValue < ArrayOfN(i) Then
MaxValue = ArrayOfN(i)
End IF
Next
I largely used vb for the coding example but I am aware that some of the syntax is incorrect, especially for the array. However since I don't have access currently to ay coding software that is the best I can do. When coding something you can definitively choose the largest value, there isn't any uncertainty. There are more effient ways of doing this as well. For example if you have the ability to use an array.sort feature then you can get the max value in just 2 lines of code. However since you did not state any programing languages, and after seeing yi_H's answer, I am starting to think that you did not want to know how to do program a solution but simply wanted the name of the problem.
See..
this is a kind'a bit mess using such strategy to retrieve the maximum
as you can use any of advanced algorithms like fibonacci search or another
anyways, here we are certainly checking the elements one by one..
so to make it happen flag up a variable with the max available till know
and iterate it along the length of array linearly..
it may be resolved like this
Arr[n]={ }
int max=0
for x in range 0 to n-1
if max>Arr[x]
max=Arr[x]
[END IF]
get(max)
so finally we will be getting the maximum
but on the cost of high time complexity.
try to use divide and conquer kind of algos...

Resources