-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGraph.elm
236 lines (184 loc) · 8.98 KB
/
Graph.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
module Graph where
import Model exposing (..)
import Dict
import Debug
import String exposing (toList, fromChar)
import List
import Constants exposing (endChar, startChar, emptyModel, endId, startId)
import ConvertJson exposing (wUpdateToJson)
import Set
import Woot exposing (..)
import Graphics.Input.Field exposing (..)
-- - - - - - I N S E R T A P I - - - - - - -
-- only called when TUpdate is local
generateInsert : Char -> Int -> Model -> (Model, Edit)
generateInsert ch place model =
let
debugModel = {model| debug = model.debug}-- ++ "~~~" ++ toString place ++ "~~~~" ++ "||||" ++ wToString model.wString ++ "|||"}
in
generateInsChar ch (place - 1) (place ) {debugModel | doc = updateCP model.doc place }
-- error case!
integrateRemoteInsert : WChar -> Model -> (Model, Edit)
integrateRemoteInsert wChar model =
let
next = grabNext wChar model.wString
insertPos = pos model.wString next
--- should prev line have wprev or wNext
prev = grabPrev wChar model.wString
currCP = model.doc.cp
newCP = if currCP > insertPos then currCP + 1 else currCP
newCPModel = {model | doc = updateCP model.doc newCP}
newModel = integrateInsert' wChar prev next insertPos newCPModel
debugModel = {newModel | debug = newModel.debug ++ "integrating: " ++ toString wChar ++ "site is: " ++ toString model.site ++ "next is" ++ toString next.ch ++ "prev is " ++ toString prev.ch}
in
if wChar.ch == 'l' then (debugModel, T (I wChar.ch insertPos (fst wChar.id))) else
(newModel, T (I wChar.ch insertPos (fst wChar.id)))
-- - - - - - I N S E R T I M P L E M E N T A T I O N - - - - - - -
insertIntoList : WChar -> WString -> Int -> WString
insertIntoList wCh wStr pos =
case wStr of
x :: xs -> if pos == 0
then wCh :: x :: xs
else x :: insertIntoList wCh xs (pos - 1)
[] -> [wCh]
intInsertChar : WChar -> Int -> Model -> Model
intInsertChar wCh pos model =
let
newWStr = insertIntoList wCh model.wString pos
newStr = wToString newWStr
newLen = String.length newStr
in
{model | wString = newWStr
, wSeen = Set.insert wCh.id model.wSeen
, doc = updateStrAndLen model.doc newStr newLen
}
integrateInsert' : WChar -> WChar -> WChar -> Int -> Model -> Model
integrateInsert' wCh pred succ posi model =
let
subStr = subSeq (startChar :: model.wString ++ [endChar]) pred succ
idOrderSubStr = pred :: (withoutPrecedenceOrdered subStr) ++ [succ]
(newPred, newSucc) = findLaterWChar wCh idOrderSubStr
in
case subStr of
[] ->
let
newModel = intInsertChar wCh (pos model.wString succ ) model
debugModel = {newModel | debug = newModel.debug
++ " MODEL BEFORE " ++ wToString model.wString
++ " //integrating " ++ toString wCh.ch
++ " site" ++ toString model.site
++ " pred - " ++ toString pred
++ " succ - " ++ toString succ
++ " subStr - " ++ toString subStr
++ " newPred - " ++ toString newPred
++ " new Succ - " ++ toString newSucc}
in
if wCh.ch == 'l' then debugModel else
newModel
x :: xs ->
let
debugModel = {model | debug = model.debug
++ "integrating WITH STUFF " ++ toString wCh.ch
++ " site" ++ toString model.site
++ " pred - " ++ toString pred
++ " succ - " ++ toString succ
++ " subStr - " ++ toString subStr
++ " id ordered " ++ toString idOrderSubStr
++ " newPred - " ++ toString newPred
++ " new Succ - " ++ toString newSucc}
in
if wCh.ch == 'l' then integrateInsert' wCh newPred newSucc posi debugModel else
integrateInsert' wCh newPred newSucc posi model
-- {newModel | debug = newModel.debug ++ "inserting: " ++ toString wCh.ch ++ "// " ++ " newPred:: " ++ toString newPred.ch ++ " NewSuc:: " ++ toString newSucc.ch}
generateInsChar : Char -> Int -> Int -> Model -> (Model, Edit)
generateInsChar char predIndex nextIndex model =
let
pred = ithVisible model.wString predIndex
succ = ithVisible model.wString nextIndex
newId = (model.site, model.counter)
newWChar = {id = newId
, ch = char
, prev = pred.id
, next = succ.id
, vis = 1}
newModel = {model | counter = model.counter + 1} --- at this point ! we arlready have a problem witht he
debugModel = {newModel | debug = "SITE IS:" ++ toString newModel.site ++ newModel.debug ++ "PRED INDEX IS: " ++ toString predIndex ++"((((((newWchar" ++ toString newWChar
++ " pred" ++ toString pred.ch
++ " w stirng is === " ++ toString model.wString
-- ++ "succ " ++ toString succ
-- ++ " pred index: " ++ toString predIndex
-- ++ " next Index " ++ toString nextIndex ++ "))))))"
}
in
--if char == 't' then
--(integrateInsert' newWChar pred succ nextIndex debugModel , W (Insert newWChar)) else
(integrateInsert' newWChar pred succ nextIndex newModel , W (Insert newWChar))
withoutPrecedenceOrdered : WString -> List WChar
withoutPrecedenceOrdered wStr =
let
prevAbsent wC = not (List.any (\x -> x.id == wC.prev) wStr)
nextAbsent wC = not (List.any (\x -> x.id == wC.next) wStr)
prevAndNextAbsent wC = prevAbsent wC && nextAbsent wC
in
List.filter prevAndNextAbsent wStr
-- the second in the pair is the thing that is a "greater" wChar..
findLaterWChar : WChar -> WString -> (WChar, WChar)
findLaterWChar insCh wStr =
case wStr of
[] -> Debug.crash "empty list"
-- error case!
x :: [] -> Debug.crash "one element in list"
-- error case
x :: y :: [] -> --(x, y)
if x `isLaterWChar` insCh then (startChar, x)
else if y `isLaterWChar` insCh then (x, y)
else (x, y) --- TODO this used to be (y, endChar)!!!!
x :: y :: xs ->
if y `isLaterWChar` insCh then
(x, y)
else
findLaterWChar insCh (y::xs)
-- - - - - - D E L E T E A P I - - - - - - -
generateDelete : Char -> Int -> Model -> (Model, Edit)
generateDelete ch place model =
let
predecessor = ithVisible model.wString (place - 1)----== my problem
successor = ithVisible model.wString (place + 1)
currWChar = ithVisible model.wString (place)
deletedWChar = {currWChar | vis = -1}
newModel = {model |
doc = updateCP model.doc place
--, debug = "CHAR deleteing is -- " ++ fromChar ch
--++ " DELETING: "
--++ String.fromChar currWChar.ch ++ "/thisIndex: "
--++ toString place ++ "/pred :" ++ String.fromChar predecessor.ch
--++ "/succ: " ++ String.fromChar successor.ch
--++ "/place: " ++ toString place
}
in
(integrateDelete deletedWChar newModel, W (Delete deletedWChar))
integrateRemoteDelete : WChar -> Model -> (Model, Edit)
integrateRemoteDelete wChar model =
let
currCP = model.doc.cp
deletePos = pos model.wString wChar
newCP = if currCP > deletePos then currCP - 1 else currCP
newDocModel = {model | doc = (updateCP model.doc newCP)
--, debug = "deleting" ++ fromChar wChar.ch ++ " at " ++ toString deletePos
}
newModel = integrateDelete wChar newDocModel
in
(newModel, T (D wChar.ch deletePos))
integrateDelete : WChar -> Model -> Model
integrateDelete wChar model =
let
newWString = setInvisible model.wString wChar.id
in
{model | wString = newWString}
-- - -- - - - - - - - - - - - - - - - - - - - - -
updateCP : Doc -> Int -> Doc
updateCP doc cp =
{doc | cp = cp}
updateStrAndLen : Doc -> String -> Int -> Doc
updateStrAndLen doc str len =
{doc | str = str, len = len}