I’m trying to create an object which is constructed from array of uvm_queue. The idea is that each item in the array is for itself a uvm_queue ( i.e. a list of items). The Queue type is a class.
The problem is I can’t manage to iterate the items of a single ‘memory ID’ list.
When I compile I get the following Error:
if (ml_mem_array[mem_id][ii].m_addr == a_addr)
xmelab: *E,CUVNAA (./utils.sv,47|45): An index has been applied to an inappropriate type.
The code looks like this:
// ====================
class mem_entry extends uvm_object;
int m_addr;
int m_data;
endclass
// ====================
class memory extends uvm_object;
uvm_queue#(mem_entry) ml_mem_array[2];
function mem_entry get_mem_entry(int a_addr, int mem_id);
int v_size;
int ii;
v_size = ml_mem_array[mem_id].size();
if ((mem_id >= 2) || (v_size == 0))
return null;
for (ii=0 ; ii<=(v_size-1) ; ii=ii+1)
begin
if (ml_mem_array[mem_id][ii].m_addr == a_addr)
return ml_mem_array[mem_id][ii];
end
return null;
endfunction
endclass
You need to use the get() method of uvm_queue to access the internal queue elements
for (int ii=0 ; ii<=(v_size-1) ; ii=ii+1)
begin
mem_entry entry = ml_mem_array[mem_id].get(ii);
if (entry.m_addr == a_addr)
return entry;
end
Related
I'm trying to use the CList Search method in an application. I have attached a very simple example below.
In this example, I always get a null pointer in the variable result. I tried it in MQL4 and MQL5. Has anyone ever made the Search method work? If so, where is my mistake? With my question, I refer to this implementation of a linked list in MQL (it's the standard implementation). Of course, in my application, I do not want to find the first list item, but items that match specific criteria. But even this trivial example does not work for me.
#property strict
#include <Arrays\List.mqh>
#include <Object.mqh>
class MyType : public CObject {
private:
int val;
public:
MyType(int val);
int GetVal(void);
};
MyType::MyType(int val): val(val) {}
int MyType::GetVal(void) {
return val;
}
void OnStart() {
CList *list = new CList();
list.Add(new MyType(3));
// This returns a valid pointer with
// the correct value
MyType* first = list.GetFirstNode();
// This always returns NULL, even though the list
// contains its first element
MyType* result = list.Search(first);
delete list;
}
CList is a kind of linked list. A classical arraylist is CArrayObj in MQL4/5 with Search() and some other methods. You have to sort the list (so implement virtual int Compare(const CObject *node,const int mode=0) const method) before calling search.
virtual int MyType::Compare(const CObject *node,const int mode=0) const {
MyType *another=(MyType*)node;
return this.val-another.GetVal();
}
void OnStart(){
CArrayObj list=new CArrayObj();
list.Add(new MyType(3));
list.Add(new MyType(4));
list.Sort();
MyType *obj3=new MyType(3), *obj2=new MyType(2);
int index3=list.Search(obj3);//found, 0
int index2=list.Search(obj2);//not found, -1
delete(obj3);
delete(obj2);
}
I'm trying an example in kotlin, like:
fun test(){
val harfler = listOf("a","b",'c','d')
println(harfler.all {
it.javaClass == String::class.java || it.javaClass == Char::class.java
})
}
List contains Char or String but all function in this expression returns false,Why return false?
Can anybody explain it?
Edit
for #JBNizet
As #JB Nizet has already told you how to analyze the problem.
According to the Mapped Types, The Kotlin Char will be mapped to Java Type decide on its declaration.
when declare as a non-nullable type Char it is a primitive Java type char.
when declare as a nullable type Char? it is a Java wrapper type Character.
when declare as a type argument List<Char> it is a Java wrapper type Character.
val it = 'a'
// v--- it should be `Any`
val array: Array<Any> = arrayOf('a')
// v--- char
println(it.javaClass)
// v--- print [java.lang.Character]
println(array.map { it.javaClass })
But I want to say that there is a different between the usage and the declaration.
For example, the parameter type it is a java.lang.Character, but its javaClass is char.
fun typeOf(it: Char?) = it?.javaClass
fun test() {
// v--- java.lang.Character
println(::typeOf.javaMethod!!.parameterTypes[0])
// v--- but it return `char` rather than `java.lang.Character`
println(typeOf('a'))
}
And the example below show the different as further, this is why I declare the array type to Array<Any> rather than Array<Char> in preceding example:
// v--- uses `java.lang.Character` instead
val array: Array<Char> = arrayOf('a')
// v--- java.lang.Character
println(array.javaClass.componentType)
// v--- [char]
println(array.map { it.javaClass })
Why did the strange behavior occurs in Koltin?
This is because Kotlin Char and other wrapper classes represent 2 roles. one is a Java primitive type char, another is a Java wrapper class java.lang.Character. However, Kotlin Char is statically which means you can't change its type in runtime. and a Char should be mapped to a char by default in Kotlin.
IF you want get the wrapper type every time, you should use KClass.javaObjectType instead, for example:
// v--- char
println(Char::class.java)
// v--- java.lang.Character
println(Char::class.javaObjectType)
The Iterable#all operation is a short-circuiting operation, which means if any first element didn't satisfied will return false immediately.
inline fun <T> Iterable<T>.all(predicate: (T) -> Boolean): Boolean {
// return `false` immediately the condition didn't satisfied
// v
for (element in this) if (!predicate(element)) return false
return true
}
When checking a Kotlin class like as Char and others. you should use the Kotlin type checking mechanism rather than traditional comparing approach, it helps you avoid such a confusion. for example:
val anything: Array<Any> = arrayOf('a')
val chars: Array<Char> = arrayOf('a')
println(chars.all { it is Char }) // print true
println(anything.all { it is Char }) // print true
So your code can replace with type checking as below:
fun test() {
val harfler = listOf("a", "b", 'c', 'd')
// v---------------v--- use type checking here
println(harfler.all { it is String || it is Char }) // print true
}
Hi. I am having trouble writing this method (in the photo) in a recursuve format. The method gets the amount of occurences of a given element in the binary search tree.
To solve this recursively, I was trying to implement it with a private helper method of the same name, like this:
public int count(){
count = 0;
if (root == null)
return count;
return count (root.getInfo());
private int count(T element){
(Basically the same code you see in the photo)
}
but I ended up with overflow errors. Would you mind taking a look and telling me how I can structure this method recursively?
Cheers, and thanks.
A tentative implementation may looks like this.
public int count(T element, T root){
if(element == null) {
return 0;
}
int count = 0;
int compare = element.compareTo(root.getInfo());
if(compare == 0){
count++;
}
count += count(element, root.getLeft());
count += count(element, root.getRight());
return count;
}
count(item, root);
I am trying to implement a graph data structure in the D language which supports parallel iteration over the node and edge sets.
alias ulong index;
alias index node;
alias ulong count;
class Graph {
index z; // max node index
count n; // number of nodes
count m; // number of edges
node[][] adja; // adjacency list
count[] deg; // node degree
this(count n = 0) {
this.z = n;
this.n = n;
this.m = 0;
this.adja = new node[][](this.z, 0);
this.deg = new count[](this.z);
}
Here's a sequential node iterator method:
/**
* Iterate over all nodes of the graph and call handler (lambda closure).
*/
void forNodes(F)(F handle) {
foreach (node v; 0 .. z) {
// call here
handle(v);
}
}
Works like this, and seems to work fine:
ulong sum1 = 0;
G.forNodes((node v) {
sum1 += v;
});
Now I try a parallel version using the 'std.parallelism' module:
void parallelForNodes(F)(F handle) {
foreach (node v; taskPool.parallel(z)) {
// call here
handle(v);
}
}
But this gives me the a compiler error. What am I doing wrong here?
cls ~/workspace/Prototypes/PLPd $ ./main.d
/usr/local/Cellar/dmd/2.063/src/phobos/std/parallelism.d(3795): Error: cannot have parameter of type void
/usr/local/Cellar/dmd/2.063/src/phobos/std/parallelism.d(3796): Error: cannot have parameter of type void
/usr/local/Cellar/dmd/2.063/src/phobos/std/parallelism.d(1539): Error: template instance std.parallelism.ParallelForeach!(ulong) error instantiating
Graph.d(90): instantiated from here: parallel!(ulong)
./main.d(100): instantiated from here: parallelForNodes!(void delegate(ulong v) nothrow #safe)
Graph.d(90): Error: template instance std.parallelism.TaskPool.parallel!(ulong) error instantiating
./main.d(100): instantiated from here: parallelForNodes!(void delegate(ulong v) nothrow #safe)
./main.d(100): Error: template instance Graph.Graph.parallelForNodes!(void delegate(ulong v) nothrow #safe) error instantiating
Failed: 'dmd' '-v' '-o-' './main.d' '-I.'
parallel takes a range. Use std.range.iota to get the range equivalent of 0 .. z: foreach (v; parallel(iota(z))) {...}
Is there any way by which I can get the sqlite3_exec() result without through callback?
When I do search I want to get the result directly, most like a return of the function or as OUT param?
Thanks.
I have written some code that allows us to read data from open db (db) according to sql query (zSql) without callback.
Please note, that this code works but might still need some work (for example, I'm not sure if we need to free the text data or not...)
int RunSqlNoCallback(sqlite3 * db, const char * zSql)
{
sqlite3_stmt *stmt = NULL;
int rc = sqlite3_prepare_v2(db, zSql, -1, &stmt, NULL);
if (rc != SQLITE_OK)
return rc;
int rowCount = 0;
rc = sqlite3_step(stmt);
while (rc != SQLITE_DONE && rc != SQLITE_OK)
{
rowCount++;
int colCount = sqlite3_column_count(stmt);
for (int colIndex = 0; colIndex < colCount; colIndex++)
{
int type = sqlite3_column_type(stmt, colIndex);
const char * columnName = sqlite3_column_name(stmt, colIndex);
if (type == SQLITE_INTEGER)
{
int valInt = sqlite3_column_int(stmt, colIndex);
printf("columnName = %s, Integer val = %d", columnName, valInt);
}
else if (type == SQLITE_FLOAT)
{
double valDouble = sqlite3_column_double(stmt, colIndex);
printf("columnName = %s,Double val = %f", columnName, valDouble);
}
else if (type == SQLITE_TEXT)
{
const unsigned char * valChar = sqlite3_column_text(stmt, colIndex);
printf("columnName = %s,Text val = %s", columnName, valChar);
free(valChar);
}
else if (type == SQLITE_BLOB)
{
printf("columnName = %s,BLOB", columnName);
}
else if (type == SQLITE_NULL)
{
printf("columnName = %s,NULL", columnName);
}
}
printf("Line %d, rowCount = %d", rowCount, colCount);
rc = sqlite3_step(stmt);
}
rc = sqlite3_finalize(stmt);
return rc;
}
sqlite3_exec is a convenience wrapper.
If you don't need a callback you should use underlying functions: sqlite3_prepare_v2(), sqlite3_step(), and sqlite3_finalize() directly.
Compile sql query with sqlite3_prepare_v2()
Run sqlite3_step() as many times as necessary to execute the query, use
sqlite3_column_bytes() etc to retrieve the data
Destroy prepared query with sqlite3_finalize()
But it requires more coding and careful error handling.
Calling the callback is how sqlite3_exec is designed to work.
If you want a function that allocates all the memory for the result and returns that, you need a different function, like sqlite3_get_table.
The call back is the mechanism that sqlite3_exec () uses to return one or more results. It looks like its primary use is with a select SQL statement. If the select SQL statement returns multiple rows of results then the call back function specified is called by the sqlite functionality for each row.
I am not sure if you are using C or C++ or some other language however I have a possible solution for you using C with SQLite.
The call back function has as its first argument a void pointer and this can point to pretty much anything. For instance it can point to a struct that contains a pointer to a memory area allocated for the result along with parameters indicating the size of the memory area.
I put an answer to this stackoverflow question, use of sql3_exec, with an example of using a call back that populates a C struct with the results of a select SQL statement. In this example I have a call back that takes one or more row results from a select and transform the results into the data values to put into a C struct. When the sqlite3_exec() function returns, I then process the results from the select using the C struct.
This may be what you are looking for in that you can create your select SQL statement, set up the results struct, call sqlite3_exec() specifying the results struct along with the function pointer to the call back to process the results using the results struct, and when sqlite3_exec() return you have the results of your select. The results of the select will be at the point where you called sqlite3_exec().