I have a member variable in a class:
val options = mutable.LinkedList[SelectOption]()
I latter then populate this list from the database.
At some point I want to refresh the list. How do I empty it?
In java:
options.clear();
Is there an equivalent in Scala?
Do not use LinkedList. That is a low level collection which provides a data structure that can be manipulated at user's will... and responsibility.
Instead, use one of the Buffer classes, which have the clear method. This method, by the way, is inherited from the Clearable trait, so you can just look at classes that extend Clearable.
Related
I'm having trouble implementing the iterator and get set functions. (Edit: By trouble I mean that I don't know how to do it and I need help)
In the past, I've inherited from ArrayList even though I need a fixed-size collection.
I've also used an Array property in my class and added a get and a set operator funcions, but these two solutions just feel like unnecessary workarounds.
Edit: I just found out that Kotlin itself doesn't have a List implementation yet (it uses ArrayList instead), So maybe I'm not supposed to be able to Implement it. I'm going to keep this open just in case.
There is a very neat feature in Kotlin that allows you to easily implement the List interface by Delegation. Just use the by operator on a list property, then the class will delegate all methods of the List interface to that property. So, for example, instead of,
class Cars {
private val cars = ArrayList<Car>()
}
you write:
class Cars(
private val cars: MutableList<Car> = ArrayList<Car>()
): MutableList<Car> by cars
Now Cars implements List, and can be used just as such:
val cars = Cars()
cars.add(Car())
for (car in cars)
car.vroom()
I am trying to model some database related functionalities through c++ inheritance.
There are around 150 tables in the database. And these tables fall into specific groups and tables within a specific group can be manipulated (querying, writing, etc) in a similar way.
class BaseTable(){
//Functionalities common for all tables. Eg: a create_table method.
}
class Group1Table(): BaseTable{
//Inherits from BaseTable and overrides(virtual function) or adds new functionlities.
}
class Group2Table(): BaseTable{
//Inherits from BaseTable and overrides(virtual function) or adds new functionlities.
}
There could be more GroupTables and the inheritance can also go deeper levels.
With this kind of inheritance, I can initialize a map like this:
std::map<int, boost::shared_ptr<BaseTable> > tablesMap = boost::assign::map_list_of(1, new BaseTable(x,y,z))(2, new Group1Table(p, q, r) ....(150, new GroupXTable(a, b, c)
So tablesMap would have a map of all the 150 tables (I have used simple keys in ascending order for simplicity. But the keys could be different. Hence the use of maps instead of a vector). With this map, I can iterate and call things like x->create_table(), x->query(), etc. Or when I want to do specific action on a specific table I can simply call
tablesMap[key]->action()
and the polymorphism will take care of calling the right function.
My concern is whether there is an overhead of storing these dynamically polymorphic objects and using it like the way I have described above. Or am I just being paranoid? Note that there will always be fixed number of objects in the map initialized once (~150 equal to the number of tables) and I would iterate through these objects and call various virtual functions wherever needed.
Is there a better way to model the problem so that I can avoid virtual functions? I tried to think in the lines of CRTP (static polymorphism) but I couldn't figure out a way to put the objects in a map and iterate through them like I can do with dynamic polymorphism.
I feel more fluent in Java than C++ so I will try to explain what I want to "transport" in a few words. In Java you could create an ArrayList containing objects of 2 or more classes that one of them inherited from another. Let me illustrate. Let's say we have:
class Vehicle
{
...
}
class Car extends Vehicle
{
...
}
You could then do something like that Car a = new Vehicle ([arguments]);
And then you could add objects of both Vehicle and Car to the same ArrayList created as following:
ArrayList ar = new ArrayList<Car>();
Now to my problem, in C++ I have manually written the code to create a simple linked list. Each node of this list is described below:
struct clientListNode
{
vip* cust;
struct clientListNode* next;
};
vip is another class which is derived from customer.Is there any way to allow my list to accept both objects of vip class and customer class?
Polymorphism works the other way. You can use a customer* pointer to point to a vip object, but not the other way. (This is related to the Liskov-substitution principle.)
So if you had a customer* in your clientListNode class, it would accept both vip* and customer* pointers.
However, I don't think you should reinvent the wheel and implement a linked list from the ground. You should look into the STL (Standard Template Library) which already has solutions for problems like this. For instance, it already has an implementation of the linked list, std::list.
You have to template std::list with the base type you'd like to use it with, but it is important that you cannot use the type directly as a value (std::list<cust>), because that would physically store cust instances in memory, in which you cannot fit the more specific vip type (slicing would occur).
Instead you have to use some kind of handle as the template parameter, you can use a native pointer (std::list<cust*>), but that way you have to manage memory deallocation, or - more preferably - you can use a smart pointer, like shared_ptr (std::list<std::shared_ptr<cust>>), that way the created objects are going to get destroyed automatically.
I doubt you could make Car = new Vehicle even in java, but the other way around makes sense.
What you're after is called "polymorphic collection". C++ standard lib does not have one out of the box, but you can approximate it with a collection of shared_ptr<Vehicle> s or unique_ptr<Vehicle> s. The former would fork very close to a java equivalent.
3rd party libraries may have ready to use polymorphic collections, you can use the term for search.
If customer is the base class, then the cust field of the node should be of type customer*, and it will gladly accept both customers and vips.
I'm trying to implement a generic class for lists for an embedded device using C++. Such a class will provide methods to update the list, sort the list, filter the list based on some user specified criteria, group the list based on some user specified criteria etc. But there are quite a few varieties of lists I want this generic class to support and each of these varieties can have different display aspects. Example: One variety of list can have strings and floating point numbers in each of its elements. Other variety could have a bitmap, string and special character in each of it's elements. etc.
I wrote down a class with the methods of interest (sort, group, etc). This class has an object of another class (say DisplayAspect) as its member. But the number of member variables and the type of each member variable of class DisplayAspect is unknown. What would be a better way to implement this?
Why not use the std::list, C++ provides that and it provides all the functionality you mentioned(It is templated class, So it supports all data types you can think of).
Also, there is no point reinventing the wheel as the code you write will almost will never be as efficient as std::list.
In case you still want to reinvent this wheel, You should write a template list class.
First, you should probably use std::list as your list, as others have stated. It seems to me that you are having problems more with what to put in the list, however, so I'm focusing on that part of the question.
Since you want to also store multiple bits of information in each element of the list, you will need to create multiple classes, one to store each combination. You don't describe why you are storing mutiple bits of information, but you'd want to use a logical name for each class. So if, for example, you were storing a name and a price (string and a double), you could give the class some name like Product.
You mention creating a class called DisplayAspect.
If this is because you want to have one piece of code print all of these lists, then you should use inheritance and polymorphism to accomplish this goal. One way to accomplish that is to make your DisplayAspect class an abstract class with the needed functions (printItem() for example) pure virtual and have each of the classes you created for the combinations of data be subclasses of this DisplayAspect class.
If, on the other hand, you created the DisplayAspect class so that you could reuse your list code, you should look into template classes. std::list is an example of a template class and it will hold any type you'd like to put into it and in that case, you could drop your DisplayAspect class.
Others (e.g., #Als) have already given the obvious, direct, answer to the question you asked. If you really want a linked list, they're undoubtedly correct: std::list is the obvious first choice.
I, however, am going to suggest that you probably don't want a linked list at all. A linked list is only rarely a useful data structure. Given what you've said you want (sorting, grouping), and especially your target (embedded system, so you probably don't have a lot of memory to waste) a linked list probably isn't a very good choice for what you're trying to do. At least right off, it sounds like something closer to an array probably makes a lot more sense.
If you end up (mistakenly) deciding that a linked list really is the right choice, there's a fair chance you only need a singly linked list though. For that, you might want to look at Boost Slist. While it's a little extra work to use (it's intrusive), this will generally have lower overhead, so it's at least not quite a poor of a choice as many generic linked lists.
Lets say I have a List<object> which is passed into a class as an argument, this list should contain a bunch of models for my application all of the same type. Is it then possible for me to somehow retrieve the type of the list which was passed in? (without calling GetType() on a item in the list).
For example, I pass in List<User> which is stored as List<object>, can I now retrieve the type User from the list without doing something like:
List<object> aList;
aList[0].GetType();
Well, you can use:
Type elementType = aList.GetType().GetGenericArguments[0];
However, that will fail if you pass in FooList which derives from List<Foo> for example. You could walk the type hierarchy and work things out appropriately that way, but it would be a pain.
If at all possible, it would be better to use generics throughout your code instead, potentially making existing methods generic - e.g. instead of:
public void Foo(List<object> list)
you'd have
public void Foo<T>(List<T> list)
or even
public void Foo<T>(IList<T> list)
If you just need it for the very specific case where the execution-time type will always be exactly List<T> for some list, then using GetGenericArguments will work... but it's not terribly nice.
As I understand it, the purpose of generics is to not have to do the type checking manually. The compiler ensures that the items in the list are the type they claim to be, and therefore the items that come out will be that type.
If you have a List<object>, you're defeating the purpose of using generics at all. A List<object> is a list that can store any type of object, no matter what types you actually put into it. Therefore, the onus is upon you to detect what the actual type of the object you retrieve is.
In short: you have to use GetType.