C++ N Nested Vectors at Runtime

Nested class that inherits from its generic parent class

I have a solution using ecapsulation of classes instead of inheritance.

public abstract class BaseGeneric<T>
{
T data;
// ctor
protected BaseGeneric(T data)
{
this.data=data;
}
// methods
public abstract void Update();
// properties
public T Data
{
get { return data; }
set { data=value; }
}

// base nested class
public abstract class BaseNested<B> where B : BaseGeneric<T>
{
protected B @base;
// ctor
protected BaseNested(B @base)
{
this.@base=@base;
}
// methods
public abstract void Input(T data);
public void Update() { @base.Update(); }
// properties
public T Data
{
get { return @base.data; }
set { @base.data=value; }
}
}
}

// implementation base
public class Base : BaseGeneric<int>
{
// ctor
protected Base(int data) : base(data) { }
//methods
public override void Update()
{
this.Data+=1;
}
// implemented nested class
public class Nested : Base.BaseNested<Base>
{
// ctor
public Nested(int data) : base(new Base(data)) { }
public Nested(Base @base) : base(@base) { }
// methods
public override void Input(int data)
{
this.Data=data;
}
}
}

class Program
{
static void Main(string[] args)
{
// new implemented class with value 0
var nested=new Base.Nested(0);
// set value to 100
nested.Input(100);
// call update as implemented by `Base`.
nested.Update();
}
}

Append an object to a list in R in amortized constant time, O(1)?

If it's a list of string, just use the c() function :

R> LL <- list(a="tom", b="dick")
R> c(LL, c="harry")
$a
[1] "tom"

$b
[1] "dick"

$c
[1] "harry"

R> class(LL)
[1] "list"
R>

That works on vectors too, so do I get the bonus points?

Edit (2015-Feb-01): This post is coming up on its fifth birthday. Some kind readers keep repeating any shortcomings with it, so by all means also see some of the comments below. One suggestion for list types:

newlist <- list(oldlist, list(someobj))

In general, R types can make it hard to have one and just one idiom for all types and uses.

Utility class that re-throws a throwable as unchecked?

Yes, there is a way to write a method that will avoid wrapping your checked exceptions. For this use case it is a perfect match, although you should definitely be very careful with it, since it can easily confuse the uninitiated. Here it is:

@SuppressWarnings("unchecked")
public static <T extends Throwable> void sneakyThrow(Throwable t) throws T {
throw (T) t;
}

and you'd use it as

catch (Throwable t) { sneakyThrow(t); }

As commented by Joachim Sauer, in certain cases it helps convince the compiler that the line calling sneakyThrow causes the method to terminate. We can just change the declared return type:

@SuppressWarnings("unchecked")
public static <T extends Throwable> T sneakyThrow(Throwable t) throws T {
throw (T) t;
}

and use it like this:

catch (Throwable t) { throw sneakyThrow(t); }

For educational purposes it is nice to see what's going on at the bytecode level. The relevant snippet from javap -verbose UncheckedThrower:

public static <T extends java.lang.Throwable> java.lang.RuntimeException sneakyThrow(java.lang.Throwable) throws T;
descriptor: (Ljava/lang/Throwable;)Ljava/lang/RuntimeException;
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: athrow
Exceptions:
throws java.lang.Throwable
Signature: #13 // <T:Ljava/lang/Throwable;>(Ljava/lang/Throwable;)Ljava/lang/RuntimeException;^TT;

Note there is no checkcast instruction. The method even legitimately declares to throw T, which can be any Throwable.

How to retrieve the class of a generic type

The simple answer is: you have to pass a Class<T> argument to the method.

public <T> List<T> methodeName(String path, Class<T> clazz) throws FileNotFoundException {
CSVReader reader = new CSVReader(new FileReader(path), ',', '\"');

HeaderColumnNameTranslateMappingStrategy<T> strat =
new HeaderColumnNameTranslateMappingStrategy<T>();
strat.setType(clazz);
}

...

ClassName cn = new ClassName();
List<Foo> foos = cn.methodeName(somePath, Foo.class);

how to understand GC Root by Monitor Used Object?

From your own reference:

Sample Image

Assuming that reference is correct, I would interpret it as:

synchronized(obj) {
// obj used as monitor
}
// obj free to collect


Related Topics



Leave a reply



Submit