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 recon472 · Aug 03, 2014 at 09:35 PM · c#queuebinaryheap

Binary Heap Minimum value

Hi, I have this script to get an object with a lowest variable value. However if I run it I get an error IndexOutOfRangeException on the first line(12) of the Add function. Any ideas?

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class BinaryHeap : MonoBehaviour
 {
     public GameObject[] binaryHeap;
     private int numberOfItems;
 
     public void Add(GameObject fCost) 
     {
         this.binaryHeap[this.numberOfItems] = fCost;
         
         int bubbleIndex = this.numberOfItems;
         while (bubbleIndex != 1) 
         {
             int parentIndex = bubbleIndex / 2;
             if (this.binaryHeap[bubbleIndex].GetComponent<Node>().f_totalCost <= this.binaryHeap[parentIndex].GetComponent<Node>().f_totalCost) 
             {
                 GameObject tmpValue = this.binaryHeap[parentIndex];
                 this.binaryHeap[parentIndex] = this.binaryHeap[bubbleIndex];
                 this.binaryHeap[bubbleIndex] = tmpValue;
                 bubbleIndex = parentIndex;
             }
             else 
             {
                 break;
             }
         }             
         this.numberOfItems++;
     } 
 
     public GameObject Remove() 
     {
         this.numberOfItems--;
         GameObject returnItem = this.binaryHeap[1];
         
         this.binaryHeap[1] = this.binaryHeap[this.numberOfItems];
         
         int swapItem = 1, parent = 1;
         do {
             parent = swapItem;
             if ((2 * parent + 1) <= this.numberOfItems) 
             {
                 if (this.binaryHeap[parent].GetComponent<Node>().f_totalCost >= this.binaryHeap[2 * parent].GetComponent<Node>().f_totalCost) 
                 {
                     swapItem = 2 * parent;
                 }
                 if (this.binaryHeap[swapItem].GetComponent<Node>().f_totalCost >= this.binaryHeap[2 * parent + 1].GetComponent<Node>().f_totalCost) 
                 {
                     swapItem = 2 * parent + 1;
                 }
             } 
             else if ((2 * parent) <= this.numberOfItems) 
             {
                 if (this.binaryHeap[parent].GetComponent<Node>().f_totalCost >= this.binaryHeap[2 * parent].GetComponent<Node>().f_totalCost) 
                 {
                     swapItem = 2 * parent;
                 }
             }
             if (parent != swapItem) 
             {
                 GameObject tmpIndex = this.binaryHeap[parent];
                 this.binaryHeap[parent] = this.binaryHeap[swapItem];
                 this.binaryHeap[swapItem] = tmpIndex;
             }
         } 
         while (parent != swapItem);
             return returnItem;
     } 
 }
 
Comment
Add comment · Show 2
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 meat5000 ♦ · Aug 03, 2014 at 09:18 PM 1
Share

Does numberOfItems have a value at that point?

avatar image recon472 · Aug 03, 2014 at 09:54 PM 0
Share

nope but I've tried setting it to 0 and 1 (it should be one) during declaration and no change. I think the problem is that the field is not declared however I can't hardcode the length since I will be adding and removing stuff. I've also tried using list array ins$$anonymous$$d of an array which should be dynamic but still the same error

1 Reply

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

Answer by Kiwasi · Aug 03, 2014 at 10:17 PM

Use a generic List. This is what they are designed for. Psuedo code

 using system.collections.generic;
 
 public List<GameObjec> binaryHeap = new List<GameObject>();
 
 binaryHeap.Add(fCost);

The other option is to resize your array every time you add an element. Which is basically the same thing a list does, but will require you to write more code.

Actually rereading your question there is probably a specific collection type in system.collections.generic that will do the job for you without any extra coding. Worth checking out.

Comment
Add comment · Show 6 · 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 recon472 · Aug 03, 2014 at 10:27 PM 0
Share

well I was trying to avoid list as there should be a drop in performance. As I understand it lists are actually looped through when you want to access a variable in contrast to arrays where you can access it directly

avatar image Kiwasi · Aug 03, 2014 at 10:30 PM 0
Share

Resizing an array isn't cheap either.

Have you actually had performance issues using a list?

Build code that works first, optimise it later if it becomes an issue.

avatar image recon472 · Aug 03, 2014 at 10:33 PM 0
Share

yes I had. its part of a pathfinding which needs to be much faster than it currently is. unfortunately c# doesn't support priority queue.

avatar image Kiwasi · Aug 04, 2014 at 02:41 AM 0
Share

SortedList?

avatar image recon472 · Aug 04, 2014 at 12:30 PM 0
Share

so it turns out list actually uses an array and you can access it via index so it is very efficient when used right. btw this is a very useful article about containers and their pros and cons

http://geekswithblogs.net/BlackRabbitCoder/archive/2011/06/16/c.net-fundamentals-choosing-the-right-collection-class.aspx

thanks for help :)

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

Multiple Cars not working 1 Answer

A node in a childnode? 1 Answer

C# how to setup a Binary Serialization 4 Answers

Distribute terrain in zones 3 Answers

Zombie Wander 1 Answer


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