Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Rockson · Jan 18, 2015 at 12:33 AM · datastructure

Data structure to efficently manage a queue of objects

I need To find and remove objects in the middle of a queue of objects. I suppose that list, arraylist, stack and queue implemented in .net 2.0 are useless. Should I implement an other data structure by myself? And which should I use? Aren't there any third party libraries?

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Rockson · Jan 18, 2015 at 05:53 AM 0
Share

Because first it will run a linear search and I think that, called each frame with more than 100 of objects in the list, will probably slow down the game. The problem is that I'm having heavy lags on android devices and the game is really simple!

avatar image tanoshimi · Jan 18, 2015 at 07:59 AM 0
Share

If you need to remove arbitrary values from the middle of the list, you won't get better than O(n)... are you sure this is what's causing your performance bottleneck?

avatar image Rockson · Jan 18, 2015 at 10:28 AM 0
Share

Probably it is..

avatar image samizzo · Jan 18, 2015 at 10:34 AM 0
Share

As tanoshimi said, you should profile your code. .NET's List class should do everything you need and I doubt it is causing your performance problems.

avatar image samizzo · Jan 18, 2015 at 10:39 AM 0
Share

There's no room for "probably" in program$$anonymous$$g. You should get the data! :)

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by fafase · Jan 18, 2015 at 10:44 AM

With an unordered collection there is no faster way than iterating through the whole thing and compare with the token.

Now, I said unordered which means if you use an ordered queue/list/array/else then you have some algorithm getting to the point faster using a binary search, then the point moves to the choice of sorting algorithm. BUT, Bill already did that for you so you can use list.Sort and that is it.

Once ordered, you can use a binary search that will increase your performance. Using an integer for comparison as opposed to string will also improve.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KurtGokhan · Jan 18, 2015 at 10:48 AM 0
Share

"There is no faster way than iterating through the whole thing and compare with the token.". Well we have dictionary don't we?

avatar image Rockson · Jan 18, 2015 at 02:04 PM 0
Share

You were right. I've compared the two data structures and the .NET implementation seems to be the quickest. 4,5 10^-6 seconds(Custom LinkedList) vs 1.9 10^-6 seconds.However now I think that my problem is due to rendering. Thank you all :) this was my implementation.

 class CustomLinkedListNode<T>
 {
    public T item;
    public CustomLinkedListNode<T> previous;
    public CustomLinkedListNode<T> next;
 
    public void Remove()
    {
       previous.next = next;
       next.previous = previous;
    }
 
    #region Constructors
    public CustomLinkedListNode(T item)
    {
       this.item = item;
    }
    public CustomLinkedListNode() { }
    #endregion
 }
 
 class CustomLinkedList<T>
 {
    private int N;
    private CustomLinkedListNode<T> pre;
    private CustomLinkedListNode<T> post;
    private Dictionary<T, CustomLinkedListNode<T>> searchDic;
 
    public CustomLinkedList()
    {
       searchDic = new Dictionary<T, CustomLinkedListNode<T>>();
       pre = new CustomLinkedListNode<T>();
       post = new CustomLinkedListNode<T>();
       pre.next = post;
       post.previous = pre;
    }
 
    public bool isEmpty() { return N == 0; }
    public int Count 
    { 
       get 
       { 
          return N; 
       } 
    }
 
    public void Remove(T item)
    {
       CustomLinkedListNode<T> nodeToRemove = searchDic[item];
       searchDic.Remove(item);
       nodeToRemove.Remove();
       N--;
    }
 
    public void Add(T item)
    {
       CustomLinkedListNode<T> last = post.previous;
       CustomLinkedListNode<T> newNode = new CustomLinkedListNode<T>();
       newNode.item = item;
       newNode.next = post;
       newNode.previous = last;
       post.previous = newNode;
       last.next = newNode;
       searchDic.Add(item, newNode);
       N++;
    }
 
 }
avatar image
0

Answer by KurtGokhan · Jan 18, 2015 at 10:11 AM

If you have programming experience you can write it yourself according to your needs. Here is an idea. Write a double linked list which will hold your objects(Which has extra node class to hold objects). When you add to the queue, add to the linked list and also hold a dictionary for fast search of node to delete. So the code will be like:

 // Actually a double linked list
 public class LinkedList<T>{
     LinkedListNode<T> head;
     LinkedListNode<T> tail;
     Dictionary<T,LinkedListNode<T>> searchDic;
     
     void Remove(T item){
         var nodeToRemove = searchDic[item];
         searchDic.Remove(item);
         nodeToRemove.Remove();
     }
 
     void Add(T item){
         var newNode = new LinkedListNode(item);
         tail.next = newNode;
         tail = newNode;
         searchDic.Add(item, newNode);
     }
 }
 
 public class LinkedListNode<T>{
     void Remove(); // This basically binds the next and previous
     
     #region Constructors
     // Insert Constructors here
     #endregion
 }

Note that it doesn't have to be generic. I don't guarantee it will work or you have what it takes to make it work. Also code is not completed and you should complete it as an exercise. If this works it means you can add, search and delete in constant time. One point I did oversee is you should first check if element exists in dictionary or you will get exceptions. You can use TryGetValue().

Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fafase · Jan 18, 2015 at 10:32 AM 0
Share

Why would you implement an already existing data structures? THis will never go as fast as the .NET linked list: http://msdn.microsoft.com/en-us/library/he2s3bh7%28v=vs.110%29.aspx

avatar image KurtGokhan · Jan 18, 2015 at 10:38 AM 0
Share

Of course he should use built-in data structures if it suits his needs. But overloading these are not possible and he may need other functionality from the data structure. For example .Net linked list takes linear time to remove an object http://msdn.microsoft.com/en-us/library/k87xw3hb(v=vs.110).aspx . This is because they assume there will be duplicates in the list. But if you won't have duplicates you can have a dictionary for fast search and make the remove constant time. That is why I prefer writing my own data structures.

avatar image KurtGokhan · Jan 18, 2015 at 10:42 AM 0
Share

By the way, if you implement it correctly, your data structures will be slower than built-in data structures only by trivial amount (trivial as in time to make 2-3 extra function calls).

avatar image Rockson · Jan 18, 2015 at 11:18 AM 0
Share

I haven't duplicates and I think this solution can be useful. However fafase why do you think it will be slower?

avatar image fafase · Jan 18, 2015 at 11:23 AM 0
Share

It is highly unlikely that a user may match the performance of .NET implementation. They know what they are doing at $$anonymous$$icrosoft and that is why they are doing it. Now, in some cases, you could get some good result by stripping off additional commands, most likely security and exception check. But then your code is less safe. So fast or safe? If you have no duplicate, then you could try Dictionary. Bets is to compare Dictionary against SortedList. First should be faster though but you could also improve by overriding the GetHashCode method of the type you use as key.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to store a Transform[,] grid in an ArrayList? 2 Answers

Irregular grid, data structure 1 Answer

How do I create a branching dialogue tree? 0 Answers

Item data structure - Scriptable Object Inheritance problems 1 Answer

Many scriptable objects at once = fine? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges