In Poly/ML, names of global values can be obtained by:
map #1 ((#allVal PolyML.globalNameSpace) ());
and top level strucutres can be obtained similarly using #allStruct.
Now, how can I list all values inside a structure?
That is, is there a way to implement ??? below?
> ??? "String";
["implode", "explode", "concatWith", ...]
This is my current workaround.
You can delete all values from PolyML.globalNameSpace (forgetValue) then open the structure and repeat (#allVal PolyML.globalNameSpace) ()
Related
I have an existing TTree after doing a simulation. I would like to add a Branch to this TTree and I want to call it Muon.Mass to the tree. I would also want to give the Muon.Mass branch a value of 0.1.
How can I write that?
I have seen how to create TTrees from scratch and to have branches of different variables. But I am not sure exactly what to do when I already have a TTree.
You can call the TTree::Branch method on an existing TTree the same way as for a new TTree. Just for filling you need to ensure you only fill the branch. (this is a strongly cut down example from https://github.com/pseyfert/tmva-branch-adder)
void AddABranch(TTree* tree) {
Float_t my_local_variable;
TBranch* my_new_branch = tree->AddBranch( ... /* use address of my_local_variable */ );
for (Long64_t entry = 0 ; entry < tree->GetEntries() ; ++e ) {
tree->GetEntry();
/* something to compute my_local_variable */
my_new_branch->Fill();
}
}
As alternative you might want to look at the root tutorials for tree friends.
As a side note, depending what you want to do with the tree / whom you give the tree to, I advise against using . in branch names as they cause headache when running MakeClass (branch names can contain periods, but c++ variables can't, so the automatically generated class members for each branch will undergo character replacement).
I have the following JSON file:
{
"outer_size":2,
"inner_size":{
"length_one":2,
"length_two":1
}
}
I will use this info to create a new JSON file, whose dimensions are determined by outer_size, inner_size, length_one and length_two. The structure I want to generate has the following form
[
{
"a":[
{
"a_one":1
},
{
"a_two":2
}
]
},
{
"b":[
{
"b_one":1
}
]
}
]
This structure contains two "outer" variables a and b because outer_size=2.
a contains two "inner" variables a_one and a_two, while b contains one "inner" variable b_one. This is because inner_size is 2 and 1, respectively.
Question Based on a given outer_size, inner_size, length_one and length_two, what is the best way to generate a JSON structure with those dimensions? Can/should it be done with classes?
Please note the following
The value of outer_size must always be equal to the number of length_XX-specifications (in the above example 2). In case it is 3, we will have to specify length_three too.
The specific values of a_one, a_ two etc... can be whatever for this example. Now my main concern is merely to construct the basic structure.
I'm using Nlohmann's JSON library for reading the initial JSON file.
Without using any JSON library, I have been using this code to produce JSON code "manually".
fputs("[\n",file);
fputs("\t{\n",file);
fputs("\t\t\"a\":[\n" ,file);
fputs("\t\t {\n",file);
fprintf(file,\t\t\t\"a_one\": \"%s\",\n",functionReturningJSONValue());
Which would print something like that you have asked. I haven't done it fully but I am sure you will understand how it works.
Hoping it helped you a bit.
You can still loop in order to create a certain size of JSON and input values with fprintf.
What is the cleanest short way to get this done ?
class AnObject{
Long attr;
}
List<AnObject> list;
I know it can be done with custom comparator for AnObject. Isn't there something ready out of the box for such case?
Kind of like this:
Collections.sort(list, X.attr);
Assuming you actually have a List<AnObject>, all you need is
list.sort(Comparator.comparing(a -> a.attr));
If you make you code clean by not using public fields, but accessor methods, it becomes even cleaner:
list.sort(Comparator.comparing(AnObject::getAttr));
As a complement to #JB Nizet's answer, if your attr is nullable,
list.sort(Comparator.comparing(AnObject::getAttr));
may throw a NPE.
If you also want to sort null values, you can consider
list.sort(Comparator.comparing(a -> a.attr, Comparator.nullsFirst(Comparator.naturalOrder())));
or
list.sort(Comparator.comparing(a -> a.attr, Comparator.nullsLast(Comparator.naturalOrder())));
which will put nulls first or last.
A null-safe option to JB Nizet's and Alex's answer above would be to do the following:
list.sort(Comparator.comparing(AnObject::getAttr, Comparator.nullsFirst(Comparator.naturalOrder())));
or
list.sort(Comparator.comparing(AnObject::getAttr, Comparator.nullsLast(Comparator.naturalOrder())));
I have some C++ code where I need to implement cache replacement using LRU technique.
So far I know two methods to implement LRU cache replacement:
Using timeStamp for each time the cached data is accessed and finally comparing the timeStamps at time of replacement.
Using a stack of cached items and moving them to the top if they are accessed recently, so finally the bottom will contain the LRU Candidate.
So, which of these is better to be used in production code?
Are their any other better methods?
Recently I implemented a LRU cache using a linked list spread over a hash map.
/// Typedef for URL/Entry pair
typedef std::pair< std::string, Entry > EntryPair;
/// Typedef for Cache list
typedef std::list< EntryPair > CacheList;
/// Typedef for URL-indexed map into the CacheList
typedef boost::unordered_map< std::string, CacheList::iterator > CacheMap;
/// Cache LRU list
CacheList mCacheList;
/// Cache map into the list
CacheMap mCacheMap;
It has the advantage of being O(1) for all important operations.
The insertion algorithm:
// create new entry
Entry iEntry( ... );
// push it to the front;
mCacheList.push_front( std::make_pair( aURL, iEntry ) );
// add it to the cache map
mCacheMap[ aURL ] = mCacheList.begin();
// increase count of entries
mEntries++;
// check if it's time to remove the last element
if ( mEntries > mMaxEntries )
{
// erease from the map the last cache list element
mCacheMap.erase( mCacheList.back().first );
// erase it from the list
mCacheList.pop_back();
// decrease count
mEntries--;
}
Here is a very simple implementation of LRU cache
https://github.com/lamerman/cpp-lru-cache .
It's easy to use and understand how it works. The total size of code is about 50 lines.
For simplicity, maybe you should consider using Boost's MultiIndex map. If we separate the key from the data, we support multiple sets of keys on the same data.
From [ http://old.nabble.com/realization-of-Last-Recently-Used-cache-with-boost%3A%3Amulti_index-td22326432.html ]:
"...use two indexes: 1) hashed for searching value by key 2) sequential for tracking last recently used items (get function put item as last item in sequesnce. If we need to remove some items from cache, we may delete they from begin of sequence)."
Note that the "project" operator "allows the programmer to move between different indices of the same multi_index_container" efficiently.
This article describes implementation using a pair of STL containers (a key-value map plus a list for the key access history), or a single boost::bimap.
In our production environment we use a C++ double linked list which is similar to the Linux kernel linked list. The beauty of it is that you can add an object to as many linked lists as you want and list operation is fast and simple.
This can be done with boost/compute/detail/lru_cache.hpp. Here is a basic example using it.
#include <boost/compute/detail/lru_cache.hpp>
...
// create an instance that maps from a double to a string and has a max size of 1000
auto my_lru_cache = boost::compute::detail::lru_cache<double, std::string>(1000);
my_lru_cache.insert(3.14, "pi");
if (my_lru_cache.contains(3.14))
{
// the first get returns a boost::optional
auto value = my_lru_cache.get(3.14).get();
std::cout << value << "\n";
}
I am trying to find the set difference of two vectors, so i do something like this:
std::vector<sha1_hash> first_vec, second_vec, difference_vec;
// populate first_vec and second_vec ...
std::sort(first_vec.begin(),first_vec.end());
std::sort(second_vec.begin(),second_vec.end());
std::set_difference(first_vec.begin(),first_vec.end(),
second_vec.begin(),second_vec.end(),
difference_vec.begin());
When i run this in debug, i get the following run-time assertion failure (in 'vector'):
_SCL_SECURE_VALIDATE_RANGE(_Myptr < ((_Myvec *)(this->_Getmycont()))->_Mylast);
I am using VS 2008.
Any ideas on what can trigger this?
Like most c++ algorithms, set_difference does not create new entries in the output vector where none existed before. You ned to create space in the output to hold the results.
Edit: Or use an an insert iterator (following untested):
back_insert_iterator< std::vector<sha1_hash> > bi( difference_vec );
std::set_difference(first_vec.begin(),first_vec.end(),
second_vec.begin(),second_vec.end(),
bi);