Posted By

damarev on 04/14/09


Tagged

httpwwwsenocularcomflashactionscriptphpfileActionScript30Associa


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

thastylos
tspitzr


AssociativeArray in AS3


 / Published in: ActionScript 3
 

  1. package {
  2.  
  3. import flash.utils.Proxy;
  4. import flash.utils.flash_proxy;
  5.  
  6. /**
  7. * A sample associative array with a working
  8. * length property.
  9. */
  10. dynamic public class AssociativeArray extends Proxy {
  11.  
  12. private var memberValueHash:Object = new Object();
  13. private var memberIndexHash:Object = new Object();
  14. private var memberNameArray:Array = new Array();
  15.  
  16. public function get length():uint {
  17. return memberNameArray.length;
  18. }
  19. override flash_proxy function setProperty(name:*, value:*):void {
  20. if (name in memberValueHash == false) {
  21. var last:uint = memberNameArray.length;
  22. memberIndexHash[name] = last;
  23. memberNameArray[last] = name;
  24. }
  25. memberValueHash[name] = value;
  26. }
  27. override flash_proxy function getProperty(name:*):* {
  28. return memberValueHash[name];
  29. }
  30. override flash_proxy function callProperty(name:*, ...rest):* {
  31. if (memberValueHash[name] is Function){
  32. return memberValueHash[name].apply(null, arguments);
  33. }
  34. return null;
  35. }
  36. override flash_proxy function hasProperty(name:*):Boolean {
  37. return name in memberValueHash;
  38. }
  39. override flash_proxy function deleteProperty(name:*):Boolean {
  40. if (name in memberValueHash){
  41. var index:int = memberIndexHash[name];
  42. memberNameArray.splice(index, 1);
  43. var last:uint = memberNameArray.length;
  44. while(index < last){
  45. memberIndexHash[memberNameArray[index]]--;
  46. index++;
  47. }
  48. delete memberValueHash[name];
  49. delete memberIndexHash[name];
  50. return true;
  51. }
  52. return false;
  53. }
  54. override flash_proxy function nextNameIndex(index:int):int {
  55. return (index < memberNameArray.length) ? index + 1 : 0;
  56. }
  57. override flash_proxy function nextName(index:int):String {
  58. return memberNameArray[index - 1];
  59. }
  60. override flash_proxy function nextValue(index:int):* {
  61. return memberValueHash[memberNameArray[index - 1]];
  62. }
  63. override flash_proxy function getDescendants(name:*):* {
  64. return null;
  65. }
  66. override flash_proxy function isAttribute(name:*):Boolean {
  67. return false;
  68. }
  69. }
  70. }

Report this snippet  

You need to login to post a comment.