2014-07-15 36 views
1

在JavaScript中,它總是困擾着我人們使用對象作爲向量,如{x: 1, y: 2}而不是使用數組[1,2]。數組的訪問時間比對象快得多,但通過索引訪問更容易混淆,尤其是在需要大數組時。我知道飛鏢有固定的數組,但有沒有一種方法來命名數組的偏移量,就像你會用另一種語言的結構或元組/記錄一樣?定義枚舉/常量也許?飛鏢中是否有類似結構的東西?

我想要的東西像

List<int> myVector = new List([x,y]); 
myVector.x = 5; 

是有一個相當於或慣用的方式做到這一點?

+1

我不會因爲在JavaScript中使用對象而煩惱。如果代碼對性能至關重要,它將被優化,並且只要您保持到達那裏的所有元素的結構相同,它就應該與列表一樣高效。關鍵是保持物業訪問「單形」 - 即只有相同結構的對象達到這一點。 – lrn

回答

8

這聽起來像一個類中的一些開放的功能要求。

class MyVector { 
    final x; 
    final y; 
    const MyVector(this.x, this.y); 
} 

有在運行時創建一個名爲索引結構沒有更簡單和更有效的方式。爲此,你通常使用Map,但它不如真正的課堂效率。

類應該和固定長度列表一樣有效(時間和內存)。

4

對我來說,我看到2種方式來做到這一點。我會通過排序最好在我的角度來看

基於類的方法

這裏,方法是封裝你的需要,在一個專門的對象

優點:

  • 它的封裝
  • 你可以提出幾種方法訪問變量,依賴的需要
  • 您可以擴展功能,而無需打破一切
  • 我喜歡它:P

缺點

  • 更多的時間花費創造類等。
  • 你真的需要我說的專業人士嗎?
  • 也許怪異JS人

例如:

class Vector { 
    int x; 
    int y; 

    static final String X = "x"; 
    static final String Y = "y"; 

    Vector({this.x, this.y}); 
    Vector.fromList(List<int> listOfCoor) { 
    this.x = listOfCoor[0]; 
    this.y = listOfCoor[1]; 
    } 

    // Here i use String, but you can use [int] an redefine static final member 
    int operator[](String coor) { 
    if (coor == "x") { 
     return this.x; 
    } else if (coor == "y") { 
     return this.y; 
    } else { 
     // Need to be change by a more adapt exception :) 
     throw new Exception("Wrong coor"); 
    } 
    } 
} 

void main() { 
    Vector v = new Vector(x: 5, y: 42); 
    Vector v2 = new Vector.fromList([12, 24]); 

    print(v.x); // print 5 
    print(v["y"]); // print 42 
    print(v2.x); // print 12 
    print(v2[Vector.Y]); // print 24 
} 

枚舉爲基礎的方法:

您也可以定義的 「枚舉」(實際上沒有真正落實但將在未來的版本),將包含您的價值「快捷方式」

優點

  • 更多實施
  • 更像是你的榜樣簡單,P

缺點

  • 較少擴展
  • 第i墨是不是很漂亮
  • 沒有OOP認爲

例如:

class Vector { 
    static final int x = 0; 
    static final int y = 1; 
} 

void main() { 
    List<int> myVector = new List(2); 
    myVector[Vector.x] = 5; 
    myVector[Vector.y] = 42; 
} 

作出你的選擇,P

3

如果你有合理的大數據結構,你可以使用"dart:typed_data"作爲一個模型併爲存儲的數據提供輕量級視圖。這種方式的開銷應該是最小的。 例如,如果你需要UINT8值的4X4矩陣:

import "dart:typed_data"; 
import "dart:collection"; 
import "package:range/range.dart"; 
class Model4X4Uint8 { 
    final Uint8List _data; 
    static const int objectLength = 4 * 4; 
    final Queue<int> _freeSlotIndexes; 
    Model4X4Uint8(int length): _data = new Uint8List((length) * objectLength), 
     _freeSlotIndexes = new Queue<int>.from(range(0, length)); 
    int get slotsLeft => _freeSlotIndexes.length; 
    num operator [](int index) => _data[index]; 
    operator []=(int index, int val) => _data[index] = val; 
    int reserveSlot() => 
     slotsLeft > 0 ? _freeSlotIndexes.removeFirst() : throw ("full"); 
    void delete(int index) => _freeSlotIndexes.addFirst(index); 
} 
class Matrix4X4Uint8 { 
    final int offset; 
    final Model4X4Uint8 model; 
    const Matrix4X4Uint8(this.model, this.offset); 
    num operator [](int index) => model[offset + index]; 
    operator []=(int index, int val) => model[offset + index] = val; 
    void delete() => model.delete(offset); 
} 
void main() { 
    final Model4X4Uint8 data = new Model4X4Uint8(100); 
    final Matrix4X4Uint8 mat = new Matrix4X4Uint8(data, data.reserveSlot()) 
     ..[14] = 10 
     ..[12] = 256; //overlow; 
    print("${mat[0]} ${mat[4]} ${mat[8]} ${mat[12]} \n" 
     "${mat[1]} ${mat[5]} ${mat[9]} ${mat[13]} \n" 
     "${mat[2]} ${mat[6]} ${mat[10]} ${mat[14]} \n" 
     "${mat[3]} ${mat[7]} ${mat[11]} ${mat[15]} \n"); 
    mat.delete(); 
} 

但是,這是非常低的水平解決方案,可以輕鬆地創建帶有內存管理和溢出偷偷摸摸的錯誤。

相關問題