Решение на Piece table от Исмаил Алиджиков

Обратно към всички решения

Към профила на Исмаил Алиджиков

Резултати

  • 10 точки от тестове
  • 0 бонус точки
  • 10 точки общо
  • 8 успешни тест(а)
  • 0 неуспешни тест(а)

Код

package main
type Editor interface {
// Insert text starting from given position.
Insert(position uint, text string) Editor
// Delete length items from offset.
Delete(offset, length uint) Editor
// Undo reverts latest change.
Undo() Editor
// Redo re-applies latest undone change.
Redo() Editor
// String returns complete representation of what a file looks
// like after all manipulations.
String() string
}
func (p PieceTableEditor) String() string {
var content []byte
for _, piece := range p.state.pieces {
if piece.origin {
content = append(content, p.origin[piece.offset:piece.offset+piece.length]...)
} else {
content = append(content, p.add[piece.offset:piece.offset+piece.length]...)
}
}
return string(content)
}
func (p PieceTableEditor) Insert(position uint, text string) Editor {
textBytes := []byte(text)
additionOffset := len(p.add)
addition := Piece{origin: false, offset: additionOffset, length: len(textBytes)}
p.add = append(p.add, textBytes...)
var piecesCopy []Piece
counter := 0
added := false
for _, piece := range p.state.pieces {
if int(position) < counter+piece.length && !added {
beforeLength := int(position) - counter
before := Piece{origin: piece.origin, offset: piece.offset, length: beforeLength}
after := Piece{origin: piece.origin, offset: piece.offset + beforeLength, length: piece.length - beforeLength}
piecesCopy = append(piecesCopy, before)
piecesCopy = append(piecesCopy, addition)
piecesCopy = append(piecesCopy, after)
added = true
} else if int(position) == counter+piece.length && !added {
piecesCopy = append(piecesCopy, piece)
piecesCopy = append(piecesCopy, addition)
added = true
} else {
piecesCopy = append(piecesCopy, piece)
}
counter += piece.length
}
if !added {
piecesCopy = append(piecesCopy, addition)
}
newState := &State{pieces: piecesCopy}
p.state.next = newState
newState.prev = p.state
p.state = newState
return p
}
func (p PieceTableEditor) Delete(offset, length uint) Editor {
var piecesCopy []Piece
counter := 0
toBeRemoved := -1
deleted := false
for _, piece := range p.state.pieces {
if toBeRemoved != -1 {
if piece.length < toBeRemoved {
toBeRemoved = toBeRemoved - piece.length
} else {
deletion := Piece{origin: piece.origin, offset: piece.offset + toBeRemoved, length: piece.length - toBeRemoved}
piecesCopy = append(piecesCopy, deletion)
toBeRemoved = -1
deleted = true
}
counter += piece.length
continue
}
if counter == int(offset) && !deleted {
if int(length) <= piece.length {
deletion := Piece{origin: piece.origin, offset: piece.offset + int(length), length: piece.length - int(length)}
piecesCopy = append(piecesCopy, deletion)
deleted = true
} else {
toBeRemoved = int(length) - piece.length
}
} else if int(offset) < counter+piece.length && !deleted {
if int(length) < piece.length {
beforeLength := int(offset) - counter
afterOffset := beforeLength + int(length) + piece.offset
before := Piece{origin: piece.origin, offset: piece.offset, length: beforeLength}
after := Piece{origin: piece.origin, offset: afterOffset, length: (piece.length + piece.offset) - afterOffset}
piecesCopy = append(piecesCopy, before)
piecesCopy = append(piecesCopy, after)
deleted = true
} else {
deletion := Piece{origin: piece.origin, offset: piece.offset, length: int(offset)}
piecesCopy = append(piecesCopy, deletion)
toBeRemoved = int(length) - (piece.length - int(offset))
}
} else {
piecesCopy = append(piecesCopy, piece)
}
counter += piece.length
}
newState := &State{pieces: piecesCopy}
p.state.next = newState
newState.prev = p.state
p.state = newState
return p
}
func (p PieceTableEditor) Undo() Editor {
prev := p.state.prev
if prev != nil {
p.state = prev
}
return p
}
func (p PieceTableEditor) Redo() Editor {
next := p.state.next
if next != nil {
p.state = next
}
return p
}
type PieceTableEditor struct {
origin []byte
add []byte
state *State
}
type State struct {
pieces []Piece
prev *State
next *State
}
type Piece struct {
origin bool
offset int
length int
}
func NewEditor(content string) Editor {
editor := PieceTableEditor{origin: []byte(content)}
piece := Piece{origin: true, offset: 0, length: len(content)}
var pieces []Piece
pieces = append(pieces, piece)
editor.state = &State{pieces: pieces}
return editor
}

Лог от изпълнението

PASS
ok  	_/tmp/d20181107-53-1rctx8p	0.002s
PASS
ok  	_/tmp/d20181107-53-1rctx8p	0.002s
PASS
ok  	_/tmp/d20181107-53-1rctx8p	0.002s
PASS
ok  	_/tmp/d20181107-53-1rctx8p	0.002s
PASS
ok  	_/tmp/d20181107-53-1rctx8p	0.002s
PASS
ok  	_/tmp/d20181107-53-1rctx8p	0.002s
PASS
ok  	_/tmp/d20181107-53-1rctx8p	0.002s
PASS
ok  	_/tmp/d20181107-53-1rctx8p	0.002s

История (7 версии и 0 коментара)

Исмаил обнови решението на 04.11.2018 18:14 (преди 9 месеца)

+package main
+
+type Editor interface {
+ // Insert text starting from given position.
+ Insert(position uint, text string) Editor
+
+ // Delete length items from offset.
+ Delete(offset, length uint) Editor
+
+ // Undo reverts latest change.
+ Undo() Editor
+
+ // Redo re-applies latest undone change.
+ Redo() Editor
+
+ // String returns complete representation of what a file looks
+ // like after all manipulations.
+ String() string
+}
+
+func (p PieceTableEditor) String() string {
+ var content []byte
+ for _, piece := range p.state.pieces {
+ if piece.origin {
+ content = append(content, p.origin[piece.offset:piece.offset+piece.length]...)
+ } else {
+ content = append(content, p.add[piece.offset:piece.offset+piece.length]...)
+ }
+ }
+
+ return string(content)
+}
+
+func (p PieceTableEditor) Insert(position uint, text string) Editor {
+ textBytes := []byte(text)
+ additionOffset := len(p.add)
+ p.add = append(p.add, textBytes...)
+
+ counter := 0
+
+ var piecesCopy []Piece
+
+ isAdded := false
+ for _, piece := range p.state.pieces {
+ if int(position) < counter+piece.length && !isAdded {
+ beforeLength := int(position) - piece.offset
+
+ before := Piece{origin: true, offset: piece.offset, length: beforeLength}
+ addition := Piece{origin: false, offset: additionOffset, length: len(textBytes)}
+ after := Piece{origin: true, offset: int(position), length: piece.length - beforeLength}
+
+ piecesCopy = append(piecesCopy, before)
+ piecesCopy = append(piecesCopy, addition)
+ piecesCopy = append(piecesCopy, after)
+
+ isAdded = true
+ } else if int(position) == counter+piece.length && !isAdded {
+ addition := Piece{origin: false, offset: additionOffset, length: len(textBytes)}
+
+ piecesCopy = append(piecesCopy, piece)
+ piecesCopy = append(piecesCopy, addition)
+
+ isAdded = true
+ } else {
+ piecesCopy = append(piecesCopy, piece)
+ }
+
+ counter += piece.length
+ }
+
+ newState := &State{pieces: piecesCopy}
+
+ p.state.next = newState
+ newState.prev = p.state
+ p.state = newState
+
+ return p
+}
+
+func (p PieceTableEditor) Delete(offset, length uint) Editor {
+ var piecesCopy []Piece
+
+ counter := 0
+
+ deleted := false
+ for _, piece := range p.state.pieces {
+ if counter == int(offset) {
+ deletion := Piece{origin: piece.origin, offset: piece.offset + int(length), length: piece.length - int(length)}
+
+ piecesCopy = append(piecesCopy, deletion)
+
+ deleted = true
+ } else if int(offset) < counter+piece.length && !deleted {
+ beforeLength := int(offset) - counter
+ afterOffset := beforeLength + int(length) + piece.offset
+
+ before := Piece{origin: piece.origin, offset: piece.offset, length: beforeLength}
+ after := Piece{origin: piece.origin, offset: afterOffset, length: (piece.length + piece.offset) - afterOffset}
+
+ piecesCopy = append(piecesCopy, before)
+ piecesCopy = append(piecesCopy, after)
+
+ deleted = true
+ } else {
+ piecesCopy = append(piecesCopy, piece)
+ }
+
+ counter += piece.length
+ }
+
+ newState := &State{pieces: piecesCopy}
+
+ p.state.next = newState
+ newState.prev = p.state
+ p.state = newState
+
+ return p
+}
+
+func (p PieceTableEditor) Undo() Editor {
+ prev := p.state.prev
+ if prev != nil {
+ p.state = prev
+ }
+
+ return p
+}
+
+func (p PieceTableEditor) Redo() Editor {
+ next := p.state.next
+ if next != nil {
+ p.state = next
+ }
+
+ return p
+}
+
+type PieceTableEditor struct {
+ origin []byte
+ add []byte
+
+ state *State
+}
+
+type State struct {
+ pieces []Piece
+
+ prev *State
+ next *State
+}
+
+type Piece struct {
+ origin bool
+ offset int
+ length int
+}
+
+func NewEditor(content string) Editor {
+ editor := PieceTableEditor{origin: []byte(content)}
+ piece := Piece{origin: true, offset: 0, length: len(content)}
+
+ var pieces []Piece
+ pieces = append(pieces, piece)
+ editor.state = &State{pieces: pieces}
+
+ return editor
+}

Исмаил обнови решението на 04.11.2018 20:00 (преди 9 месеца)

package main
type Editor interface {
// Insert text starting from given position.
Insert(position uint, text string) Editor
// Delete length items from offset.
Delete(offset, length uint) Editor
// Undo reverts latest change.
Undo() Editor
// Redo re-applies latest undone change.
Redo() Editor
// String returns complete representation of what a file looks
// like after all manipulations.
String() string
}
func (p PieceTableEditor) String() string {
var content []byte
for _, piece := range p.state.pieces {
if piece.origin {
content = append(content, p.origin[piece.offset:piece.offset+piece.length]...)
} else {
content = append(content, p.add[piece.offset:piece.offset+piece.length]...)
}
}
return string(content)
}
func (p PieceTableEditor) Insert(position uint, text string) Editor {
textBytes := []byte(text)
additionOffset := len(p.add)
p.add = append(p.add, textBytes...)
counter := 0
var piecesCopy []Piece
- isAdded := false
+ added := false
for _, piece := range p.state.pieces {
- if int(position) < counter+piece.length && !isAdded {
+ if int(position) < counter+piece.length && !added {
beforeLength := int(position) - piece.offset
before := Piece{origin: true, offset: piece.offset, length: beforeLength}
addition := Piece{origin: false, offset: additionOffset, length: len(textBytes)}
after := Piece{origin: true, offset: int(position), length: piece.length - beforeLength}
piecesCopy = append(piecesCopy, before)
piecesCopy = append(piecesCopy, addition)
piecesCopy = append(piecesCopy, after)
- isAdded = true
- } else if int(position) == counter+piece.length && !isAdded {
+ added = true
+ } else if int(position) == counter+piece.length && !added {
addition := Piece{origin: false, offset: additionOffset, length: len(textBytes)}
piecesCopy = append(piecesCopy, piece)
piecesCopy = append(piecesCopy, addition)
- isAdded = true
+ added = true
} else {
piecesCopy = append(piecesCopy, piece)
}
counter += piece.length
}
newState := &State{pieces: piecesCopy}
p.state.next = newState
newState.prev = p.state
p.state = newState
return p
}
func (p PieceTableEditor) Delete(offset, length uint) Editor {
var piecesCopy []Piece
counter := 0
-
+ toBeRemoved := -1
deleted := false
+
for _, piece := range p.state.pieces {
- if counter == int(offset) {
+ if toBeRemoved != -1 {
+ if piece.length < toBeRemoved {
+ toBeRemoved = toBeRemoved - piece.length
+ } else {
+ deletion := Piece{origin: piece.origin, offset: piece.offset + toBeRemoved, length: piece.length - toBeRemoved}
+
+ piecesCopy = append(piecesCopy, deletion)
+ toBeRemoved = -1
+
+ deleted = true
+ }
+
+ counter += piece.length
+ continue
+ }
+
+ if counter == int(offset) && !deleted {
+
deletion := Piece{origin: piece.origin, offset: piece.offset + int(length), length: piece.length - int(length)}
piecesCopy = append(piecesCopy, deletion)
deleted = true
} else if int(offset) < counter+piece.length && !deleted {
- beforeLength := int(offset) - counter
- afterOffset := beforeLength + int(length) + piece.offset
+ if int(length) < piece.length {
+ beforeLength := int(offset) - counter
+ afterOffset := beforeLength + int(length) + piece.offset
- before := Piece{origin: piece.origin, offset: piece.offset, length: beforeLength}
- after := Piece{origin: piece.origin, offset: afterOffset, length: (piece.length + piece.offset) - afterOffset}
+ before := Piece{origin: piece.origin, offset: piece.offset, length: beforeLength}
+ after := Piece{origin: piece.origin, offset: afterOffset, length: (piece.length + piece.offset) - afterOffset}
- piecesCopy = append(piecesCopy, before)
- piecesCopy = append(piecesCopy, after)
+ piecesCopy = append(piecesCopy, before)
+ piecesCopy = append(piecesCopy, after)
- deleted = true
+ deleted = true
+ } else {
+ deletion := Piece{origin: piece.origin, offset: piece.offset, length: int(offset)}
+
+ piecesCopy = append(piecesCopy, deletion)
+ toBeRemoved = int(length) - (piece.length - int(offset))
+ }
} else {
piecesCopy = append(piecesCopy, piece)
}
counter += piece.length
}
newState := &State{pieces: piecesCopy}
p.state.next = newState
newState.prev = p.state
p.state = newState
return p
}
func (p PieceTableEditor) Undo() Editor {
prev := p.state.prev
if prev != nil {
p.state = prev
}
return p
}
func (p PieceTableEditor) Redo() Editor {
next := p.state.next
if next != nil {
p.state = next
}
return p
}
type PieceTableEditor struct {
origin []byte
add []byte
state *State
}
type State struct {
pieces []Piece
prev *State
next *State
}
type Piece struct {
origin bool
offset int
length int
}
func NewEditor(content string) Editor {
editor := PieceTableEditor{origin: []byte(content)}
piece := Piece{origin: true, offset: 0, length: len(content)}
var pieces []Piece
pieces = append(pieces, piece)
editor.state = &State{pieces: pieces}
return editor
}

Исмаил обнови решението на 04.11.2018 20:15 (преди 9 месеца)

package main
type Editor interface {
// Insert text starting from given position.
Insert(position uint, text string) Editor
// Delete length items from offset.
Delete(offset, length uint) Editor
// Undo reverts latest change.
Undo() Editor
// Redo re-applies latest undone change.
Redo() Editor
// String returns complete representation of what a file looks
// like after all manipulations.
String() string
}
func (p PieceTableEditor) String() string {
var content []byte
for _, piece := range p.state.pieces {
if piece.origin {
content = append(content, p.origin[piece.offset:piece.offset+piece.length]...)
} else {
content = append(content, p.add[piece.offset:piece.offset+piece.length]...)
}
}
return string(content)
}
func (p PieceTableEditor) Insert(position uint, text string) Editor {
textBytes := []byte(text)
additionOffset := len(p.add)
p.add = append(p.add, textBytes...)
counter := 0
var piecesCopy []Piece
added := false
for _, piece := range p.state.pieces {
if int(position) < counter+piece.length && !added {
beforeLength := int(position) - piece.offset
before := Piece{origin: true, offset: piece.offset, length: beforeLength}
addition := Piece{origin: false, offset: additionOffset, length: len(textBytes)}
after := Piece{origin: true, offset: int(position), length: piece.length - beforeLength}
piecesCopy = append(piecesCopy, before)
piecesCopy = append(piecesCopy, addition)
piecesCopy = append(piecesCopy, after)
added = true
} else if int(position) == counter+piece.length && !added {
addition := Piece{origin: false, offset: additionOffset, length: len(textBytes)}
piecesCopy = append(piecesCopy, piece)
piecesCopy = append(piecesCopy, addition)
added = true
} else {
piecesCopy = append(piecesCopy, piece)
}
counter += piece.length
}
newState := &State{pieces: piecesCopy}
p.state.next = newState
newState.prev = p.state
p.state = newState
return p
}
func (p PieceTableEditor) Delete(offset, length uint) Editor {
var piecesCopy []Piece
counter := 0
toBeRemoved := -1
deleted := false
for _, piece := range p.state.pieces {
if toBeRemoved != -1 {
if piece.length < toBeRemoved {
toBeRemoved = toBeRemoved - piece.length
} else {
deletion := Piece{origin: piece.origin, offset: piece.offset + toBeRemoved, length: piece.length - toBeRemoved}
piecesCopy = append(piecesCopy, deletion)
toBeRemoved = -1
deleted = true
}
counter += piece.length
continue
}
if counter == int(offset) && !deleted {
+ if int(length) <= piece.length {
+ deletion := Piece{origin: piece.origin, offset: piece.offset + int(length), length: piece.length - int(length)}
- deletion := Piece{origin: piece.origin, offset: piece.offset + int(length), length: piece.length - int(length)}
+ piecesCopy = append(piecesCopy, deletion)
- piecesCopy = append(piecesCopy, deletion)
-
- deleted = true
+ deleted = true
+ } else {
+ toBeRemoved = int(length)
+ }
} else if int(offset) < counter+piece.length && !deleted {
if int(length) < piece.length {
beforeLength := int(offset) - counter
afterOffset := beforeLength + int(length) + piece.offset
before := Piece{origin: piece.origin, offset: piece.offset, length: beforeLength}
after := Piece{origin: piece.origin, offset: afterOffset, length: (piece.length + piece.offset) - afterOffset}
piecesCopy = append(piecesCopy, before)
piecesCopy = append(piecesCopy, after)
deleted = true
} else {
deletion := Piece{origin: piece.origin, offset: piece.offset, length: int(offset)}
piecesCopy = append(piecesCopy, deletion)
toBeRemoved = int(length) - (piece.length - int(offset))
}
} else {
piecesCopy = append(piecesCopy, piece)
}
counter += piece.length
}
newState := &State{pieces: piecesCopy}
p.state.next = newState
newState.prev = p.state
p.state = newState
return p
}
func (p PieceTableEditor) Undo() Editor {
prev := p.state.prev
if prev != nil {
p.state = prev
}
return p
}
func (p PieceTableEditor) Redo() Editor {
next := p.state.next
if next != nil {
p.state = next
}
return p
}
type PieceTableEditor struct {
origin []byte
add []byte
state *State
}
type State struct {
pieces []Piece
prev *State
next *State
}
type Piece struct {
origin bool
offset int
length int
}
func NewEditor(content string) Editor {
editor := PieceTableEditor{origin: []byte(content)}
piece := Piece{origin: true, offset: 0, length: len(content)}
var pieces []Piece
pieces = append(pieces, piece)
editor.state = &State{pieces: pieces}
return editor
}

Исмаил обнови решението на 05.11.2018 22:43 (преди 9 месеца)

package main
type Editor interface {
// Insert text starting from given position.
Insert(position uint, text string) Editor
// Delete length items from offset.
Delete(offset, length uint) Editor
// Undo reverts latest change.
Undo() Editor
// Redo re-applies latest undone change.
Redo() Editor
// String returns complete representation of what a file looks
// like after all manipulations.
String() string
}
func (p PieceTableEditor) String() string {
var content []byte
for _, piece := range p.state.pieces {
if piece.origin {
content = append(content, p.origin[piece.offset:piece.offset+piece.length]...)
} else {
content = append(content, p.add[piece.offset:piece.offset+piece.length]...)
}
}
return string(content)
}
func (p PieceTableEditor) Insert(position uint, text string) Editor {
textBytes := []byte(text)
additionOffset := len(p.add)
p.add = append(p.add, textBytes...)
counter := 0
var piecesCopy []Piece
added := false
for _, piece := range p.state.pieces {
if int(position) < counter+piece.length && !added {
- beforeLength := int(position) - piece.offset
+ beforeLength := int(position) - counter - piece.offset
before := Piece{origin: true, offset: piece.offset, length: beforeLength}
addition := Piece{origin: false, offset: additionOffset, length: len(textBytes)}
- after := Piece{origin: true, offset: int(position), length: piece.length - beforeLength}
+ after := Piece{origin: true, offset: beforeLength, length: piece.length - beforeLength}
piecesCopy = append(piecesCopy, before)
piecesCopy = append(piecesCopy, addition)
piecesCopy = append(piecesCopy, after)
added = true
} else if int(position) == counter+piece.length && !added {
addition := Piece{origin: false, offset: additionOffset, length: len(textBytes)}
piecesCopy = append(piecesCopy, piece)
piecesCopy = append(piecesCopy, addition)
added = true
} else {
piecesCopy = append(piecesCopy, piece)
}
counter += piece.length
}
newState := &State{pieces: piecesCopy}
p.state.next = newState
newState.prev = p.state
p.state = newState
return p
}
func (p PieceTableEditor) Delete(offset, length uint) Editor {
var piecesCopy []Piece
counter := 0
toBeRemoved := -1
deleted := false
for _, piece := range p.state.pieces {
if toBeRemoved != -1 {
if piece.length < toBeRemoved {
toBeRemoved = toBeRemoved - piece.length
} else {
deletion := Piece{origin: piece.origin, offset: piece.offset + toBeRemoved, length: piece.length - toBeRemoved}
piecesCopy = append(piecesCopy, deletion)
toBeRemoved = -1
deleted = true
}
counter += piece.length
continue
}
if counter == int(offset) && !deleted {
if int(length) <= piece.length {
deletion := Piece{origin: piece.origin, offset: piece.offset + int(length), length: piece.length - int(length)}
piecesCopy = append(piecesCopy, deletion)
deleted = true
} else {
- toBeRemoved = int(length)
+ toBeRemoved = int(length) - piece.length
}
} else if int(offset) < counter+piece.length && !deleted {
if int(length) < piece.length {
beforeLength := int(offset) - counter
afterOffset := beforeLength + int(length) + piece.offset
before := Piece{origin: piece.origin, offset: piece.offset, length: beforeLength}
after := Piece{origin: piece.origin, offset: afterOffset, length: (piece.length + piece.offset) - afterOffset}
piecesCopy = append(piecesCopy, before)
piecesCopy = append(piecesCopy, after)
deleted = true
} else {
deletion := Piece{origin: piece.origin, offset: piece.offset, length: int(offset)}
piecesCopy = append(piecesCopy, deletion)
toBeRemoved = int(length) - (piece.length - int(offset))
}
} else {
piecesCopy = append(piecesCopy, piece)
}
counter += piece.length
}
newState := &State{pieces: piecesCopy}
p.state.next = newState
newState.prev = p.state
p.state = newState
+
+ //fmt.Println("%v", piecesCopy)
return p
}
func (p PieceTableEditor) Undo() Editor {
prev := p.state.prev
if prev != nil {
p.state = prev
}
return p
}
func (p PieceTableEditor) Redo() Editor {
next := p.state.next
if next != nil {
p.state = next
}
return p
}
type PieceTableEditor struct {
origin []byte
add []byte
state *State
}
type State struct {
pieces []Piece
prev *State
next *State
}
type Piece struct {
origin bool
offset int
length int
}
func NewEditor(content string) Editor {
editor := PieceTableEditor{origin: []byte(content)}
piece := Piece{origin: true, offset: 0, length: len(content)}
var pieces []Piece
pieces = append(pieces, piece)
editor.state = &State{pieces: pieces}
return editor
}

Исмаил обнови решението на 05.11.2018 22:44 (преди 9 месеца)

package main
type Editor interface {
// Insert text starting from given position.
Insert(position uint, text string) Editor
// Delete length items from offset.
Delete(offset, length uint) Editor
// Undo reverts latest change.
Undo() Editor
// Redo re-applies latest undone change.
Redo() Editor
// String returns complete representation of what a file looks
// like after all manipulations.
String() string
}
func (p PieceTableEditor) String() string {
var content []byte
for _, piece := range p.state.pieces {
if piece.origin {
content = append(content, p.origin[piece.offset:piece.offset+piece.length]...)
} else {
content = append(content, p.add[piece.offset:piece.offset+piece.length]...)
}
}
return string(content)
}
func (p PieceTableEditor) Insert(position uint, text string) Editor {
textBytes := []byte(text)
additionOffset := len(p.add)
p.add = append(p.add, textBytes...)
counter := 0
var piecesCopy []Piece
added := false
for _, piece := range p.state.pieces {
if int(position) < counter+piece.length && !added {
beforeLength := int(position) - counter - piece.offset
before := Piece{origin: true, offset: piece.offset, length: beforeLength}
addition := Piece{origin: false, offset: additionOffset, length: len(textBytes)}
after := Piece{origin: true, offset: beforeLength, length: piece.length - beforeLength}
piecesCopy = append(piecesCopy, before)
piecesCopy = append(piecesCopy, addition)
piecesCopy = append(piecesCopy, after)
added = true
} else if int(position) == counter+piece.length && !added {
addition := Piece{origin: false, offset: additionOffset, length: len(textBytes)}
piecesCopy = append(piecesCopy, piece)
piecesCopy = append(piecesCopy, addition)
added = true
} else {
piecesCopy = append(piecesCopy, piece)
}
counter += piece.length
}
newState := &State{pieces: piecesCopy}
p.state.next = newState
newState.prev = p.state
p.state = newState
return p
}
func (p PieceTableEditor) Delete(offset, length uint) Editor {
var piecesCopy []Piece
counter := 0
toBeRemoved := -1
deleted := false
for _, piece := range p.state.pieces {
if toBeRemoved != -1 {
if piece.length < toBeRemoved {
toBeRemoved = toBeRemoved - piece.length
} else {
deletion := Piece{origin: piece.origin, offset: piece.offset + toBeRemoved, length: piece.length - toBeRemoved}
piecesCopy = append(piecesCopy, deletion)
toBeRemoved = -1
deleted = true
}
counter += piece.length
continue
}
if counter == int(offset) && !deleted {
if int(length) <= piece.length {
deletion := Piece{origin: piece.origin, offset: piece.offset + int(length), length: piece.length - int(length)}
piecesCopy = append(piecesCopy, deletion)
deleted = true
} else {
toBeRemoved = int(length) - piece.length
}
} else if int(offset) < counter+piece.length && !deleted {
if int(length) < piece.length {
beforeLength := int(offset) - counter
afterOffset := beforeLength + int(length) + piece.offset
before := Piece{origin: piece.origin, offset: piece.offset, length: beforeLength}
after := Piece{origin: piece.origin, offset: afterOffset, length: (piece.length + piece.offset) - afterOffset}
piecesCopy = append(piecesCopy, before)
piecesCopy = append(piecesCopy, after)
deleted = true
} else {
deletion := Piece{origin: piece.origin, offset: piece.offset, length: int(offset)}
piecesCopy = append(piecesCopy, deletion)
toBeRemoved = int(length) - (piece.length - int(offset))
}
} else {
piecesCopy = append(piecesCopy, piece)
}
counter += piece.length
}
newState := &State{pieces: piecesCopy}
p.state.next = newState
newState.prev = p.state
p.state = newState
- //fmt.Println("%v", piecesCopy)
-
return p
}
func (p PieceTableEditor) Undo() Editor {
prev := p.state.prev
if prev != nil {
p.state = prev
}
return p
}
func (p PieceTableEditor) Redo() Editor {
next := p.state.next
if next != nil {
p.state = next
}
return p
}
type PieceTableEditor struct {
origin []byte
add []byte
state *State
}
type State struct {
pieces []Piece
prev *State
next *State
}
type Piece struct {
origin bool
offset int
length int
}
func NewEditor(content string) Editor {
editor := PieceTableEditor{origin: []byte(content)}
piece := Piece{origin: true, offset: 0, length: len(content)}
var pieces []Piece
pieces = append(pieces, piece)
editor.state = &State{pieces: pieces}
return editor
}

Исмаил обнови решението на 05.11.2018 23:07 (преди 9 месеца)

package main
type Editor interface {
// Insert text starting from given position.
Insert(position uint, text string) Editor
// Delete length items from offset.
Delete(offset, length uint) Editor
// Undo reverts latest change.
Undo() Editor
// Redo re-applies latest undone change.
Redo() Editor
// String returns complete representation of what a file looks
// like after all manipulations.
String() string
}
func (p PieceTableEditor) String() string {
var content []byte
for _, piece := range p.state.pieces {
if piece.origin {
content = append(content, p.origin[piece.offset:piece.offset+piece.length]...)
} else {
content = append(content, p.add[piece.offset:piece.offset+piece.length]...)
}
}
return string(content)
}
func (p PieceTableEditor) Insert(position uint, text string) Editor {
textBytes := []byte(text)
additionOffset := len(p.add)
p.add = append(p.add, textBytes...)
counter := 0
var piecesCopy []Piece
added := false
for _, piece := range p.state.pieces {
if int(position) < counter+piece.length && !added {
- beforeLength := int(position) - counter - piece.offset
+ beforeLength := int(position) - counter
- before := Piece{origin: true, offset: piece.offset, length: beforeLength}
+ before := Piece{origin: piece.origin, offset: piece.offset, length: beforeLength}
addition := Piece{origin: false, offset: additionOffset, length: len(textBytes)}
- after := Piece{origin: true, offset: beforeLength, length: piece.length - beforeLength}
+ after := Piece{origin: piece.origin, offset: piece.offset + beforeLength, length: piece.length - beforeLength}
piecesCopy = append(piecesCopy, before)
piecesCopy = append(piecesCopy, addition)
piecesCopy = append(piecesCopy, after)
added = true
} else if int(position) == counter+piece.length && !added {
addition := Piece{origin: false, offset: additionOffset, length: len(textBytes)}
piecesCopy = append(piecesCopy, piece)
piecesCopy = append(piecesCopy, addition)
added = true
} else {
piecesCopy = append(piecesCopy, piece)
}
counter += piece.length
}
newState := &State{pieces: piecesCopy}
p.state.next = newState
newState.prev = p.state
p.state = newState
return p
}
func (p PieceTableEditor) Delete(offset, length uint) Editor {
var piecesCopy []Piece
counter := 0
toBeRemoved := -1
deleted := false
for _, piece := range p.state.pieces {
if toBeRemoved != -1 {
if piece.length < toBeRemoved {
toBeRemoved = toBeRemoved - piece.length
} else {
deletion := Piece{origin: piece.origin, offset: piece.offset + toBeRemoved, length: piece.length - toBeRemoved}
piecesCopy = append(piecesCopy, deletion)
toBeRemoved = -1
deleted = true
}
counter += piece.length
continue
}
if counter == int(offset) && !deleted {
if int(length) <= piece.length {
deletion := Piece{origin: piece.origin, offset: piece.offset + int(length), length: piece.length - int(length)}
piecesCopy = append(piecesCopy, deletion)
deleted = true
} else {
toBeRemoved = int(length) - piece.length
}
} else if int(offset) < counter+piece.length && !deleted {
if int(length) < piece.length {
beforeLength := int(offset) - counter
afterOffset := beforeLength + int(length) + piece.offset
before := Piece{origin: piece.origin, offset: piece.offset, length: beforeLength}
after := Piece{origin: piece.origin, offset: afterOffset, length: (piece.length + piece.offset) - afterOffset}
piecesCopy = append(piecesCopy, before)
piecesCopy = append(piecesCopy, after)
deleted = true
} else {
deletion := Piece{origin: piece.origin, offset: piece.offset, length: int(offset)}
piecesCopy = append(piecesCopy, deletion)
toBeRemoved = int(length) - (piece.length - int(offset))
}
} else {
piecesCopy = append(piecesCopy, piece)
}
counter += piece.length
}
newState := &State{pieces: piecesCopy}
p.state.next = newState
newState.prev = p.state
p.state = newState
return p
}
func (p PieceTableEditor) Undo() Editor {
prev := p.state.prev
if prev != nil {
p.state = prev
}
return p
}
func (p PieceTableEditor) Redo() Editor {
next := p.state.next
if next != nil {
p.state = next
}
return p
}
type PieceTableEditor struct {
origin []byte
add []byte
state *State
}
type State struct {
pieces []Piece
prev *State
next *State
}
type Piece struct {
origin bool
offset int
length int
}
func NewEditor(content string) Editor {
editor := PieceTableEditor{origin: []byte(content)}
piece := Piece{origin: true, offset: 0, length: len(content)}
var pieces []Piece
pieces = append(pieces, piece)
editor.state = &State{pieces: pieces}
return editor
}

Исмаил обнови решението на 07.11.2018 12:25 (преди 9 месеца)

package main
type Editor interface {
// Insert text starting from given position.
Insert(position uint, text string) Editor
// Delete length items from offset.
Delete(offset, length uint) Editor
// Undo reverts latest change.
Undo() Editor
// Redo re-applies latest undone change.
Redo() Editor
// String returns complete representation of what a file looks
// like after all manipulations.
String() string
}
func (p PieceTableEditor) String() string {
var content []byte
for _, piece := range p.state.pieces {
if piece.origin {
content = append(content, p.origin[piece.offset:piece.offset+piece.length]...)
} else {
content = append(content, p.add[piece.offset:piece.offset+piece.length]...)
}
}
return string(content)
}
func (p PieceTableEditor) Insert(position uint, text string) Editor {
textBytes := []byte(text)
additionOffset := len(p.add)
+ addition := Piece{origin: false, offset: additionOffset, length: len(textBytes)}
+
p.add = append(p.add, textBytes...)
- counter := 0
-
var piecesCopy []Piece
-
+ counter := 0
added := false
for _, piece := range p.state.pieces {
if int(position) < counter+piece.length && !added {
beforeLength := int(position) - counter
before := Piece{origin: piece.origin, offset: piece.offset, length: beforeLength}
- addition := Piece{origin: false, offset: additionOffset, length: len(textBytes)}
after := Piece{origin: piece.origin, offset: piece.offset + beforeLength, length: piece.length - beforeLength}
piecesCopy = append(piecesCopy, before)
piecesCopy = append(piecesCopy, addition)
piecesCopy = append(piecesCopy, after)
added = true
} else if int(position) == counter+piece.length && !added {
- addition := Piece{origin: false, offset: additionOffset, length: len(textBytes)}
-
piecesCopy = append(piecesCopy, piece)
piecesCopy = append(piecesCopy, addition)
added = true
} else {
piecesCopy = append(piecesCopy, piece)
}
counter += piece.length
+ }
+
+ if !added {
+ piecesCopy = append(piecesCopy, addition)
}
newState := &State{pieces: piecesCopy}
p.state.next = newState
newState.prev = p.state
p.state = newState
return p
}
func (p PieceTableEditor) Delete(offset, length uint) Editor {
var piecesCopy []Piece
counter := 0
toBeRemoved := -1
deleted := false
for _, piece := range p.state.pieces {
if toBeRemoved != -1 {
if piece.length < toBeRemoved {
toBeRemoved = toBeRemoved - piece.length
} else {
deletion := Piece{origin: piece.origin, offset: piece.offset + toBeRemoved, length: piece.length - toBeRemoved}
piecesCopy = append(piecesCopy, deletion)
toBeRemoved = -1
deleted = true
}
counter += piece.length
continue
}
if counter == int(offset) && !deleted {
if int(length) <= piece.length {
deletion := Piece{origin: piece.origin, offset: piece.offset + int(length), length: piece.length - int(length)}
piecesCopy = append(piecesCopy, deletion)
deleted = true
} else {
toBeRemoved = int(length) - piece.length
}
} else if int(offset) < counter+piece.length && !deleted {
if int(length) < piece.length {
beforeLength := int(offset) - counter
afterOffset := beforeLength + int(length) + piece.offset
before := Piece{origin: piece.origin, offset: piece.offset, length: beforeLength}
after := Piece{origin: piece.origin, offset: afterOffset, length: (piece.length + piece.offset) - afterOffset}
piecesCopy = append(piecesCopy, before)
piecesCopy = append(piecesCopy, after)
deleted = true
} else {
deletion := Piece{origin: piece.origin, offset: piece.offset, length: int(offset)}
piecesCopy = append(piecesCopy, deletion)
toBeRemoved = int(length) - (piece.length - int(offset))
}
} else {
piecesCopy = append(piecesCopy, piece)
}
counter += piece.length
}
newState := &State{pieces: piecesCopy}
p.state.next = newState
newState.prev = p.state
p.state = newState
return p
}
func (p PieceTableEditor) Undo() Editor {
prev := p.state.prev
if prev != nil {
p.state = prev
}
return p
}
func (p PieceTableEditor) Redo() Editor {
next := p.state.next
if next != nil {
p.state = next
}
return p
}
type PieceTableEditor struct {
origin []byte
add []byte
state *State
}
type State struct {
pieces []Piece
prev *State
next *State
}
type Piece struct {
origin bool
offset int
length int
}
func NewEditor(content string) Editor {
editor := PieceTableEditor{origin: []byte(content)}
piece := Piece{origin: true, offset: 0, length: len(content)}
var pieces []Piece
pieces = append(pieces, piece)
editor.state = &State{pieces: pieces}
return editor
}