diff options
| author | Grant Ammons <gammons@gmail.com> | 2017-03-07 08:50:00 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-03-07 08:50:00 -0500 |
| commit | b735a0cc90c6df666f59b7ee302dae27a6a7afeb (patch) | |
| tree | 8dc6d5376e1bf422a1aa693c27f505721741a1f0 /todolist/todo_list.go | |
| parent | 34931b46ed9ebb06e9555d6e62c98c70dc460d59 (diff) | |
| parent | 5763376a7ef50a116f9585ffd280bbf8e37621d3 (diff) | |
Merge pull request #37 from gammons/garbage-collect
Add garbage collection feature
Diffstat (limited to 'todolist/todo_list.go')
| -rw-r--r-- | todolist/todo_list.go | 34 |
1 files changed, 32 insertions, 2 deletions
diff --git a/todolist/todo_list.go b/todolist/todo_list.go index 71484c6..19fe0b7 100644 --- a/todolist/todo_list.go +++ b/todolist/todo_list.go @@ -92,14 +92,32 @@ func (t *TodoList) Todos() []*Todo { return t.Data } -func (t *TodoList) NextId() int { +func (t *TodoList) MaxId() int { maxId := 0 for _, todo := range t.Data { if todo.Id > maxId { maxId = todo.Id } } - return maxId + 1 + return maxId +} + +func (t *TodoList) NextId() int { + var found bool + maxID := t.MaxId() + for i := 1; i <= maxID; i++ { + found = false + for _, todo := range t.Data { + if todo.Id == i { + found = true + break + } + } + if !found { + return i + } + } + return maxID + 1 } func (t *TodoList) FindById(id int) *Todo { @@ -110,3 +128,15 @@ func (t *TodoList) FindById(id int) *Todo { } return nil } + +func (t *TodoList) GarbageCollect() { + var toDelete []*Todo + for _, todo := range t.Data { + if todo.Archived { + toDelete = append(toDelete, todo) + } + } + for _, todo := range toDelete { + t.Delete(todo.Id) + } +} |
