def draw_root(canvas, center, left, right): SEG_W = 0.2 SEG_H = 0.4 n = len(left) + len(right) segs = repeat(center + Point(-SEG_W * (n - 1) / 2, 0)) segs = accumulate(segs, lambda x, _: x + Point(SEG_W, 0)) segs = (Rectangle( center=x, width=SEG_W, height=SEG_H) \ for x in segs) segs = list(islice(segs, n)) src = chain(((i * 2, colors.BLUE, x) for i, x in enumerate(left)), ((i * 2 + 1, colors.ORANGE, x) for i, x in enumerate(right))) for i, c, (r, t) in src: d = segs[i] canvas.new_text(anchor=d.center(), text=t, pen_color=c) canvas.new_arrow(src=centroid([r.vertices()[2], r.vertices()[3]]), dst=centroid([d.vertices()[0], d.vertices()[1]])) canvas.new_rectangle(vertices=[ segs[0].vertices()[0], segs[-1].vertices()[1], segs[-1].vertices()[2], segs[0].vertices()[3] ]) for x in segs[1:]: canvas.new_line(src=x.vertices()[0], dst=x.vertices()[3])
def epoch_m(canvas, leftmost): texts = ['header', 'op', '\\dots'] rects = repeat(leftmost + Point(HEADER_WIDTH, 0)) rects = accumulate(rects, lambda x, _: x + Point(SEG_WIDTH, 0)) rects = islice(rects, len(texts) + 1) rects = [ geo.Rectangle(center=x + Point(SEG_WIDTH / 2, 0), width=SEG_WIDTH, height=SEG_HEIGHT) for x in rects ] rects.insert( 0, geo.Rectangle(center=leftmost + Point(HEADER_WIDTH / 2, 0), width=HEADER_WIDTH, height=SEG_HEIGHT)) for x, t in zip(rects, texts): canvas.new_text(anchor=x.center(), text=t) canvas.new_rectangle(vertices=[ rects[0].vertices()[0], rects[2].vertices()[1], rects[2].vertices()[2], rects[0].vertices()[3] ]) for x in rects[1:3]: canvas.new_line(src=x.vertices()[0], dst=x.vertices()[3]) v_srcs = rects[3].vertices() v_dsts = v_srcs[1:] for s, t in zip(v_srcs, v_dsts): canvas.new_line(src=s, dst=t, line_style=line_styles.DASHED) canvas.new_arrow(src=rects[-1].center(), dst=rects[-2].center()) canvas.new_text(anchor=rects[-1].center(), text='$\\text{op}_{m,i}$', location=locations.EAST)
def text_points(self): start = self.upleft() + \ Point(Leaf.SEG_WIDTH / 2, -Leaf.KEY_HEIGHT / 2) vs = repeat(start) vs = accumulate(vs, lambda x, _: x + Point(Leaf.SEG_WIDTH, 0)) vs = islice(vs, BRANCH - 1) return list(vs)
def epoch0(canvas, leftmost): texts = ['header', 'op', '\\dots', 'idx', 'op', '\\dots', 'idx', 'footer'] rects = repeat(leftmost + Point(HEADER_WIDTH, 0)) rects = accumulate(rects, lambda x, _: x + Point(SEG_WIDTH, 0)) rects = islice(rects, len(texts) - 2) rects = [ geo.Rectangle(center=x + Point(SEG_WIDTH / 2, 0), width=SEG_WIDTH, height=SEG_HEIGHT) for x in rects ] rects.insert( 0, geo.Rectangle(center=leftmost + Point(HEADER_WIDTH / 2, 0), width=HEADER_WIDTH, height=SEG_HEIGHT)) rects.append( geo.Rectangle(center=rects[-1].center() + Point(SEG_WIDTH / 2 + FOOTER_WIDTH / 2, 0), width=FOOTER_WIDTH, height=SEG_HEIGHT)) for x, t in zip(rects, texts): canvas.new_text(anchor=x.center(), text=t) canvas.new_rectangle(vertices=[ rects[0].vertices()[0], rects[-1].vertices()[1], rects[-1].vertices() [2], rects[0].vertices()[3] ]) for x in rects[1:]: canvas.new_line(src=x.vertices()[0], dst=x.vertices()[3])
def _draw_texts(self, canvas, upleft): start = upleft + \ Point(Inner.LEFTMOST_WIDTH + Inner.SEG_WIDTH / 2, - Inner.KEY_HEIGHT / 2) vs = repeat(start) vs = accumulate(vs, lambda x, _: x + Point(Inner.SEG_WIDTH, 0)) for p, t in zip(vs, self._texts): canvas.new_text(anchor=p, text=t)
def key(canvas, rects): p = Point(1, 1.5) canvas.new_text(anchor=p, text='$k$', location=locations.NORTH) canvas.new_arrow(src=p, dst=rects[0]) canvas.new_arrow(src=p, dst=rects[2]) canvas.new_arrow(src=p, dst=rects[5]) canvas.new_text(anchor=Point(0.2, 0.7), text='\\scriptsize $h_0(k)$') canvas.new_text(anchor=Point(1.2, 0.5), text='\\scriptsize $h_1(k)$') canvas.new_text(anchor=Point(1.8, 0.9), text='\\scriptsize $h_2(k)$')
def bbox(ps): minx = min(v.x for v in ps) miny = min(v.y for v in ps) maxx = max(v.x for v in ps) maxy = max(v.y for v in ps) return [ Point(minx, maxy), Point(maxx, maxy), Point(maxx, miny), Point(minx, miny)]
def pointers(self, canvas): if self._ptrs is not None: return self._ptrs upleft = self._center + Point(-self._width / 2, self._height / 2) start = upleft + Point(Leaf.SEG_WIDTH/2, -Leaf.KEY_HEIGHT - Leaf.PTR_HEIGHT / 2) vs = repeat(start) vs = accumulate(vs, lambda x, _: x + Point(Leaf.SEG_WIDTH, 0)) vs = islice(vs, BRANCH - 1) self._ptrs = [canvas.new_bullet(center=x) for x in vs] return self._ptrs
def level1(canvas): center_y = (SEPS_Y[0] + SEPS_Y[1]) / 2 canvas.new_text(anchor=Point(0, center_y), text='$L_1$', location=locations.EAST) xs = [2, 5, 8] for x in xs: canvas.new_triangle(center=Point(x, center_y), width=2, height=0.8) for x0, x1 in zip(xs, xs[1:]): x = (x0 + x1) / 2 canvas.new_line(src=Point(x, center_y + 0.7), dst=Point(x, center_y - 0.6), line_style=line_styles.DASHED)
def mem(canvas): y = SEPS_Y[2] + 0.7 active = canvas.new_triangle(center=Point(4.5, y), width=1, height=0.4) vs = active.get_skeleton().vertices() canvas.new_text(anchor=geo.centroid([vs[0], vs[2]]), text='\\scriptsize active memtable', location=locations.SOUTH) rect_width = 0.4 rect_height = 0.4 oplog = canvas.new_rectangle(center=Point(2, y), width=rect_width * 4, height=rect_height) canvas.new_arrow(src=oplog, dst=active, pen_color=colors.BLUE) vs = oplog.get_skeleton().vertices() xs = repeat(vs[0]) xs = accumulate(xs, lambda x, _: x + Point(rect_width, 0)) xs = islice(xs, 1, 4) for x in xs: canvas.new_line(src=x, dst=x + Point(0, -rect_height)) canvas.new_text(anchor=geo.centroid([vs[2], vs[3]]), text='\\scriptsize oplog', location=locations.SOUTH) rightmost_oplog = geo.Rectangle(center=vs[1] + Point(-rect_width / 2, -rect_height / 2), width=rect_width, height=rect_height) write_pos = rightmost_oplog.center() + Point(0, 0.5) canvas.new_text(anchor=write_pos, text='\\scriptsize write', location=locations.NORTH, pen_color=colors.BLUE) canvas.new_arrow(src=write_pos, dst=rightmost_oplog.intersect_from_center(write_pos), pen_color=colors.BLUE) shadow = canvas.new_triangle(center=Point(7, y), width=1, height=0.4) vs = shadow.get_skeleton().vertices() canvas.new_text(anchor=geo.centroid([vs[0], vs[2]]), text='\\scriptsize shadowed memtable', location=locations.SOUTH) canvas.new_arrow(src=active, dst=shadow, pen_color=colors.BLUE, line_style=line_styles.DASHED) canvas.new_arrow(src=shadow, dst=Point(7, 4.5), pen_color=colors.BLUE, line_style=line_styles.DASHED) canvas.new_text(anchor=Point(7, 5), text='dump', location=locations.SOUTHEAST)
def column_data(canvas, leftmost, texts): CELL_WIDTH = 0.8 CELL_HEIGHT = 0.5 ps = repeat(leftmost + Point(CELL_WIDTH / 2, 0)) ps = accumulate(ps, lambda x, _: x + Point(CELL_WIDTH, 0)) ps = islice(ps, len(texts)) ps = list(ps) for p, t in zip(ps, texts): canvas.new_text(anchor=p, text=t) canvas.new_rectangle(center=centroid([ps[0], ps[-1]]), width=CELL_WIDTH * len(ps), height=CELL_HEIGHT)
def rectangle_pen_color(): canvas = tikz.Canvas() canvas.new_rectangle(center=Point(1, 1), width=1, height=2, pen_color=colors.RED) return canvas.draw()
def rectangle_line_style(): canvas = tikz.Canvas() canvas.new_rectangle(center=Point(1, 1), width=1, height=2, line_style=line_styles.DASHED) return canvas.draw()
def scaled_color(): canvas = tikz.Canvas() canvas.new_circle(center=Point(1, 1), radius=1, pen_color=colors.RED.scale(0.5), brush_color=colors.BLACK.scale(0.5)) return canvas.draw()
def line_to_shape(): canvas = tikz.Canvas() c1 = canvas.new_circle(center=Point(3, 0), radius=1, pen_color=colors.INVISIBLE) canvas.new_line(src=ORIGIN, dst=c1) return canvas.draw()
def mixed_color(): canvas = tikz.Canvas() canvas.new_circle(center=Point(1, 1), radius=1, pen_color=colors.RED.mix(colors.GREEN), brush_color=colors.BLACK.mix(colors.WHITE)) return canvas.draw()
def file_format(canvas): SEG_HEIGHT = 0.8 SEG_WIDTH = 2 TEXTS = [ 'File Header', 'Seg Header', 'Column Data', 'Column Index', 'Seg Footer', '\\dots', 'File Footer' ] vs = repeat(ORIGIN) vs = accumulate(vs, lambda x, _: x + Point(0, -SEG_HEIGHT)) vs = islice(vs, len(TEXTS)) vs = list(vs) res = [] for p, t in zip(vs, TEXTS): if t.startswith('File'): r = canvas.new_rectangle(center=p, width=SEG_WIDTH, height=SEG_HEIGHT, brush_color=colors.LIME) else: r = canvas.new_rectangle(center=p, width=SEG_WIDTH, height=SEG_HEIGHT, brush_color=colors.ORANGE) res.append(r) for p, t in zip(vs, TEXTS): canvas.new_text(anchor=p, text=t) return res
def fill_circle(): canvas = tikz.Canvas() canvas.new_circle(center=Point(1, 1), radius=1, pen_color=colors.INVISIBLE, brush_color=colors.RED) return canvas.draw()
def _draw_lines(self, canvas, upleft): line_color = self._line_color if self._line_color is not None else colors.BLACK canvas.new_rectangle( center=self._center, width=self._width, height=self._height, pen_color=line_color) canvas.new_line( src=upleft + Point(0, -Inner.KEY_HEIGHT), dst=upleft + Point(self._width, -Inner.KEY_HEIGHT), pen_color=line_color) vs = repeat(upleft + Point(Inner.LEFTMOST_WIDTH, 0)) vs = accumulate(vs, lambda x, _: x + Point(Inner.SEG_WIDTH, 0)) vs = islice(vs, BRANCH - 1) for x in vs: canvas.new_line(src=x, dst=x + Point(0, -self._height))
def _draw_lines(self, canvas, upleft): color = self._line_color if self._line_color is not None else colors.BLACK canvas.new_rectangle( center=self._center, width=self._width, height=self._height, pen_color=color) canvas.new_line( src=upleft + Point(0, -Leaf.KEY_HEIGHT), dst=upleft + Point(self._width - Leaf.RIGHTMOST_WIDTH, -Leaf.KEY_HEIGHT), pen_color=color) vs = repeat(upleft) vs = accumulate(vs, lambda x, _: x + Point(Leaf.SEG_WIDTH, 0)) vs = islice(vs, 1, BRANCH) for p in vs: canvas.new_line( src=p, dst=p + Point(0, -self._height), pen_color=color)
def bitmap(canvas): width = 0.4 height = 0.4 rects = repeat(ORIGIN) rects = accumulate(rects, lambda x, _: x + Point(width, 0)) rects = islice(rects, 7) rects = [canvas.new_rectangle( center=x, width=width, height=height) for x in rects] return rects
def _draw_upleft_corner(self, canvas, upleft): center = upleft + \ Point(Inner.LEFTMOST_WIDTH / 2, -Inner.KEY_HEIGHT / 2) canvas.new_rectangle( center=center, width=Inner.LEFTMOST_WIDTH, height=Inner.KEY_HEIGHT, pen_color=colors.INVISIBLE, brush_color=colors.GRAY)
def pointers(self, canvas): if self._ptrs is not None: return self._ptrs upleft_corner = self._center + \ Point(-self._width / 2, self._height / 2) ptrs = repeat(upleft_corner + Point(Inner.LEFTMOST_WIDTH + Inner.SEG_WIDTH / 2, - Inner.KEY_HEIGHT - Inner.PTR_HEIGHT / 2)) ptrs = accumulate(ptrs, lambda x, _: x + Point(Inner.SEG_WIDTH, 0)) ptrs = islice(ptrs, len(self._texts)) ptrs = [canvas.new_bullet(center=p) for p in ptrs] p = canvas.new_bullet( center=upleft_corner + Point(Inner.LEFTMOST_WIDTH/2, - Inner.KEY_HEIGHT - Inner.PTR_HEIGHT / 2)) ptrs.insert(0, p) self._ptrs = ptrs return self._ptrs
def rectangle_rounded_corners(): canvas = tikz.Canvas() canvas.new_rectangle(center=Point(1, 1), width=1, height=2, pen_color=colors.BLACK, brush_color=colors.RED, corner_style=corner_styles.DEFAULT_ROUNDED) return canvas.draw()
def node(canvas, center, **kws): SEG_WIDTH = 0.5 SEG_HEIGHT = 0.4 vs = repeat(center + Point(-SEG_WIDTH / 2, 0)) vs = accumulate(vs, lambda x, _: x + Point(SEG_WIDTH, 0)) vs = islice(vs, 2) vs = [Rectangle(center=x, width=SEG_WIDTH, height=SEG_HEIGHT) for x in vs] canvas.new_rectangle( center=center, width=SEG_WIDTH * 2, height=SEG_HEIGHT, **kws) canvas.new_line( src=center + Point(0, SEG_HEIGHT / 2), dst=center + Point(0, -SEG_HEIGHT / 2), **kws) return vs
def fraction(canvas, center, value, text_color): SEG_NUM = 3 SEG_W = 0.2 SEG_H = 0.4 ts = repeat((value, 1, 0)) ts = accumulate( ts, lambda x, _: (x[0], x[1] / 2, 0) if x[0] < x[1] else \ (x[0] - x[1], x[1] / 2, 1)) ts = drop(ts, 2) ts = ('{}'.format(x) for _, _, x in ts) rs = repeat(center + Point(-SEG_W * (SEG_NUM - 1) / 2, 0)) rs = accumulate(rs, lambda x, _: x + Point(SEG_W, 0)) rs = (Rectangle(center=x, width=SEG_W, height=SEG_H) for x in rs) rs = islice(rs, SEG_NUM) rs = list(rs) canvas.new_text(anchor=centroid([rs[0].vertices()[0], rs[0].vertices()[3]]), text='0.', location=locations.WEST) res = list(zip(rs, ts)) for r, t in res: canvas.new_text(anchor=r.center(), text=t, pen_color=text_color) canvas.new_rectangle(vertices=[ rs[0].vertices()[0], rs[-1].vertices()[1], rs[-1].vertices()[2], rs[0].vertices()[3] ]) for x in rs[1:]: canvas.new_line(src=x.vertices()[0], dst=x.vertices()[3]) return res
def index_tree(canvas): tree = layout.tree(['', ['l'], ['r']], root=Point(3.5, -1), h_sep=1, v_sep=1) root = tree[''] left = canvas.new_bullet(center=tree['l']) right = canvas.new_bullet(center=tree['r']) l = canvas.new_line(src=root, dst=left) canvas.new_text(anchor=centroid(l.get_skeleton().vertices()), text='age', location=locations.NORTHWEST) l = canvas.new_line(src=root, dst=right) canvas.new_text(anchor=centroid(l.get_skeleton().vertices()), text='name', location=locations.NORTHEAST) return root, left, right
text='a', location=locations.NORTHWEST) e = canvas.new_line(src=tree['ala'], dst=tree['alan']) canvas.new_text(anchor=e.get_skeleton().center(), text='n', location=locations.WEST) e = canvas.new_line(src=tree['al'], dst=tree['ali']) canvas.new_text(anchor=e.get_skeleton().center(), text='i', location=locations.NORTHEAST) e = canvas.new_line(src=tree['ali'], dst=tree['alic']) canvas.new_text(anchor=e.get_skeleton().center(), text='c', location=locations.EAST) e = canvas.new_line(src=tree['alic'], dst=tree['alice']) canvas.new_text(anchor=e.get_skeleton().center(), text='e', location=locations.EAST) ALICE_START_POINT = Point(3, -3.5) alice_docs = draw_doc_chain(canvas, ALICE_START_POINT, ['3', '4']) canvas.new_arrow(src=tree['alice'], dst=alice_docs[0], pen_color=colors.RED) ALAN_START_POINT = Point(3, -1.5) alan_docs = draw_doc_chain(canvas, ALAN_START_POINT, ['1', '2', '3']) canvas.new_arrow(src=tree['alan'], dst=alan_docs[0], pen_color=colors.RED) print(canvas.draw())
def draw_dashed_circle(): canvas = tikz.Canvas() canvas.new_circle(center=Point(1, 1), radius=1, line_style=line_styles.DASHED) return canvas.draw()
shawow_canvas = Canvas() tree['root'] = shawow_canvas.new_rectangle(center=tree['root'], width=1, height=0.5) tree['left'] = shawow_canvas.new_rectangle(center=tree['left'], width=1, height=0.5) tree['right'] = shawow_canvas.new_rectangle(center=tree['right'], width=1, height=0.5) canvas.new_line(src=tree['root'], dst=tree['left']) canvas.new_line(src=tree['root'], dst=tree['right']) canvas.new_text(anchor=tree['root'].get_skeleton().center(), text='[18,21]') canvas.new_text(anchor=tree['left'].get_skeleton().center(), text='[18,18]', pen_color=colors.RED) canvas.new_text(anchor=tree['right'].get_skeleton().center(), text='[20,21]', pen_color=colors.RED) left_docs = draw_doc_chain(canvas, Point(3, 0), ['0', '4']) canvas.new_arrow(src=tree['left'], dst=left_docs[0], pen_color=colors.RED) right_docs = draw_doc_chain(canvas, Point(3, -2), ['1', '2', '3']) canvas.new_arrow(src=tree['right'], dst=right_docs[0], pen_color=colors.RED) print(canvas.draw())