Skip to content

Commit

Permalink
Optimize add()
Browse files Browse the repository at this point in the history
  • Loading branch information
vtereshkov committed Jun 20, 2022
1 parent f5922f5 commit 9915f32
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
2 changes: 1 addition & 1 deletion umplot.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ typedef struct
typedef struct
{
UmkaDynArray(Point) points;
int64_t actLen;
char *name;
Style style;
int64_t reserved;
} Series;


Expand Down
27 changes: 11 additions & 16 deletions umplot.um
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ type (

Series* = struct {
points: []Point
actLen: int
name: str
style: Style
reserved: int
}

Grid* = struct {
Expand Down Expand Up @@ -48,30 +48,25 @@ type (
)

fn (s: ^Series) clear*() {
s.points = []Point{}
}

fn (s: ^Series) reserve*(size: int) {
s.points = make([]Point, size)
s.reserved = size
s.points = make([]Point, 256)
s.actLen = 0
}

fn (s: ^Series) trim() {
if s.reserved > 0 {
s.points = slice(s.points, 0, len(s.points) - s.reserved)
s.reserved = 0
if s.actLen < len(s.points) {
s.points = slice(s.points, 0, s.actLen)
}
}

fn (s: ^Series) add*(x, y: real) {
if len(s.points) == 0 {
s.points = []Point{Point{x, y}}
} else if s.reserved > 0 {
s.points[len(s.points) - s.reserved] = Point{x, y}
s.reserved--
} else {
s.points = append(s.points, Point{x, y})
s.clear()
}
if s.actLen >= len(s.points) {
s.points = append(s.points, make([]Point, len(s.points)))
}
s.points[s.actLen] = Point{x, y}
s.actLen++
}

fn init*(numSeries: int = 1, kind: int = STYLE_LINE): Plot {
Expand Down

0 comments on commit 9915f32

Please sign in to comment.