Решение на Piece table от Михаил Копчев

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

Към профила на Михаил Копчев

Резултати

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

Код

package main
const (
UNDO int = iota
REDO
)
type Operation interface {
Do()
}
type operationManager struct {
undoes []Operation
redoes []Operation
}
func (op *operationManager) size(o int) int {
if o == UNDO {
return len(op.undoes)
} else {
return len(op.redoes)
}
}
func (op *operationManager) pop(from int) Operation {
if from == UNDO {
last := op.undoes[len(op.undoes)-1]
op.undoes = op.undoes[:len(op.undoes)-1]
return last
} else {
last := op.redoes[len(op.redoes)-1]
op.redoes = op.redoes[:len(op.redoes)-1]
return last
}
}
func (op *operationManager) add(operation Operation, o int) {
if o == UNDO {
op.undoes = append(op.undoes, operation)
} else if o == REDO {
op.redoes = append(op.redoes, operation)
}
}
type Insert struct {
typ int
f func(p uint, t string) *MyEditor
position uint
text string
}
type Delete struct {
f func(o, l uint) *MyEditor
offset uint
length uint
}
func (d Delete) Do() {
d.f(d.offset, d.length)
}
func (i Insert) Do() {
i.f(i.position, i.text)
}
type Editor interface {
Insert(position uint, text string) Editor
Delete(offset, length uint) Editor
Undo() Editor
Redo() Editor
String() string
}
type piece struct {
origin bool
offset uint
length uint
}
type MyEditor struct {
origin string
add string
table []piece
curLen int
operationManager *operationManager
}
func (m *MyEditor) reverseOperation(operation Operation) {
}
func (m *MyEditor) String() string {
var result string
var buffer string
for _, pie := range m.table {
if pie.origin {
buffer = m.origin
} else {
buffer = m.add
}
result += m.read(buffer, pie.offset, pie.length)
}
return result
}
func (m *MyEditor) read(buffer string, start, how uint) string {
return buffer[start : start+how]
}
func (m *MyEditor) sequence(offset, length uint) string {
var res string
pieIndex, _ := m.pieIndexAndResidualOffset(offset)
lastPieIndex, lastResidual := m.pieIndexAndResidualOffset(offset + length)
var buffer string
if pieIndex == lastPieIndex {
if m.table[pieIndex].origin {
buffer = m.origin
} else {
buffer = m.add
}
res += m.read(buffer, offset, length)
return res
}
for i := pieIndex; i <= lastPieIndex; i++ {
pie := m.table[i]
if pie.origin {
buffer = m.origin
} else {
buffer = m.add
}
if i == lastPieIndex {
res += m.read(buffer, pie.offset, pie.length-lastResidual)
} else {
res += m.read(buffer, pie.offset, pie.length)
}
}
return res
}
func (m *MyEditor) modifyLen(len int) {
m.curLen = m.curLen + len
}
func (m *MyEditor) Redo() MyEditor {
if m.operationManager != nil && m.operationManager.size(REDO) > 0 {
operation := m.operationManager.pop(REDO)
operation.Do()
//m.operationManager.add(, UNDO)
}
return *m
}
func (m *MyEditor) Undo() MyEditor {
if m.operationManager != nil && m.operationManager.size(UNDO) > 0 {
operation := m.operationManager.pop(UNDO)
operation.Do()
//reverseOperation := m.operationManager.
//m.operationManager.add(, REDO)
}
return *m
}
func (m *MyEditor) Delete(offset, len uint) *MyEditor {
currentLen := uint(m.curLen)
if currentLen < offset {
return m
}
if offset+len >= currentLen {
len = currentLen - offset
}
pieIndex, residualOffset := m.pieIndexAndResidualOffset(offset)
lastPieIndex, lastResidualOffset := m.pieIndexAndResidualOffset(offset + len)
first := piece{
origin: m.table[pieIndex].origin,
offset: m.table[pieIndex].offset,
length: residualOffset,
}
second := piece{
origin: m.table[lastPieIndex].origin,
offset: m.table[lastPieIndex].offset + lastResidualOffset,
length: m.table[lastPieIndex].length - lastResidualOffset,
}
m.modifyLen(-int(len))
parts := []piece{first, second}
prefix := m.table[:pieIndex]
how := lastPieIndex - pieIndex + 1
suffix := m.table[pieIndex+how:]
parts = append(parts, suffix...)
whole := append(prefix, parts...)
m.table = whole
//m.operationManager.add(Insert{m.Insert, offset, ""}, UNDO)
return m
}
func (m *MyEditor) Insert(position uint, text string) *MyEditor {
currentLen := uint(m.curLen)
if position > currentLen {
position = currentLen
}
addOffset := uint(len(m.add))
m.add += text
m.modifyLen(len(text))
pieIndex, residualOffset := m.pieIndexAndResidualOffset(position)
currentPiece := m.table[pieIndex]
first := piece{
origin: currentPiece.origin,
offset: currentPiece.offset,
length: residualOffset,
}
second := piece{
origin: false,
offset: addOffset,
length: uint(len(text)),
}
third := piece{
origin: currentPiece.origin,
offset: residualOffset,
length: currentPiece.length - residualOffset,
}
parts := []piece{first, second, third}
prefix := m.table[:pieIndex]
suffix := m.table[pieIndex+1:]
whole := append(prefix, append(parts, suffix...)...)
m.table = whole
//m.operationManager.add(Delete{m.Delete, position, uint(len(text))}, UNDO)
return m
}
func (m *MyEditor) pieIndexAndResidualOffset(offset uint) (int, uint) {
residualOffset := offset
for index, pie := range m.table {
if residualOffset <= pie.length {
return index, residualOffset
}
residualOffset = residualOffset - pie.length
}
return len(m.table), 0
}
func NewEditor(text string) *MyEditor {
myEditor := new(MyEditor)
myEditor.origin = text
myEditor.curLen = len(text)
table := []piece{{true, 0, uint(len(text))}}
myEditor.table = table
opM := &operationManager{}
myEditor.operationManager = opM
return myEditor
}

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

# _/tmp/d20181107-53-14qfj0n [_/tmp/d20181107-53-14qfj0n.test]
./solution_test.go:28:33: cannot call pointer method on f.Undo()
./solution_test.go:28:33: cannot take the address of f.Undo()
./solution_test.go:31:46: cannot call pointer method on f.Delete(2, 6).Undo()
./solution_test.go:31:46: cannot take the address of f.Delete(2, 6).Undo()
./solution_test.go:40:45: cannot call pointer method on f.Undo()
./solution_test.go:40:45: cannot take the address of f.Undo()
./solution_test.go:40:52: cannot call pointer method on f.Undo().Undo()
./solution_test.go:40:52: cannot take the address of f.Undo().Undo()
./solution_test.go:40:59: cannot call pointer method on f.Undo().Undo().Undo()
./solution_test.go:40:59: cannot take the address of f.Undo().Undo().Undo()
./solution_test.go:40:59: too many errors
FAIL	_/tmp/d20181107-53-14qfj0n [build failed]
# _/tmp/d20181107-53-14qfj0n [_/tmp/d20181107-53-14qfj0n.test]
./solution_test.go:28:33: cannot call pointer method on f.Undo()
./solution_test.go:28:33: cannot take the address of f.Undo()
./solution_test.go:31:46: cannot call pointer method on f.Delete(2, 6).Undo()
./solution_test.go:31:46: cannot take the address of f.Delete(2, 6).Undo()
./solution_test.go:40:45: cannot call pointer method on f.Undo()
./solution_test.go:40:45: cannot take the address of f.Undo()
./solution_test.go:40:52: cannot call pointer method on f.Undo().Undo()
./solution_test.go:40:52: cannot take the address of f.Undo().Undo()
./solution_test.go:40:59: cannot call pointer method on f.Undo().Undo().Undo()
./solution_test.go:40:59: cannot take the address of f.Undo().Undo().Undo()
./solution_test.go:40:59: too many errors
FAIL	_/tmp/d20181107-53-14qfj0n [build failed]
# _/tmp/d20181107-53-14qfj0n [_/tmp/d20181107-53-14qfj0n.test]
./solution_test.go:28:33: cannot call pointer method on f.Undo()
./solution_test.go:28:33: cannot take the address of f.Undo()
./solution_test.go:31:46: cannot call pointer method on f.Delete(2, 6).Undo()
./solution_test.go:31:46: cannot take the address of f.Delete(2, 6).Undo()
./solution_test.go:40:45: cannot call pointer method on f.Undo()
./solution_test.go:40:45: cannot take the address of f.Undo()
./solution_test.go:40:52: cannot call pointer method on f.Undo().Undo()
./solution_test.go:40:52: cannot take the address of f.Undo().Undo()
./solution_test.go:40:59: cannot call pointer method on f.Undo().Undo().Undo()
./solution_test.go:40:59: cannot take the address of f.Undo().Undo().Undo()
./solution_test.go:40:59: too many errors
FAIL	_/tmp/d20181107-53-14qfj0n [build failed]
# _/tmp/d20181107-53-14qfj0n [_/tmp/d20181107-53-14qfj0n.test]
./solution_test.go:28:33: cannot call pointer method on f.Undo()
./solution_test.go:28:33: cannot take the address of f.Undo()
./solution_test.go:31:46: cannot call pointer method on f.Delete(2, 6).Undo()
./solution_test.go:31:46: cannot take the address of f.Delete(2, 6).Undo()
./solution_test.go:40:45: cannot call pointer method on f.Undo()
./solution_test.go:40:45: cannot take the address of f.Undo()
./solution_test.go:40:52: cannot call pointer method on f.Undo().Undo()
./solution_test.go:40:52: cannot take the address of f.Undo().Undo()
./solution_test.go:40:59: cannot call pointer method on f.Undo().Undo().Undo()
./solution_test.go:40:59: cannot take the address of f.Undo().Undo().Undo()
./solution_test.go:40:59: too many errors
FAIL	_/tmp/d20181107-53-14qfj0n [build failed]
# _/tmp/d20181107-53-14qfj0n [_/tmp/d20181107-53-14qfj0n.test]
./solution_test.go:28:33: cannot call pointer method on f.Undo()
./solution_test.go:28:33: cannot take the address of f.Undo()
./solution_test.go:31:46: cannot call pointer method on f.Delete(2, 6).Undo()
./solution_test.go:31:46: cannot take the address of f.Delete(2, 6).Undo()
./solution_test.go:40:45: cannot call pointer method on f.Undo()
./solution_test.go:40:45: cannot take the address of f.Undo()
./solution_test.go:40:52: cannot call pointer method on f.Undo().Undo()
./solution_test.go:40:52: cannot take the address of f.Undo().Undo()
./solution_test.go:40:59: cannot call pointer method on f.Undo().Undo().Undo()
./solution_test.go:40:59: cannot take the address of f.Undo().Undo().Undo()
./solution_test.go:40:59: too many errors
FAIL	_/tmp/d20181107-53-14qfj0n [build failed]
# _/tmp/d20181107-53-14qfj0n [_/tmp/d20181107-53-14qfj0n.test]
./solution_test.go:28:33: cannot call pointer method on f.Undo()
./solution_test.go:28:33: cannot take the address of f.Undo()
./solution_test.go:31:46: cannot call pointer method on f.Delete(2, 6).Undo()
./solution_test.go:31:46: cannot take the address of f.Delete(2, 6).Undo()
./solution_test.go:40:45: cannot call pointer method on f.Undo()
./solution_test.go:40:45: cannot take the address of f.Undo()
./solution_test.go:40:52: cannot call pointer method on f.Undo().Undo()
./solution_test.go:40:52: cannot take the address of f.Undo().Undo()
./solution_test.go:40:59: cannot call pointer method on f.Undo().Undo().Undo()
./solution_test.go:40:59: cannot take the address of f.Undo().Undo().Undo()
./solution_test.go:40:59: too many errors
FAIL	_/tmp/d20181107-53-14qfj0n [build failed]
# _/tmp/d20181107-53-14qfj0n [_/tmp/d20181107-53-14qfj0n.test]
./solution_test.go:28:33: cannot call pointer method on f.Undo()
./solution_test.go:28:33: cannot take the address of f.Undo()
./solution_test.go:31:46: cannot call pointer method on f.Delete(2, 6).Undo()
./solution_test.go:31:46: cannot take the address of f.Delete(2, 6).Undo()
./solution_test.go:40:45: cannot call pointer method on f.Undo()
./solution_test.go:40:45: cannot take the address of f.Undo()
./solution_test.go:40:52: cannot call pointer method on f.Undo().Undo()
./solution_test.go:40:52: cannot take the address of f.Undo().Undo()
./solution_test.go:40:59: cannot call pointer method on f.Undo().Undo().Undo()
./solution_test.go:40:59: cannot take the address of f.Undo().Undo().Undo()
./solution_test.go:40:59: too many errors
FAIL	_/tmp/d20181107-53-14qfj0n [build failed]
# _/tmp/d20181107-53-14qfj0n [_/tmp/d20181107-53-14qfj0n.test]
./solution_test.go:28:33: cannot call pointer method on f.Undo()
./solution_test.go:28:33: cannot take the address of f.Undo()
./solution_test.go:31:46: cannot call pointer method on f.Delete(2, 6).Undo()
./solution_test.go:31:46: cannot take the address of f.Delete(2, 6).Undo()
./solution_test.go:40:45: cannot call pointer method on f.Undo()
./solution_test.go:40:45: cannot take the address of f.Undo()
./solution_test.go:40:52: cannot call pointer method on f.Undo().Undo()
./solution_test.go:40:52: cannot take the address of f.Undo().Undo()
./solution_test.go:40:59: cannot call pointer method on f.Undo().Undo().Undo()
./solution_test.go:40:59: cannot take the address of f.Undo().Undo().Undo()
./solution_test.go:40:59: too many errors
FAIL	_/tmp/d20181107-53-14qfj0n [build failed]

История (2 версии и 1 коментар)

Михаил обнови решението на 06.11.2018 14:22 (преди 9 месеца)

+package main
+
+type Operation interface {
+ Do()
+}
+type Insert struct {
+ f func(p uint, t string) *MyEditor
+ position uint
+ text string
+}
+type Delete struct {
+ f func(o, l uint) *MyEditor
+ offset uint
+ length uint
+}
+
+func (d Delete) Do() {
+ d.f(d.offset, d.length)
+}
+func (i Insert) Do() {
+ i.f(i.position, i.text)
+}
+
+type Editor interface {
+ Insert(position uint, text string) Editor
+
+ Delete(offset, length uint) Editor
+
+ Undo() Editor
+
+ Redo() Editor
+
+ String() string
+}
+
+type piece struct {
+ origin bool
+ offset uint
+ length uint
+}
+
+type MyEditor struct {
+ origin string
+ add string
+ table []piece
+ curLen int
+
+ undoes []Operation
+ redoes []Operation
+}
+
+type del func(uint, uint) *MyEditor
+
+func (m *MyEditor) String() string {
+ var str string
+ for _, pie := range m.table {
+ if pie.origin {
+ str += m.origin[pie.offset:(pie.offset + pie.length)]
+ } else {
+ str += m.add[pie.offset:(pie.offset + pie.length)]
+ }
+ }
+ return str
+}
+func (m *MyEditor) modifyLen(len int) {
+ m.curLen = m.curLen + len
+}
+
+func (m *MyEditor) Redo() MyEditor {
+ if m.redoes != nil && len(m.redoes) > 0 {
+
+ }
+
+ return *m
+}
+
+func (m *MyEditor) Undo() MyEditor {
+ if m.undoes != nil && len(m.undoes) > 0 {
+ last := m.undoes[len(m.undoes)-1]
+ last.Do()
+ // add opposite to redo
+ }
+ return *m
+}
+
+func (m *MyEditor) Delete(offset, len uint) *MyEditor {
+ currentLen := uint(m.curLen)
+
+ if currentLen < offset {
+ return m
+ }
+
+ if offset+len >= currentLen {
+ len = currentLen - offset
+ }
+
+ pieIndex, residualOffset := m.pieIndexAndResidualOffset(offset)
+ lastPieIndex, lastResidualOffset := m.pieIndexAndResidualOffset(offset + len)
+
+ first := piece{
+ origin: m.table[pieIndex].origin,
+ offset: m.table[pieIndex].offset,
+ length: residualOffset,
+ }
+ second := piece{
+ origin: m.table[lastPieIndex].origin,
+ offset: m.table[lastPieIndex].offset + lastResidualOffset,
+ length: m.table[lastPieIndex].length - lastResidualOffset,
+ }
+
+ m.modifyLen(-int(len))
+ parts := []piece{first, second}
+
+ prefix := m.table[:pieIndex]
+ how := lastPieIndex - pieIndex + 1
+ suffix := m.table[pieIndex+how:]
+ whole := append(prefix, append(parts, suffix...)...)
+ m.table = whole
+
+ var i Insert = Insert{m.Insert, offset, ""}
+ m.undoes = append(m.undoes, i)
+
+ return m
+}
+
+func (m *MyEditor) Insert(position uint, text string) *MyEditor {
+ currentLen := uint(m.curLen)
+
+ if position > currentLen {
+ position = currentLen
+ }
+ addOffset := uint(len(m.add))
+ m.add += text
+
+ m.modifyLen(len(text))
+
+ pieIndex, residualOffset := m.pieIndexAndResidualOffset(position)
+ currentPiece := m.table[pieIndex]
+
+ first := piece{
+ origin: currentPiece.origin,
+ offset: currentPiece.offset,
+ length: residualOffset,
+ }
+ second := piece{
+ origin: false,
+ offset: addOffset,
+ length: uint(len(text)),
+ }
+ third := piece{
+ origin: currentPiece.origin,
+ offset: residualOffset,
+ length: currentPiece.length - residualOffset,
+ }
+
+ parts := []piece{first, second, third}
+ prefix := m.table[:pieIndex]
+ suffix := m.table[pieIndex+1:]
+ whole := append(prefix, append(parts, suffix...)...)
+ m.table = whole
+
+ var d Delete = Delete{m.Delete, position, uint(len(text))}
+ m.undoes = append(m.undoes, d)
+ return m
+}
+func (m *MyEditor) pieIndexAndResidualOffset(offset uint) (int, uint) {
+ residualOffset := offset
+
+ for index, pie := range m.table {
+ if residualOffset <= pie.length {
+ return index, residualOffset
+ }
+ residualOffset = residualOffset - pie.length
+ }
+ return 0, 0 // todo return last element no just 0,0
+
+}
+
+func NewEditor(text string) *MyEditor {
+ myEditor := new(MyEditor)
+ myEditor.origin = text
+ myEditor.curLen = len(text)
+ table := []piece{{true, 0, uint(len(text))}}
+ myEditor.table = table
+
+ return myEditor
+}

Михаил обнови решението на 07.11.2018 16:56 (преди 9 месеца)

package main
+const (
+ UNDO int = iota
+ REDO
+)
+
type Operation interface {
Do()
}
+type operationManager struct {
+ undoes []Operation
+ redoes []Operation
+}
+
+func (op *operationManager) size(o int) int {
+ if o == UNDO {
+ return len(op.undoes)
+ } else {
+ return len(op.redoes)
+ }
+}
+
+func (op *operationManager) pop(from int) Operation {
+ if from == UNDO {
+ last := op.undoes[len(op.undoes)-1]
+ op.undoes = op.undoes[:len(op.undoes)-1]
+ return last
+ } else {
+ last := op.redoes[len(op.redoes)-1]
+ op.redoes = op.redoes[:len(op.redoes)-1]
+ return last
+ }
+}
+
+func (op *operationManager) add(operation Operation, o int) {
+ if o == UNDO {
+ op.undoes = append(op.undoes, operation)
+ } else if o == REDO {
+ op.redoes = append(op.redoes, operation)
+ }
+}
+
type Insert struct {
+ typ int
f func(p uint, t string) *MyEditor
position uint
text string
}
type Delete struct {
f func(o, l uint) *MyEditor
offset uint
length uint
}
func (d Delete) Do() {
d.f(d.offset, d.length)
}
func (i Insert) Do() {
i.f(i.position, i.text)
}
type Editor interface {
Insert(position uint, text string) Editor
Delete(offset, length uint) Editor
Undo() Editor
Redo() Editor
String() string
}
type piece struct {
origin bool
offset uint
length uint
}
type MyEditor struct {
- origin string
- add string
- table []piece
- curLen int
+ origin string
+ add string
+ table []piece
+ curLen int
+ operationManager *operationManager
+}
- undoes []Operation
- redoes []Operation
+func (m *MyEditor) reverseOperation(operation Operation) {
}
-type del func(uint, uint) *MyEditor
-
func (m *MyEditor) String() string {
- var str string
+ var result string
+ var buffer string
for _, pie := range m.table {
if pie.origin {
- str += m.origin[pie.offset:(pie.offset + pie.length)]
+ buffer = m.origin
} else {
- str += m.add[pie.offset:(pie.offset + pie.length)]
+ buffer = m.add
}
+ result += m.read(buffer, pie.offset, pie.length)
}
- return str
+ return result
}
+func (m *MyEditor) read(buffer string, start, how uint) string {
+ return buffer[start : start+how]
+}
+func (m *MyEditor) sequence(offset, length uint) string {
+ var res string
+
+ pieIndex, _ := m.pieIndexAndResidualOffset(offset)
+ lastPieIndex, lastResidual := m.pieIndexAndResidualOffset(offset + length)
+
+ var buffer string
+
+ if pieIndex == lastPieIndex {
+ if m.table[pieIndex].origin {
+ buffer = m.origin
+ } else {
+ buffer = m.add
+ }
+ res += m.read(buffer, offset, length)
+ return res
+ }
+
+ for i := pieIndex; i <= lastPieIndex; i++ {
+ pie := m.table[i]
+ if pie.origin {
+ buffer = m.origin
+ } else {
+ buffer = m.add
+ }
+ if i == lastPieIndex {
+ res += m.read(buffer, pie.offset, pie.length-lastResidual)
+ } else {
+ res += m.read(buffer, pie.offset, pie.length)
+ }
+
+ }
+
+ return res
+
+}
+
func (m *MyEditor) modifyLen(len int) {
m.curLen = m.curLen + len
}
func (m *MyEditor) Redo() MyEditor {
- if m.redoes != nil && len(m.redoes) > 0 {
-
+ if m.operationManager != nil && m.operationManager.size(REDO) > 0 {
+ operation := m.operationManager.pop(REDO)
+ operation.Do()
+ //m.operationManager.add(, UNDO)
}
return *m
}
func (m *MyEditor) Undo() MyEditor {
- if m.undoes != nil && len(m.undoes) > 0 {
- last := m.undoes[len(m.undoes)-1]
- last.Do()
- // add opposite to redo
+ if m.operationManager != nil && m.operationManager.size(UNDO) > 0 {
+ operation := m.operationManager.pop(UNDO)
+ operation.Do()
+ //reverseOperation := m.operationManager.
+ //m.operationManager.add(, REDO)
}
return *m
}
func (m *MyEditor) Delete(offset, len uint) *MyEditor {
currentLen := uint(m.curLen)
if currentLen < offset {
return m
}
if offset+len >= currentLen {
len = currentLen - offset
}
pieIndex, residualOffset := m.pieIndexAndResidualOffset(offset)
lastPieIndex, lastResidualOffset := m.pieIndexAndResidualOffset(offset + len)
first := piece{
origin: m.table[pieIndex].origin,
offset: m.table[pieIndex].offset,
length: residualOffset,
}
second := piece{
origin: m.table[lastPieIndex].origin,
offset: m.table[lastPieIndex].offset + lastResidualOffset,
length: m.table[lastPieIndex].length - lastResidualOffset,
}
m.modifyLen(-int(len))
parts := []piece{first, second}
prefix := m.table[:pieIndex]
how := lastPieIndex - pieIndex + 1
suffix := m.table[pieIndex+how:]
- whole := append(prefix, append(parts, suffix...)...)
+
+ parts = append(parts, suffix...)
+ whole := append(prefix, parts...)
m.table = whole
- var i Insert = Insert{m.Insert, offset, ""}
- m.undoes = append(m.undoes, i)
+ //m.operationManager.add(Insert{m.Insert, offset, ""}, UNDO)
return m
}
func (m *MyEditor) Insert(position uint, text string) *MyEditor {
currentLen := uint(m.curLen)
if position > currentLen {
position = currentLen
}
addOffset := uint(len(m.add))
m.add += text
m.modifyLen(len(text))
pieIndex, residualOffset := m.pieIndexAndResidualOffset(position)
currentPiece := m.table[pieIndex]
first := piece{
origin: currentPiece.origin,
offset: currentPiece.offset,
length: residualOffset,
}
second := piece{
origin: false,
offset: addOffset,
length: uint(len(text)),
}
third := piece{
origin: currentPiece.origin,
offset: residualOffset,
length: currentPiece.length - residualOffset,
}
parts := []piece{first, second, third}
prefix := m.table[:pieIndex]
suffix := m.table[pieIndex+1:]
whole := append(prefix, append(parts, suffix...)...)
m.table = whole
- var d Delete = Delete{m.Delete, position, uint(len(text))}
- m.undoes = append(m.undoes, d)
+ //m.operationManager.add(Delete{m.Delete, position, uint(len(text))}, UNDO)
return m
}
func (m *MyEditor) pieIndexAndResidualOffset(offset uint) (int, uint) {
residualOffset := offset
for index, pie := range m.table {
if residualOffset <= pie.length {
return index, residualOffset
}
residualOffset = residualOffset - pie.length
}
- return 0, 0 // todo return last element no just 0,0
+ return len(m.table), 0
}
-
func NewEditor(text string) *MyEditor {
myEditor := new(MyEditor)
myEditor.origin = text
myEditor.curLen = len(text)
table := []piece{{true, 0, uint(len(text))}}
myEditor.table = table
+ opM := &operationManager{}
+ myEditor.operationManager = opM
return myEditor
}