generic-collections

Avoiding Returning Wildcard Types

风流意气都作罢 提交于 2019-12-09 10:41:35
问题 I have a class with a collection of Wildcard Types that is a singleton, something like: public ObliviousClass{ private static final ObliviousClass INSTANCE = new ObliviousClass(); private Map<Key, Type<?>> map = new HashMap<Key, Type<?>>(); public void putType(Key key, Type<?> type){ map.put(type); } // returns the singleton public static ObliviousClass getInstance(){ return INSTANCE; } } I'd like to be able to add different Parameterized types to this collection in client code: void

Stack<T> implements ICollection, but has methods from ICollection<T>

南笙酒味 提交于 2019-12-08 19:44:19
问题 I'm trying to create a custom collection based on Stack<T> . When I look at Stack<T> [from metadata] in visual studio, it shows that Stack<T> implements ICollection , which would require it to implement ICollection 's CopyTo(Array array, index) method, but instead, it is shown as having ICollection<T> 's CopyTo(T[] array, index) method. Can someone explain why this is the case? I'm trying to create a collection that mimics Stack<T> pretty heavily. When I implement ICollection as stack does,

Trying to make templates in C

元气小坏坏 提交于 2019-12-07 23:23:31
问题 I made a generic vector in C using macros. Is the concept viable or do I get a one-way trip to the bonfire for even thinking about it? #ifndef VECTOR_H #define VECTOR_H #define vector_at(vector, pos) ((vector).data[pos]) #define VECTOR_DEFINITION(type)\ typedef struct {\ size_t size;\ size_t capacity;\ type *data;\ } vector_ ## type ## _t;\ void vector_ ## type ## _reserve(vector_ ## type ## _t *vector, size_t size) {\ size_t capacity = 1;\ while (capacity < size) capacity *= 2;\ if (size ==

How to convert LINQ query result to list and return generic List?

时光总嘲笑我的痴心妄想 提交于 2019-12-07 18:39:01
问题 I used generic ORM list example. This is my table. Person table Guid Name LastName and this is my struct class. public struct PersonItem // database and class field names are the same { public Guid Guid{ get; set; } public string Name { get; set; } public string LastName { get; set; } } public struct PersonItems { public PersonItems() { Items = new List<PersonItem>(); } public List<PersonItem> Items { get; set; } } I'm using such and no problem but I always have to write field's public

Command line query within Java

陌路散爱 提交于 2019-12-07 08:02:17
问题 I'm accessing a MongoDB and want to reuse typical command line queries also within Java. I know it is possible to use the BasicDBObject, but I want to use command line queries like this: db.MyCollection.find() I tried now using the command() method of the database: MongoClient mongoClient = new MongoClient("localhost", 27017); DB db = mongoClient.getDB("MyDatabase"); CommandResult result= db.command("db.MyCollection.find()"); JSONObject resultJson = new JSONObject(result.toString()); System

How to convert LINQ query result to list and return generic List?

回眸只為那壹抹淺笑 提交于 2019-12-06 12:33:04
I used generic ORM list example. This is my table. Person table Guid Name LastName and this is my struct class. public struct PersonItem // database and class field names are the same { public Guid Guid{ get; set; } public string Name { get; set; } public string LastName { get; set; } } public struct PersonItems { public PersonItems() { Items = new List<PersonItem>(); } public List<PersonItem> Items { get; set; } } I'm using such and no problem but I always have to write field's public PersonItems GetPersons() { var query = (from p in _DbEntities.t_Crew select p).ToList(); if (query != null) {

Trying to make templates in C

瘦欲@ 提交于 2019-12-06 09:44:57
I made a generic vector in C using macros. Is the concept viable or do I get a one-way trip to the bonfire for even thinking about it? #ifndef VECTOR_H #define VECTOR_H #define vector_at(vector, pos) ((vector).data[pos]) #define VECTOR_DEFINITION(type)\ typedef struct {\ size_t size;\ size_t capacity;\ type *data;\ } vector_ ## type ## _t;\ void vector_ ## type ## _reserve(vector_ ## type ## _t *vector, size_t size) {\ size_t capacity = 1;\ while (capacity < size) capacity *= 2;\ if (size == 0) capacity = 0;\ if (capacity != vector->capacity)\ {\ vector->capacity = capacity;\ if (size == 0) {\

Command line query within Java

点点圈 提交于 2019-12-05 14:08:39
I'm accessing a MongoDB and want to reuse typical command line queries also within Java. I know it is possible to use the BasicDBObject, but I want to use command line queries like this: db.MyCollection.find() I tried now using the command() method of the database: MongoClient mongoClient = new MongoClient("localhost", 27017); DB db = mongoClient.getDB("MyDatabase"); CommandResult result= db.command("db.MyCollection.find()"); JSONObject resultJson = new JSONObject(result.toString()); System.out.println(resultJson.toString(4)); But this returns me the following result. "ok": 0, "code": 59,

How can I return <TEnumerable, T> : where TEnumerable:IEnumerable<T>

白昼怎懂夜的黑 提交于 2019-12-05 07:28:24
Goal: Generic enumerated type to be the same type when returned. Note: This works when the types are entered but I don't understand why they can't be inferred. List<T> then return List<T> IOrderedEnumerable<T> then return IOrderedEnumerable<T> ETC Current method (works only if all types are entered) public static TEnumerable WithEach<TEnumerable, T>(this TEnumerable items, Action<T> action) where TEnumerable : IEnumerable<T> { foreach (var item in items) action.Invoke(item); return items; } Example Only var list = new List<int>(); //TODO: Mock random values list.WithEach(x => Console.WriteLine

How to remove from List<T> efficiently (C#)?

社会主义新天地 提交于 2019-12-05 00:49:12
问题 If I understood correctly (and please correct me if i'm wrong), list is implemented by array in .NET, which means that every deletion of an item in the list will cause re-allocation of all the list (which in turn means O(n) ). I'm developing a game, in the game i have many bullets fly in the air on any giving moment, let's say 100 bullets, each frame I move them by few pixels and check for collision with objects in the game, I need to remove from the list every bullet that collided. So I