學習 LLVM(8) ArrayRef 類

類 ArrayRef 定義在文件 llvm/include/llvm/[[ADT]]/[[ArrayRef.h]] 中。數組

== 簡介 ==
ArrayRef 模板類可用作首選的在內存中只讀元素的序列容器。使用 ArrayRef 的數據可被傳遞爲固定長度的數組,其元素在內存中是連續的。 函數

The llvm::ArrayRef class is the preferred class to use in an interface that accepts a sequential list of elements in memory and just reads from them. By taking an ArrayRef, the API can be passed a fixed size array, an std::vector, an llvm::SmallVector and anything else that is contiguous in memory.code


相似於 StringRef,ArrayRef 不擁有元素數組自己,只是引用一個數組或數組的一個連續區段。接口

== 實現機理 ==
ArrayRef 模板類簡要以下:內存

template <typename T> class ArrayRef {
  const T *Data; // 指向外部數組緩衝區,做爲數組的第0個元素。
  size_type Length; // 元素個數。

  ArrayRef(...) // 多種構造函數
  begin(), end(), empty(), size(), [], at() 等 STL 標準接口函數。
  slice(), equals() 等輔助函數
  vec() 生成爲 std::vector<T> // 注1
}

*注1: 重載有 operator std::vector<T>(),但實際上會建立一個新的 std::vector<T>,和轉換的語義是有區別的。叫作 to_vector() 可能更容易理解。element

* MutableArrayRef 和 ArrayRef 彷佛沒有很大區別,Mutable 體如今哪裏?
* makeArrayRef() 多種重載版本的函數,至關於調用 ArrayRef<T> 的各類構造函數。table

* ArrayRef 可當作 POD 數據看待,因其只保存 Data, Length 而不實際擁有底層數組。
模板

相關文章
相關標籤/搜索