Flash 浏览器应用程序 ActionScript:如何*高效*地从排序数组中提取对象子集?
我有一个浏览器部署的 Flash 应用程序(不是可以访问 SQLConnection 的 AIR 应用程序),它通过 HTTPService 从远程服务器获取 JSON 结果。
我需要从返回的结果集中提取子集,即对象数组,高效。通过云多次调用后端是行不通的。这一切都必须发生在客户端。
Flex ActionScript 中是否有任何集合类可以根据对象共有的属性之一对对象数组进行排序,例如 Array sortOn 方法,然后还提供二分搜索方法可以从数组的排序版本中提取对象子集而无需访问数组中的每个项目并进行比较?
例如,如果我有一个对象数组,并且每个对象都有一个 zip 属性和 name 属性,我希望能够使用 zip 提取所有对象 = 10015 来自原始数组的副本,该副本已在 zip 上排序。
谢谢
I have a browser-deployed Flash app (not an AIR app with access to SQLConnection) and it fetches JSON results from a remote server via HTTPService.
I need to extract subsets from the returned resultset, an array of objects, efficiently. Mutltiple calls through the cloud to the back-end won't do. It all has to happen client-side.
Is there any collection class in Flex ActionScript that can sort an array of objects by one of the properties the objects all have in common, like the Array sortOn method, and then also provides a binary search method can extract a subset of objects from the sorted version of the array without visiting every item in the array and comparing?
E.g. if I have an array of objects and each object had a zip property and a name property, I'd like to be able to extract all objects with zip = 10015 from the a copy of the original array where the copy has been sorted on zip.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道有任何内置集合可以进行二分搜索。但是您可以使用
Array:: 对数组进行排序sortOn
方法并编写自己的二分搜索代码。您可以从以下内容开始:现在您可以从返回的索引值(如果是!= -1)向上和向下搜索,并检索 zip 值 = 10015 的整个子集。
顺便说一句,如果数据太大而无法搜索在客户端使用正常方法,它不会大到足以成为带宽瓶颈吗?
I am not aware of any inbuilt collection that does a binary search. But you can sort the array using the
Array::sortOn
method and write your own code for binary search. You can start with something like:Now you can search up and down from the returned index value (if it is != -1) and retrieve the whole subset with zip value = 10015.
Btw, if the data is too big to be searched at the client side using normal methods, wouldn't it be big enough to be a bandwidth bottleneck too?
您可以使用 array.sortOn() ,然后在排序后的数组上迭代一次(从 0 开始):当到达第一个匹配项时,开始向前迭代时返回元素,直到停止匹配。这将返回匹配元素的整个子集,平均而言,您将仅访问原始数组的一半(排序后)。
(如果这太慢,根据您的数据,使用二分搜索来获取匹配项可能会更快,然后向下迭代直到停止匹配,即找到有序集中的第一个匹配项,然后开始返回元素向上迭代,直到用完匹配项...但是与执行原始 sortOn() 所需的时间相比,节省的时间可能微不足道
You could use
array.sortOn()
and then iterate once over the sorted array (starting from 0): when you reach the first match, start returning elements as you iterate forwards, until you stop matching. This returns the entire subset of matching elements, and on average you will be visiting only half the original array (after sorting).(If this is too slow, it might be quicker,depending on your data, to use a binary search to get a match, then iterate down until you stop matching ie find the first match in the ordered set, then start returning elements as you iterate upwards until you run out of matches... BUT the time saving might be marginal compared to the time needed to do the original sortOn())