Am I overcomplicating this? Since I recently learned that with std::vector you can use the [] operator and it will add the entry if missing.
I have something a little more detailed:
using WeekendFutureItemHistMap = std::map<CString, std::vector<COleDateTime>>;
using WeekendFutureHistMap = std::map<WeekendHistAssign, WeekendFutureItemHistMap>;
WeekendHistAssign is just an enum class.
In my function I am populating it this way:
if (m_mapScheduleFutureHist[eHistAssign].find(strName) != m_mapScheduleFutureHist[eHistAssign].end())
m_mapScheduleFutureHist[eHistAssign][strName].push_back(datAssign);
else
{
m_mapScheduleFutureHist[eHistAssign].emplace(strName, std::vector<COleDateTime>{datAssign});
}
According to the std::vector operator[] it states:
Returns a reference to the element at specified location pos. No bounds checking is performed.
As a result it seemed the right thing to do is test for the existing first as done.
Did I overcomplicate it?
std::vector<int> v;
v.resize(100);
v[0]=1;
v[1]=10;
...
Related
I have a array T* which is created like the following
T* array = new T[len];
// Some initialization codes
We know we can delete the array like
delete [] array;
However, I am writing a Rust FFI program with C++. I have sent this array pointer to the Rust part of my code, and I want to delete the code now.
So I can implement a function ffi_delete_array_of_T on the C++ part, and the Rust part code will call the function through FFI, attached with the argument array.
void ffi_delete_array_of_T(void * arr) {
delete [] reinterpret_cast<T *>(arr);
}
However, I already have a function which can delete single element of T.
void ffi_delete_pointer_of_T(void * arr) {
delete reinterpret_cast<T *>(arr);
}
These two functions are so similar that I wonder if I can combined them into one, so I don't need to write two copy.
void ffi_delete_whatever_of_T(void * arr, bool is_array) {
if (is_array)
delete [] reinterpret_cast<T *>(arr);
else
delete reinterpret_cast<T *>(arr);
}
However, I still think the above codes redundant. I wonder if I only give ffi_delete_pointer_of_T, is it possible the Rust do some magic, and have ffi_delete_array_of_T for me?
To the best of my knowledge, it is not possible. C++ can delete [] a array without hint of length, because it has already stored the length of array somewhere when array is allocated. However, the Rust part don't know the actual length of this array.
However, I still wonder if there are some other ways to do that?
Trying to fix a HH complain... Basically the code is doing something similar to this
Sfirstgroup = idx($largegroup, "first");
$final_thing = null
if(HH\is_any_array(Sfirstgroup) && Sfirstgroup){
/*HH_FIXME[4110] Error revealed by is_arry refining to varray_or_darray */
$final_thing = idx(Sfirstgroup[0],"final_thing")
}
I think it must have some thing to with firstgroup[0] doesn't have a collection type. but couldn't figure out how to fix this...
Thanks a lot!
You have two type errors:
Indexing with square bracket syntax $firstgroup[0] doesn't work very well with the inferred type from is_any_array, which infers wildcard generics for the key type (i.e. it's a KeyedContainer<_,_>). Either change $a[0] to idx($a, 0) or use is_vec_or_varray if all your keys are int, since it refines to vec<_>.
You need to prove $firstgroup[0] is a KeyedContainer to use your second idx call for $final_thing. The refinement checks like is_any_array won't work on an indexed variable (like $firstgroup[0]), so you have to make a temporary variable to refine it.
All in all, here's one possible solution:
$firstgroup = idx($largegroup, "first");
$final_thing = null;
if(HH\is_vec_or_varray($firstgroup)){
$first_item = $firstgroup[0];
if(HH\is_any_array($first_item)) {
$final_thing = idx($first_item,"final_thing");
}
}
I am trying to write a class in Haxe supporting array like access using the [] operator such as:
var vector = new Vec3();
trace(vector.length); // displays 3
vector[0] = 1; // array like access to the class, how?
vector[1] = 5.6; // more array access
vector[2] = Math.PI; // yet more array access
The problem is I don't know how to define a class such that it allows the [] operator. I need this class, rather than using an Array<Float> or List<Float> because there is some trickery going on with it to support my animation system which references to parts of vectors using storyboards (see http://www.youtube.com/watch?v=ijF50rRbRZI)
In C# i could write:
public float this[index] { get { ... } set { .... } }
I've read the Haxe documentation and found ArrayAccess<T>, but the interface is empty. That is I don't understand how to implement it, or if I just implement ArrayAccess<Float> ... what method on my class would be called to retrieve Float at said index?
Haxe doesn't support operators overload (yet) so you will have to use a get/set pair. You can use inline if the magic that happens inside your methods need to be optimized for speed.
Preface: I'm working with Processing and I've never used Java.
I have this Processing function, designed to find and return the most common color among the pixels of the current image that I'm working on. the last line complains that "The method color(int) in the type PApplet is not applicable for the arguments (String)." What's up?
color getModeColor() {
HashMap colors = new HashMap();
loadPixels();
for (int i=0; i < pixels.length; i++) {
if (colors.containsKey(hex(pixels[i]))) {
colors.put(hex(pixels[i]), (Integer)colors.get(hex(pixels[i])) + 1);
} else {
colors.put(hex(pixels[i]),1);
}
}
String highColor;
int highColorCount = 0;
Iterator i = colors.entrySet().iterator();
while (i.hasNext()) {
Map.Entry me = (Map.Entry)i.next();
if ((Integer)me.getValue() > highColorCount) {
highColorCount = (Integer)me.getValue();
highColor = (String)me.getKey();
}
}
return color((highColor);
}
The Processing docs that I'm looking at are pretty sparse on the HashMap so I'm not really sure what's going on inside it, but I've been augmenting what's available there with Java docs they point to. But I'm not really grokking what's happening with the types. It looks like the key in the HashMap needs to be a string and the value needs to be an integer, but they come out as objects that I have to cast before using. So I'm not sure whether that's causing this glitch.
Or maybe there's just a problem with color() but the docs say that it'll take a hex value which is what I was trying to use as the key in the HashMap (where I'd rather just use the color itself).
Now that I've talked through this, I'm thinking that the color() function sees the hex value as an int but the hex() function converts a color to a string. And I don't seem to be able to convert that string to an int. I guess I could parse the substrings and reconstruct the color, but there must be some more elegant way to do this that I'm missing. Should I just create a key-value-pair class that'll hold a color and a count and use an arraylist of those?
Thanks in advance for any help or suggestions you can provide!
I'll dig deeper into this, but an initial thought is to employ Java generics so that the compiler will complain about type issues (and you won't get runtime errors):
HashMap<String,Integer> colors = new HashMap<String,Integer>();
So the compiler will know that keys are Strings and elements are Integers. Thus, no casting will be necessary.
I didn't figure it out, but I did work around it. I'm just making my own string from the color components like:
colors.put(red(pixels[i]) + "," + green(pixels[i]) + "," + blue(pixels[i]),1)
and then letting the function drop a color out like this:
String[] colorConstituents = split(highColor, ",");
return color(int(colorConstituents[0]), int(colorConstituents[1]), int(colorConstituents[2]));
This doesn't really seem like the best way to handle it -- if I'm messing with this long-term I guess I'll change it to use an arraylist of objects that hold the color and count, but this works for now.
I get the following error when compiling the std::find_if function:
error C2451: conditional expression of type 'overloaded-function' is illegal
The code looks like this:
typedef std::vector<boost::shared_ptr<node> >::iterator nodes_iterator;
nodes_iterator node_iter = std::find_if(_request->begin(), _request->end(), boost::bind(&RequestValues::is_parameter, this));
bool RequestValues::is_parameter(nodes_iterator iter)
{
return ((*iter)->name.compare("parameter") == 0);
}
It seems to have something to do with the predicate function passed to the std::find_if, however I cannot figure out what is wrong, can anybody help?
node is a struct containing some values.
You should use _1, not this when binding, and take a value_type as an argument of your function.
If this is a class or struct member function, then bind(func, this, _1) maybe? But if it is a class member function it should probably be static because it needn't state.
The comparison function you provide to find_if should not take in an iterator, but rather the value that the iterator is pointing at (or even better, a const reference to it). For example, when writing a predicate for find_if over a range of ints, the comparison should take in an int rather than vector<int>::iterator. Changing your comparison function to work on shared_ptr<node>s might not fix all your errors, but it should at least account for some of them.
That function's signature should be
bool RequestValues::is_parameter(boost::shared_ptr<node>);
i.e., it doesn't take an iterator, but the iterator's value_type.