Gopher2600/database/select.go
JetSetIlly f98a775d37 regression and database improvements
added Replace() to database package

simplified Regressor interface

playback regressor can now be reduxed

regression Redux() now uses database.Replace() instead of separate
Delete() and Add(). this makes sure that the redux entry gets the same
database key once the redux has completed

removed -dryrun option from REGRESS REDUX

removed regression fails log. it wasn't well developed and not a
particularly useful idea

fixed television.SetSimple(). the signal function was not set correctly
2024-11-23 14:56:36 +00:00

77 lines
2.2 KiB
Go

// This file is part of Gopher2600.
//
// Gopher2600 is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Gopher2600 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Gopher2600. If not, see <https://www.gnu.org/licenses/>.
package database
// SelectAll entries in the database. onSelect can be nil.
//
// onSelect() should return true if select process is to continue. Continue
// flag is ignored if error is not nil.
//
// Returns last matched entry in selection or an error with the last entry
// matched before the error occurred.
func (db Session) SelectAll(onSelect func(Entry) error) (Entry, error) {
var entry Entry
if onSelect == nil {
onSelect = func(_ Entry) error { return nil }
}
keyList := db.SortedKeyList()
for k := range keyList {
entry := db.entries[keyList[k]]
err := onSelect(entry)
if err != nil {
return entry, err
}
}
return entry, nil
}
// SelectKeys matches entries with the specified key(s). keys can be singular.
// if list of keys is empty then all keys are matched (SelectAll() maybe more
// appropriate in that case). onSelect can be nil.
//
// onSelect() should return true if select process is to continue. If error is
// not nil then not continuing the select process is implied.
//
// Returns last matched entry in selection or an error with the last entry
// matched before the error occurred.
func (db Session) SelectKeys(onSelect func(Entry, int) error, keys ...int) (Entry, error) {
var entry Entry
if onSelect == nil {
onSelect = func(_ Entry, _ int) error { return nil }
}
if len(keys) == 0 {
keys = db.SortedKeyList()
}
for i := range keys {
entry = db.entries[keys[i]]
if entry == nil {
continue
}
err := onSelect(entry, keys[i])
if err != nil {
return entry, err
}
}
return entry, nil
}