A class representing a VPP vector
| 81 | |
| 82 | |
| 83 | class StatsVector: |
| 84 | """A class representing a VPP vector""" |
| 85 | |
| 86 | def __init__(self, stats, ptr, fmt): |
| 87 | self.vec_start = ptr - stats.base |
| 88 | self.vec_len = get_vec_len(stats, ptr - stats.base) |
| 89 | self.struct = Struct(fmt) |
| 90 | self.fmtlen = len(fmt) |
| 91 | self.elementsize = self.struct.size |
| 92 | self.statseg = stats.statseg |
| 93 | self.stats = stats |
| 94 | |
| 95 | if self.vec_start + self.vec_len * self.elementsize >= stats.size: |
| 96 | raise IOError("Vector overruns stats segment") |
| 97 | |
| 98 | def __iter__(self): |
| 99 | with self.stats.lock: |
| 100 | return self.struct.iter_unpack( |
| 101 | self.statseg[ |
| 102 | self.vec_start : self.vec_start + self.elementsize * self.vec_len |
| 103 | ] |
| 104 | ) |
| 105 | |
| 106 | def __getitem__(self, index): |
| 107 | if index > self.vec_len: |
| 108 | raise IOError("Index beyond end of vector") |
| 109 | with self.stats.lock: |
| 110 | if self.fmtlen == 1: |
| 111 | return self.struct.unpack_from( |
| 112 | self.statseg, self.vec_start + (index * self.elementsize) |
| 113 | )[0] |
| 114 | return self.struct.unpack_from( |
| 115 | self.statseg, self.vec_start + (index * self.elementsize) |
| 116 | ) |
| 117 | |
| 118 | |
| 119 | class VPPStats: |
no outgoing calls
no test coverage detected