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
1
Question by Rockythechao · Feb 14, 2014 at 12:37 AM · listrangevalues

How can I get all array/list values within a given range?

Given a sorted array/list/hashtable/dictionary/sortedlist/etc of values, what would be the best way to derive a selection of all the values between two values?

 var currentTime = 10;

 var allTimes = [1,2,4,7,8,9,11,14,16,17,17,19,22,28,46];
     
 // Returns a new list with [11,14,16,17,17,19]
 var nextFewTimes = GetListWithinRange(allTimes, currentTime, currentTime+10); 
 
Comment
Add comment
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

2 Replies

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

Answer by whydoidoit · Feb 14, 2014 at 05:47 AM

Given a sorted list or array use BinarySearch to get the lowest and highest values.

The two searches will be O(logN), presuming you can leave the data in situ then there would be no need to read the individual values until processing was required.

Comment
Add comment · 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
0

Answer by MakeCodeNow · Feb 14, 2014 at 01:25 AM

The answer depends on what kind of container you are using, but if it's a simple linear container like a list or array, then just use a for loop. It's linear time (O(N)), which is totally fine unless this array is really enormous and GetListWithinRange is run a lot.

Comment
Add comment · Show 1 · 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 Rockythechao · Feb 14, 2014 at 05:39 AM 0
Share

This is actually meant to read ti$$anonymous$$g data for a rhythm game (floats ins$$anonymous$$d of ints), so the list will be fairly large and GetListWithinRange will be called every update...

A friend just gave me the idea to use binary searches to find the positions of the $$anonymous$$ and max values within the time window, then copy everything between those to a new list. That sounds like it'd probably be faster.

And now that I think about it, I really only need to call GetListWithinRange at the beginning. After that, it simply needs to check the lowest-valued items in the original and derived lists -- which should be the first items in each list -- to see if they're within the time window and shift them accordingly.

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

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

how can I get values stored in a list? 3 Answers

Find the correct index number for array inside two loops 2 Answers

Argument Out of Range On a List 2 Answers

Excluding values from a Random.range? 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